FlappyBird/main.c

77 lines
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <time.h>
#include "game.h"
#include "config.h"
void msleep(int msec){
struct timespec ts;
ts.tv_sec = msec/1000;
ts.tv_nsec = (msec%1000) * 1000000;
nanosleep(&ts, &ts);
}
int main(int argc, char** argv){
/* Init curses */
WINDOW* win;
win = initscr();
noecho();
keypad(win, TRUE);
nodelay(win, TRUE);
curs_set(FALSE);
/* Init game */
srand(time(NULL));
game* g = game_create();
/* Game Loop */
char c;
while(1){
/* Get input */
do{
c = getch();
if(c == KEY_QUIT) break;
if(c != ERR) game_key_pressed(g, c);
}while(c != ERR);
if(c == KEY_QUIT) break;
/* Update */
int state = game_update(g, 1.0F/(float)FPS);
/* Render */
game_render(g);
refresh();
/* Exit if collision */
if(state == STATE_COLLISION){
/* Update score */
char is_best_score = 0;
if(g->score > g->highscore){
g->highscore = g->score;
is_best_score = 1;
}
/* Draw message */
move(HEIGHT+2, 1);
addstr((is_best_score ? "New Highscore!" : "Game Over!"));
refresh();
/* Delay and restart */
msleep(1000);
game_reset_level(g);
}
/* Delay */
msleep(1000/FPS);
}
/* Cleanup */
game_destroy(g);
delwin(win);
endwin();
refresh();
return 0;
}