From 906afa5e5b9e20cc41d5fbfc3205562c75eb3449 Mon Sep 17 00:00:00 2001 From: girst Date: Tue, 22 May 2018 15:08:40 +0200 Subject: [PATCH] nice end screen --- README.md | 6 +++--- viiper.c | 42 +++++++++++++++++++++++++++++++++++------- viiper.h | 1 + 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9699f98..a883356 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,10 @@ r to restart, p to pause, q to quit. - DONE unicode chars - DONE input buffer (so fast 180° turns get executed) - DONE only redraw changing parts of the screen - - PoC: input out of whack when stopping (^Z) and resuming + - DONE input out of whack when stopping (^Z) and resuming + - DONE keybindings for restart, pause, redraw + - DONE on dying: show end screen, allow restarting - timer, score, increasing speed - - keybindings for restart, pause, redraw - - on dying: show end screen, allow restarting - bonus/special items: slower snake, shorter snake, etc. - decaying points? (more points the faster you get the food) - wall-wrap-around mode? diff --git a/viiper.c b/viiper.c index 6c051ce..96c3c6f 100644 --- a/viiper.c +++ b/viiper.c @@ -112,6 +112,7 @@ int main (int argc, char** argv) { screen_setup(1); atexit (*quit); +restart: switch (sigsetjmp(game_over, 1)) { case GAME_INIT: case GAME_START: @@ -119,14 +120,16 @@ int main (int argc, char** argv) { case GAME_OVER: timer_setup(0); show_playfield(); - move_ph (g.h/2+LINE_OFFSET, g.w); - printf ("snek ded :("); - fflush(stdout); - sleep(2); + for (;;) switch (end_screen()) { + case 'r': goto restart; + case 'q': goto quit; + default: continue; + } case GAME_EXIT: - exit(0); + goto quit; } +quit: return 0; } @@ -171,7 +174,7 @@ int viiper(void) { #define pop_dir() (g.k.n? g.k.c[(16+g.k.h-g.k.n--)%16] : NONE) void snake_advance (void) { int respawn = 0; - struct item* i; /* temporary item (defined here to respawn at the end) */ + struct item* i; /*temporary item (defined here to respawn at the end) */ int new_dir = pop_dir(); /* switch direction if new one is in the buffer and it won't kill us: */ if (new_dir && g.d != OPPOSITE(new_dir)) g.d = new_dir; @@ -218,6 +221,7 @@ try_again: col = rand() % g.w; /* loop through snake to check if we aren't on it */ //WARN: inefficient as snake gets longer; near impossible in the end + //TODO: check if game won for (struct snake* s = g.s; s; s = s->next) if (s->r == row && s->c == col) goto try_again; @@ -287,7 +291,7 @@ void draw_sprites (int erase_r, int erase_c) { struct snake* last = NULL; int color = 2; for (struct snake* s = g.s; s; s = s->next) { - move_ph (s->r+LINE_OFFSET, s->c*CW+COL_OFFSET); /*NOTE: all those are actually wrong; draws snake 1 col too far left*/ + move_ph (s->r+LINE_OFFSET, s->c*CW+COL_OFFSET); int predecessor = (last==NULL)?NONE: (last->r < s->r) ? NORTH: @@ -320,6 +324,30 @@ void draw_sprites (int erase_r, int erase_c) { printf ("%s %0*d %s", BORDER(S,L), score_width, g.p, BORDER(S,R)); } +#define MOVE_POPUP(WIDTH, LINE) \ + move_ph(g.h/2+LINE_OFFSET-1+LINE,(g.w*op.scheme->display_width-WIDTH)/2) +int end_screen(void) { + MOVE_POPUP(11, -1); + print(BORDER(T,L)); + printm (12/op.scheme->display_width, BORDER(T,C)); + print (BORDER(T,R)); + + MOVE_POPUP(11, 0); + printf("%s GAME OVER %s", BORDER(C,L), BORDER(C,R)); + MOVE_POPUP(11, 1); + printf("%s `r' restart%s", BORDER(C,L), BORDER(C,R)); + MOVE_POPUP(11, 2); + printf("%s `q' quit %s", BORDER(C,L), BORDER(C,R)); + + MOVE_POPUP(11, 3); + print(BORDER(B,L)); + printm (12/op.scheme->display_width, BORDER(B,C)); + print (BORDER(B,R)); + fflush(stdout); + + return getctrlseq(); +} + void snake_append (struct snake** s, int row, int col) { struct snake* new = malloc (sizeof(struct snake)); new->r = row; diff --git a/viiper.h b/viiper.h index 71a6a5f..d624f88 100644 --- a/viiper.h +++ b/viiper.h @@ -65,6 +65,7 @@ void spawn_item (int type, int value, struct item* p_item); void consume_item (struct item* i); void show_playfield (void); void draw_sprites (int erase_r, int erase_c); +int end_screen(void); void snake_append (struct snake** s, int row, int col); void init_snake(); void quit (void); -- 2.39.3