#include #include #include #include "game.h" #include "player.h" #include "paddle.h" static void wait(struct game *g); static void input(struct game *g); static void update(struct game *g); static void draw(struct game *g); struct game * new_game(void) { struct game *g; g = malloc(sizeof(*g)); if (g == NULL) { return NULL; } g->players[0] = new_player(0); g->players[1] = new_player(1); return g; } void free_game(struct game *g) { free_player(g->players[0]); free_player(g->players[1]); free(g); } void run_game(struct game *g) { g->running = 1; while (g->running) { input(g); update(g); draw(g); wait(g); } } static void wait(struct game *g) { ++g; usleep(10000); } static void input(struct game *g) { int c; timeout(0); for (;;) { c = getch(); if (c == 'q') { g->running = 0; } else if (c == ERR) { break; } else if (c == KEY_LEFT) { g->players[0]->paddle_h.dir = -1; } else if (c == KEY_RIGHT) { g->players[0]->paddle_h.dir = 1; } else if (c == KEY_UP) { g->players[0]->paddle_v.dir = -1; } else if (c == KEY_DOWN) { g->players[0]->paddle_v.dir = 1; } } } static void update(struct game *g) { update_paddle(&g->players[0]->paddle_h); update_paddle(&g->players[0]->paddle_v); update_paddle(&g->players[1]->paddle_h); update_paddle(&g->players[1]->paddle_v); } static void draw(struct game *g) { draw_paddle(&g->players[0]->paddle_h); draw_paddle(&g->players[0]->paddle_v); draw_paddle(&g->players[1]->paddle_h); draw_paddle(&g->players[1]->paddle_v); }