char *filename = NULL;
int maxnbrecords = 0;
+int maxnbturns = 50;
int savelen = 12;
+int turnspan = 400;
int height = 20;
int width = 40;
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[])
{
keypad (stdscr, TRUE);
curs_set (0);
start_color ();
+ halfdelay (1);
/* window positions (board) */
int xscore = (board->width - scorelen) / 2;
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 */
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 ();
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 */