change speed calculation
authorLaurent Mazet <mazet@softndesign.org>
Sun, 1 Sep 2024 21:12:03 +0000 (23:12 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 1 Sep 2024 21:12:03 +0000 (23:12 +0200)
snake.c

diff --git a/snake.c b/snake.c
index 078a803d9ef581ded3bd06c7a3b26ed60b12ef64..e6e2d19e707082e4614b09eb1012abd66dbb1be6 100644 (file)
--- a/snake.c
+++ b/snake.c
@@ -20,10 +20,11 @@ char *progname = NULL;
 char *version = "0.1";
 
 char *filename = NULL;
-int ratio = 10;
 int scale = 1;
 int seed = 0;
-int speed = 500;
+int tic = 80;
+int speed = 8;
+int turnlimit = 100;
 int malus = 10;
 int bonus = 5;
 
@@ -174,17 +175,18 @@ int main (int argc, char *argv[])
     helpwindow (help, xhelp, yhelp);
 
     /* main loop */
+    int nonewpoint = 1;
     int stop = 0;
     int mode = 0;
     timeval_t turn = {0, 0};
-    int nbturns = 0;
     while (!stop) {
 
         if (mode == 1) {
             if (isovertime (&turn)) {
-                setendtime (&turn, speed);
-                nbturns++;
+                setendtime (&turn, tic);
                 duration++;
+            }
+            if ((duration) && (duration % speed == 0)) {
                 int ret = movesnake (board, snake, snake->dir);
                 switch (ret) {
                 case -1:
@@ -192,13 +194,18 @@ int main (int argc, char *argv[])
                     break;
                 case 1:
                     score++;
+                    nonewpoint = 0;
+                    speed = (speed < 10) ? speed + 1 : speed;
                     break;
                 }
 
             }
-            if (nbturns == ratio) {
+            if ((duration) && (duration % turnlimit == 0)) {
                 drawbonus (board, snake, malus, bonus);
-                nbturns = 0;
+                if (nonewpoint) {
+                    speed = (speed > 1) ? speed - 1 : speed;
+                }
+                nonewpoint = 1;
             }
         }
 
@@ -216,7 +223,7 @@ int main (int argc, char *argv[])
             setcolor (red_black);
             break;
         }
-        scorewindow (xscore, yscore, whelp - 2, score, (duration * speed) / 1000);
+        scorewindow (xscore, yscore, whelp - 2, score, (duration * tic) / 1000);
         setcolor (gray_black);
 
         int ch = getch ();
@@ -227,7 +234,7 @@ int main (int argc, char *argv[])
         case 'p':
             if (mode == 0) {
                 mode = 1;
-                setendtime (&turn, speed);
+                setendtime (&turn, tic);
             } else if (mode == 1) {
                 mode = 0;
             }