diff options
author | P. J. McDermott <pjm@nac.net> | 2013-05-20 16:39:40 (EDT) |
---|---|---|
committer | P. J. McDermott <pjm@nac.net> | 2013-05-20 16:41:22 (EDT) |
commit | 090b6d62acbe03c139f4dcb96d86402c03d25fc3 (patch) | |
tree | 14e731fbb8c094fbbc76468f3a2b7b014f6841ed /src | |
parent | 1f4b8a236c39ae2fc0bc702943baf4c3f6953607 (diff) | |
download | cursespong-090b6d62acbe03c139f4dcb96d86402c03d25fc3.zip cursespong-090b6d62acbe03c139f4dcb96d86402c03d25fc3.tar.gz cursespong-090b6d62acbe03c139f4dcb96d86402c03d25fc3.tar.bz2 |
Start implementing game loop.
Diffstat (limited to 'src')
-rw-r--r-- | src/game.c | 51 | ||||
-rw-r--r-- | src/game.h | 3 | ||||
-rw-r--r-- | src/main.c | 4 |
3 files changed, 54 insertions, 4 deletions
@@ -5,6 +5,9 @@ #include "player.h" static void draw_paddles(struct game *g); +static void wait(struct game *g); +static void input(struct game *g); +static void draw(struct game *g); struct game * new_game(void) @@ -32,7 +35,53 @@ free_game(struct game *g) } void -draw_game(struct game *g) +run_game(struct game *g) +{ + g->running = 1; + + while (g->running) { + wait(g); + input(g); + draw(g); + } +} + +static void +wait(struct game *g) +{ + g->running = 1; +} + +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.pos -= + g->players[0]->paddle_h.speed; + } else if (c == KEY_RIGHT) { + g->players[0]->paddle_h.pos += + g->players[0]->paddle_h.speed; + } else if (c == KEY_UP) { + g->players[0]->paddle_v.pos -= + g->players[0]->paddle_v.speed; + } else if (c == KEY_DOWN) { + g->players[0]->paddle_v.pos += + g->players[0]->paddle_v.speed; + } + } +} + +static void +draw(struct game *g) { draw_paddles(g); } @@ -5,10 +5,11 @@ struct game { struct player *players[2]; + char running; }; struct game *new_game(void); void free_game(struct game *g); -void draw_game(struct game *g); +void run_game(struct game *g); #endif @@ -13,8 +13,7 @@ main(void) init_curses(); g = new_game(); - draw_game(g); - getch(); + run_game(g); free_game(g); fini_curses(); @@ -28,6 +27,7 @@ init_curses(void) initscr(); noecho(); raw(); + keypad(stdscr, TRUE); curs_set(0); } |