blocks move!
authorLaurent Mazet <mazet@softndesign.org>
Mon, 22 Jul 2024 21:45:42 +0000 (23:45 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 22 Jul 2024 21:55:40 +0000 (23:55 +0200)
function.c
tetris.c

index ea76a938cd6e66188d5f883c3e4a41f51ba5b864..7e9c16052455ddd8082dd96ce016fccb600f322a 100644 (file)
@@ -275,6 +275,7 @@ block_t *rotateblock (block_t *block, int rot)
         break;
     case 1:
         newblock = initblock (block->height, block->width);
+        newblock->color = block->color;
         for (i = 0; i < block->width; i++) {
             for (j = 0; j < block->height; j++) {
                 *getcell (newblock, block->height - 1 - j, i) = *getcell (block, i, j);
@@ -283,6 +284,7 @@ block_t *rotateblock (block_t *block, int rot)
         break;
     case 2:
         newblock = initblock (block->width, block->height);
+        newblock->color = block->color;
         for (i = 0; i < block->width; i++) {
             for (j = 0; j < block->height; j++) {
                 *getcell (newblock, block->width - 1 - i, block->height - 1 - j) = *getcell (block, i, j);
@@ -291,6 +293,7 @@ block_t *rotateblock (block_t *block, int rot)
         break;
     case 3:
         newblock = initblock (block->height, block->width);
+        newblock->color = block->color;
         for (i = 0; i < block->width; i++) {
             for (j = 0; j < block->height; j++) {
                 *getcell (newblock, j, block->width - 1 - i) = *getcell (block, i, j);
index a1e331cadf85c3e54c74218591ee5cd0db0b109e..37383cc516303ed495627b0d68dbce68c46494e8 100644 (file)
--- a/tetris.c
+++ b/tetris.c
@@ -9,6 +9,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <sys/time.h>
 
 #include "constant.h"
 #include "debug.h"
@@ -54,6 +55,22 @@ int usage (int ret)
     return ret;
 }
 
+void setendtime (struct timeval *t, int s)
+{
+    gettimeofday (t, NULL);
+    t->tv_usec += s * 1000l;
+    t->tv_sec += t->tv_usec / 1000000l;
+    t->tv_usec = t->tv_usec % 1000000l;
+}
+
+int isovertime (struct timeval *t)
+{
+    struct timeval _c;
+    struct timeval *c = &_c;
+    gettimeofday (c, NULL);
+    return ((t->tv_sec < c->tv_sec) || ((t->tv_sec == c->tv_sec) && ( t->tv_usec < c->tv_usec)));
+}
+
 /* main function */
 int main (int argc, char *argv[])
 {
@@ -170,9 +187,10 @@ int main (int argc, char *argv[])
 
     /* event loop */
     int mode = 0;
-    int speed = 255;
+    int speed = 5000;
     int rot = 0;
     int stop = 0;
+    struct timeval tend = {0, 0};
     while (!stop) {
 
         boardwindow (board);
@@ -191,10 +209,23 @@ sprintf (msg, "%d/%d: (%d, %d)\n", current, nb_blocks, xblock, yblock);
                 //sprintf (msg, "Get ready player One");
                 break;
             case 1:
-                halfdelay (speed);
+                halfdelay (10);
                 rotateblock (cblock, rot);
+                if (isovertime (&tend)) {
+                    yblock++;
+                    if (yblock + cblock->height >= board->height) {
+                        freeblock (cblock);
+                        current = next;
+                        cblock = copyblock (blocks + current);
+                        xblock = (board->width - cblock->width) / 2;
+                        yblock = 0;
+                        next = rand () % nb_blocks;
+                    }
+                    setendtime (&tend, speed);
+                }
                 displayblock (board, cblock, xblock, yblock);
                 rot = 0;
+sprintf (msg, "%ld/%06ld\n", tend.tv_sec, tend.tv_usec);
                 break;
             }
             msgwindow (msg, xmsg, ymsg, lmsg);
@@ -208,15 +239,19 @@ sprintf (msg, "%d/%d: (%d, %d)\n", current, nb_blocks, xblock, yblock);
         case '\n':
         case '\r':
         case 'c':
+            yblock = board->height - cblock->height;
             break;
         case KEY_LEFT:
         case 'j':
+            xblock--;
             break;
         case KEY_DOWN:
         case 'k':
+            yblock++;
             break;
         case KEY_RIGHT:
         case 'l':
+            xblock++;
             break;
         case KEY_UP:
         case ' ':
@@ -226,6 +261,9 @@ sprintf (msg, "%d/%d: (%d, %d)\n", current, nb_blocks, xblock, yblock);
             break;
         case 'p':
             mode = (mode == 0) ? 1 : 0; 
+            if (mode == 1) {
+                setendtime (&tend, speed);
+            }
             break;
         case KEY_ESC:
         case 'q':