From: Laurent Mazet Date: Sun, 1 Sep 2024 21:12:03 +0000 (+0200) Subject: change speed calculation X-Git-Tag: v1.0~10 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=caa7c2c06dfdafff0253014d1c1e2ae30a60ff8c;p=snake.git change speed calculation --- diff --git a/snake.c b/snake.c index 078a803..e6e2d19 100644 --- 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; }