From 284064de8e1dd440829a53d0c3e875ba8215ac84 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 22 Jul 2024 23:45:42 +0200 Subject: [PATCH] blocks move! --- function.c | 3 +++ tetris.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/function.c b/function.c index ea76a93..7e9c160 100644 --- a/function.c +++ b/function.c @@ -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); diff --git a/tetris.c b/tetris.c index a1e331c..37383cc 100644 --- a/tetris.c +++ b/tetris.c @@ -9,6 +9,7 @@ #include #include #include +#include #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': -- 2.30.2