better speed managment
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 2 Sep 2024 16:49:21 +0000 (18:49 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 2 Sep 2024 16:49:21 +0000 (18:49 +0200)
snake.c

diff --git a/snake.c b/snake.c
index e6e2d19e707082e4614b09eb1012abd66dbb1be6..99b5331063d0c7714c86e1b7a4b5c2d0b461f8f5 100644 (file)
--- a/snake.c
+++ b/snake.c
@@ -23,6 +23,7 @@ char *filename = NULL;
 int scale = 1;
 int seed = 0;
 int tic = 80;
+int maxspeed = 8;
 int speed = 8;
 int turnlimit = 100;
 int malus = 10;
@@ -163,7 +164,7 @@ int main (int argc, char *argv[])
     int yboard = board->yoffset = yoffset + 1;
     int xscore = xboard + xoffset + 2 + board->xsize;
     int yscore = yboard;
-    int xhelp = xscore;
+    int xhelp = xscore - 1;
     int yhelp = yscore + 3 + xoffset;
     int xsave = max (xboard + (board->xsize - savelen) / 2, 1);
     int ysave = yboard + (board->ysize - 1) / 2;
@@ -172,7 +173,12 @@ int main (int argc, char *argv[])
     VERBOSE (DEBUG, printf ("width: %d\nheight: %d\n", board->width, board->height));
 
     /* help window */
-    helpwindow (help, xhelp, yhelp);
+    int lhelp = helpwindow (help, xhelp, yhelp);
+
+    /* window positions (msg) */
+    int xmsg = xhelp + 1;
+    int ymsg = yhelp + lhelp + xoffset;
+    int lmsg = strmaxlen (help, '\n') - 2;
 
     /* main loop */
     int nonewpoint = 1;
@@ -195,7 +201,7 @@ int main (int argc, char *argv[])
                 case 1:
                     score++;
                     nonewpoint = 0;
-                    speed = (speed < 10) ? speed + 1 : speed;
+                    speed = (speed < maxspeed) ? speed + 1 : speed;
                     break;
                 }
 
@@ -226,6 +232,21 @@ int main (int argc, char *argv[])
         scorewindow (xscore, yscore, whelp - 2, score, (duration * tic) / 1000);
         setcolor (gray_black);
 
+        /* message */
+        char msg[128] = {0};
+        switch (mode) {
+        case 0:
+            msgwindow ("Press <p> to start", xmsg, ymsg, lmsg);
+            break;
+        case 1:
+            sprintf (msg, "Speed %d", maxspeed - speed + 1);
+            msgwindow (msg, xmsg, ymsg, lmsg);
+            break;
+        case 2:
+            msgwindow ("End of game", xmsg, ymsg, lmsg);
+            break;
+        }
+
         int ch = getch ();
 
         /* main controls */