add game end screen, lay down base for highscore list
parent
6c29862ff8
commit
f0011a1359
12
Makefile
12
Makefile
|
@ -1,5 +1,5 @@
|
||||||
snek : main.o field.o ball.o player.o game.o
|
snek : main.o field.o ball.o player.o endscreen.o game.o
|
||||||
gcc -o snek main.o field.o ball.o player.o game.o -lncurses -lm
|
gcc -o snek main.o field.o ball.o player.o endscreen.o game.o -lncurses -lm
|
||||||
|
|
||||||
main.o : main.c
|
main.o : main.c
|
||||||
gcc -Wall -c main.c
|
gcc -Wall -c main.c
|
||||||
|
@ -12,9 +12,13 @@ field.o : field.c
|
||||||
|
|
||||||
player.o : player.c
|
player.o : player.c
|
||||||
gcc -Wall -c player.c
|
gcc -Wall -c player.c
|
||||||
|
|
||||||
|
endscreen.o : endscreen.c
|
||||||
|
gcc -Wall -c endscreen.c
|
||||||
|
|
||||||
game.o : game.c
|
game.o : game.c
|
||||||
gcc -Wall -c game.c
|
gcc -Wall -c game.c
|
||||||
|
|
||||||
|
|
||||||
clean:
|
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
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef _ENDSCREEN_H_
|
||||||
|
#define _ENDSCREEN_H_
|
||||||
|
|
||||||
|
#define HIGHSCORE_FILE "./highscores.txt"
|
||||||
|
|
||||||
|
void display_endscreen(int currentScore);
|
||||||
|
|
||||||
|
#endif
|
1
field.c
1
field.c
|
@ -56,6 +56,7 @@ void destroy_field(FIELD* f) {
|
||||||
}
|
}
|
||||||
|
|
||||||
free(f -> balls);
|
free(f -> balls);
|
||||||
|
delwin(f -> win);
|
||||||
free(f);
|
free(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
game.c
1
game.c
|
@ -85,5 +85,4 @@ void play_game(GAME_STATE* state) {
|
||||||
timeout.tv_usec = 50000;
|
timeout.tv_usec = 50000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(f);
|
|
||||||
}
|
}
|
||||||
|
|
17
main.c
17
main.c
|
@ -4,7 +4,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "endscreen.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,16 +34,15 @@ int main(int argc, char** argv) {
|
||||||
cbreak();
|
cbreak();
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
|
|
||||||
GAME_STATE* state = malloc(sizeof(GAME_STATE));
|
while (1) {
|
||||||
|
GAME_STATE* state = malloc(sizeof(GAME_STATE));
|
||||||
play_game(state);
|
play_game(state);
|
||||||
|
display_endscreen(state -> points);
|
||||||
|
free(state);
|
||||||
|
}
|
||||||
endwin();
|
endwin();
|
||||||
|
//printf("GAME OVER! POINTS: %d\n", state -> points);
|
||||||
printf("GAME OVER! POINTS: %d\n", state -> points);
|
|
||||||
|
|
||||||
free(state);
|
free(state);
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue