From: Laurent Mazet Date: Mon, 14 Oct 2024 21:48:10 +0000 (+0200) Subject: pacman moves X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=3f6edee5b80dae235132cb89dad71bfb6dd53789;p=pacman.git pacman moves --- diff --git a/display.c b/display.c index 14e975a..1ea6198 100644 --- a/display.c +++ b/display.c @@ -108,7 +108,7 @@ void displayelement (board_t *board, int x, int y) element = 'C'; break; case 'C': - setcolor (red_black); + setcolor (lightred_black); element = 'C'; break; case 'o': @@ -116,7 +116,7 @@ void displayelement (board_t *board, int x, int y) element = 'O'; break; case 'O': - setcolor (red_black); + setcolor (lightred_black); element = 'O'; break; case '.': diff --git a/pacman.c b/pacman.c index eb15086..f4d5848 100644 --- a/pacman.c +++ b/pacman.c @@ -19,7 +19,9 @@ char *version = "0.1"; char *filename = NULL; int maxnbrecords = 0; +int maxnbturns = 50; int savelen = 12; +int turnspan = 400; int height = 20; int width = 40; @@ -49,6 +51,29 @@ int usage (int ret) return ret; } +/* get new position */ +void getnewposition (board_t *board, int dir, int x, int y, int *px, int *py) +{ + switch (dir) { + case 0: + *px = x; + *py = ((y > 0) ? y : board->height) - 1; + break; + case 1: + *px = ((x > 0) ? x : board->width) - 1; + *py = y; + break; + case 2: + *px = x; + *py = ((y < board->height - 1) ? y : -1) + 1; + break; + case 3: + *px = ((x < board->width - 1) ? x : -1) + 1; + *py = y; + break; + } +} + /* main function */ int main (int argc, char *argv[]) { @@ -135,6 +160,7 @@ int main (int argc, char *argv[]) keypad (stdscr, TRUE); curs_set (0); start_color (); + halfdelay (1); /* window positions (board) */ int xscore = (board->width - scorelen) / 2; @@ -159,6 +185,12 @@ int main (int argc, char *argv[]) int draw = 1; int mode = 0; int score = 0; + timeval_t turn = {0, 0}; + int delta = 0; + int nbturns = 0; + int dir = -1; + int ndir = -1; + int spin = 0; while (!stop) { /* draw board */ @@ -172,20 +204,78 @@ int main (int argc, char *argv[]) sprintf (msg, "score: % 5d", score); msgwindow (msg, xscore, yscore, scorelen); - /* move pacman */ - if ((nx != x) || (ny != y)) { - *getcell (board, x, y) = (mode == 0) ? 'o' : 'O'; + /* turn span */ + delta += toc (&turn); + tic (&turn); + if (delta > turnspan) { + delta -= turnspan; + + /* change mode */ + if ((mode) && (nbturns++ > maxnbturns)) { + mode = 0; + } + + /* move pacman */ + char *cell = NULL; + if ((ndir != -1) && (dir != ndir)) { + getnewposition (board, ndir, x, y, &nx, &ny); + cell = getcell (board, nx, ny); + if ((*cell != '*') && (*cell != '.') && (*cell != ' ')) { + nx = x; + ny = y; + } else { + dir = ndir; + } + } + if ((dir != -1) && (nx == x) && (ny == y)) { + getnewposition (board, dir, x, y, &nx, &ny); + cell = getcell (board, nx, ny); + if ((*cell != '*') && (*cell != '.') && (*cell != ' ')) { + nx = x; + ny = y; + } + } + + /* check new position */ + if (cell) { + switch (*cell) { + case '*': + mode = 1; + nbturns = 0; + /* fallthrough */ + case '.': + score++; + *cell = ' '; + /* fallthrough */ + case ' ': + break; + default: + nx = x; + ny = y; + } + } + + /* refresh board */ + if ((nx != x) || (ny != y)) { + *getcell (board, x, y) = ' '; + displayelement (board, x, y); + x = nx; + y = ny; + } + + /* pacman mouth wide open */ + *getcell (board, x, y) = (mode == 0) ? 'c' : 'C'; displayelement (board, x, y); refresh (); - msleep (150); - *getcell (board, x, y) = ' '; - displayelement (board, x, y); - x = nx; - y = ny; - *getcell (board, x, y) = (mode == 0) ? 'c' : 'C'; + spin = 0; + + } else if ((delta > turnspan / 2) && (spin == 0)) { + + /* pacman mouth close */ + *getcell (board, x, y) = (mode == 0) ? 'o' : 'O'; displayelement (board, x, y); refresh (); - msleep (150); + spin = 1; } int ch = getch (); @@ -228,37 +318,21 @@ int main (int argc, char *argv[]) switch (ch) { case KEY_UP: case 'i': - ny = ((y > 0) ? y : board->height) - 1; + ndir = 0; break; case KEY_LEFT: case 'j': - nx = ((x > 0) ? x : board->width) - 1; + ndir = 1; break; case KEY_DOWN: case 'k': - ny = ((y < board->height - 1) ? y : -1) + 1; + ndir = 2; break; case KEY_RIGHT: case 'l': - nx = ((x < board->width - 1) ? x : -1) + 1; + ndir = 3; break; } - - /* check new position */ - char *cell = getcell (board, nx, ny); - switch (*cell) { - case '*': - mode = 1; - /* fallthrough */ - case '.': - score++; - /* fallthrough */ - case ' ': - break; - default: - nx = x; - ny = y; - } } /* cleaning before quiting */ diff --git a/time.c b/time.c index ba0084e..2ccb9f6 100644 --- a/time.c +++ b/time.c @@ -1,7 +1,42 @@ +#include +#include +#include +#include #include +#include "debug.h" #include "time.h" +int newseed (int seed) +{ + if (seed == 0) { + seed = time (NULL); + } + srand (seed); + return seed; +} + +void tic (timeval_t *t) +{ + gettimeofday (t, NULL); +} + +int toc (timeval_t *t0) +{ + timeval_t _t = {0, 0}; + timeval_t *t = &_t; + + gettimeofday (t, NULL); + + int delta = 0; + if (t0->tv_sec != 0) { + delta += (t->tv_sec - t0->tv_sec) * 1000; + delta += (t->tv_usec - t0->tv_usec) / 1000; + //assert (delta > 0); + } + return delta; +} + void msleep (int msec) { usleep (msec * 1000); diff --git a/time.h b/time.h index bc21d7f..d991595 100644 --- a/time.h +++ b/time.h @@ -1,6 +1,16 @@ #ifndef __TIME_H__ #define __TIME_H__ +#include + +typedef struct timeval timeval_t; + +int newseed (int seed); + +void tic (timeval_t *t); + +int toc (timeval_t *t0); + void msleep (int msec); #endif /* __TIME_H__ */