add game end screen, lay down base for highscore list

master
NetherEran 2020-09-10 17:55:57 +02:00
parent 6c29862ff8
commit f0011a1359
6 changed files with 68 additions and 14 deletions

View File

@ -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

43
endscreen.c Normal file
View File

@ -0,0 +1,43 @@
#include <curses.h>
#include <stdlib.h>
#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;
}
}
}

8
endscreen.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _ENDSCREEN_H_
#define _ENDSCREEN_H_
#define HIGHSCORE_FILE "./highscores.txt"
void display_endscreen(int currentScore);
#endif

View File

@ -56,6 +56,7 @@ void destroy_field(FIELD* f) {
}
free(f -> balls);
delwin(f -> win);
free(f);
}

1
game.c
View File

@ -85,5 +85,4 @@ void play_game(GAME_STATE* state) {
timeout.tv_usec = 50000;
}
}
free(f);
}

17
main.c
View File

@ -4,7 +4,7 @@
#include <unistd.h>
#include <time.h>
#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;
}