From f0011a1359d83912cd283eadba4af64e44d4208a Mon Sep 17 00:00:00 2001 From: NetherEran Date: Thu, 10 Sep 2020 17:55:57 +0200 Subject: [PATCH] add game end screen, lay down base for highscore list --- Makefile | 12 ++++++++---- endscreen.c | 43 +++++++++++++++++++++++++++++++++++++++++++ endscreen.h | 8 ++++++++ field.c | 1 + game.c | 1 - main.c | 17 ++++++++--------- 6 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 endscreen.c create mode 100644 endscreen.h diff --git a/Makefile b/Makefile index d190d1b..5b61205 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -snek : main.o field.o ball.o player.o game.o - gcc -o snek main.o field.o ball.o player.o game.o -lncurses -lm +snek : main.o field.o ball.o player.o endscreen.o game.o + gcc -o snek main.o field.o ball.o player.o endscreen.o game.o -lncurses -lm main.o : main.c gcc -Wall -c main.c @@ -12,9 +12,13 @@ field.o : field.c player.o : player.c gcc -Wall -c player.c - + +endscreen.o : endscreen.c + gcc -Wall -c endscreen.c + game.o : game.c gcc -Wall -c game.c + clean: - rm main.o field.o ball.o snek player.o game.o + rm snek main.o field.o ball.o player.o game.o endscreen.o diff --git a/endscreen.c b/endscreen.c new file mode 100644 index 0000000..f2db3be --- /dev/null +++ b/endscreen.c @@ -0,0 +1,43 @@ +#include +#include +#include "endscreen.h" + +int* load_highscores() { + FILE* file; + if ((file = fopen(HIGHSCORE_FILE, "r")) == 0) { + int* ret = malloc(sizeof(int)); + ret[0] = 0; + return ret; + } + return NULL; +} + +void display_endscreen(int currentScore) { + if (2 * 40 > COLS || 20 > LINES) { + endwin(); + fprintf(stderr, "Snek: Terminal is to small, must be 80x20 at least\n"); + exit(1); + } + WINDOW* win = subwin(stdscr, 20, 80, 0, 0); + clear(); + wattrset(win, COLOR_PAIR(7)); + mvwprintw(win, 1, 1, "GAME OVER! YOUR SCORE: %d", currentScore); + mvwprintw(win, 2, 1, "Press SPACEBAR to play again, q to quit"); + wrefresh(win); + nodelay(stdscr, FALSE); + while(1) { + char c = getch(); + switch(c) { + case ' ': + nodelay(stdscr, TRUE); + delwin(win); + return; + case 'q': + endwin(); + exit(0); + default: + break; + } + } +} + diff --git a/endscreen.h b/endscreen.h new file mode 100644 index 0000000..36c381f --- /dev/null +++ b/endscreen.h @@ -0,0 +1,8 @@ +#ifndef _ENDSCREEN_H_ +#define _ENDSCREEN_H_ + +#define HIGHSCORE_FILE "./highscores.txt" + +void display_endscreen(int currentScore); + +#endif diff --git a/field.c b/field.c index acf0ddf..a5eb20e 100644 --- a/field.c +++ b/field.c @@ -56,6 +56,7 @@ void destroy_field(FIELD* f) { } free(f -> balls); + delwin(f -> win); free(f); } diff --git a/game.c b/game.c index 3e1701b..080f1c6 100644 --- a/game.c +++ b/game.c @@ -85,5 +85,4 @@ void play_game(GAME_STATE* state) { timeout.tv_usec = 50000; } } - free(f); } diff --git a/main.c b/main.c index e8113ac..00a0aa4 100644 --- a/main.c +++ b/main.c @@ -4,7 +4,7 @@ #include #include #include "game.h" - +#include "endscreen.h" @@ -34,16 +34,15 @@ int main(int argc, char** argv) { cbreak(); curs_set(0); - GAME_STATE* state = malloc(sizeof(GAME_STATE)); - - play_game(state); - + while (1) { + GAME_STATE* state = malloc(sizeof(GAME_STATE)); + play_game(state); + display_endscreen(state -> points); + free(state); + } endwin(); - - printf("GAME OVER! POINTS: %d\n", state -> points); - + //printf("GAME OVER! POINTS: %d\n", state -> points); free(state); - return 0; }