pacman moves
authorLaurent Mazet <mazet@softndesign.org>
Mon, 14 Oct 2024 21:48:10 +0000 (23:48 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 14 Oct 2024 21:48:10 +0000 (23:48 +0200)
display.c
pacman.c
time.c
time.h

index 14e975a101dcf64b760b54fc8aaa8481e5fcc20a..1ea61986dc1be4aab2f94dc2229a7bbcc89668e1 100644 (file)
--- 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 '.':
index eb1508680cb5738c3ea0e0b9dcfc4ba2655f5774..f4d58488aedcdb76e1507b2286e811997dc86c92 100644 (file)
--- 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 ba0084ef8b3744dbc1311d3cd9ce85f645ad9d42..2ccb9f6e6dd9aa535c852b6a44e0a8e37b3fc15a 100644 (file)
--- a/time.c
+++ b/time.c
@@ -1,7 +1,42 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <time.h>
 #include <unistd.h>
 
+#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 bc21d7f60ba88a90102a989333ed734b37bf3be9..d991595fafab60c81b0541d771ee4a9142e6d601 100644 (file)
--- a/time.h
+++ b/time.h
@@ -1,6 +1,16 @@
 #ifndef __TIME_H__
 #define __TIME_H__
 
+#include <sys/time.h>
+
+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__ */