score and next windows
authorLaurent Mazet <mazet@softndesign.org>
Tue, 23 Jul 2024 22:01:45 +0000 (00:01 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 23 Jul 2024 22:01:45 +0000 (00:01 +0200)
display.c
display.h
function.c
function.h
tetris.c

index b1c047074f9a3f26a5cd7b479382c44a9fddad19..c725ebf32a87a324f3ed90f7e81ec0cceaf75d58 100644 (file)
--- a/display.c
+++ b/display.c
@@ -218,6 +218,51 @@ void boardwindow (board_t *board)
     }
 }
 
+void scorewindow (int xoffset, int yoffset, int length, int score, int level)
+{
+    set_color (black);
+    _dobound (length + 7, 2, xoffset, yoffset);
+    set_color (white);
+
+    char fmt[12], msg[32];
+    sprintf (fmt, "%% %dd", length);
+
+    sprintf (msg, "score: ");
+    sprintf (msg + strlen (msg), fmt, score);
+    mvaddstr (yoffset, xoffset, msg);
+
+    sprintf (msg, "level: % 3d", level);
+    mvaddstr (yoffset + 1, xoffset, msg);
+}
+
+void nextwindow (block_t *blocks, int nb, int xoffset, int yoffset, int next)
+{
+    int height = maxblockheight (blocks, nb);
+    int width = maxblockwidth (blocks, nb) + 2;
+
+    set_color (black);
+    _dobound (width, height, xoffset, yoffset);
+    set_color (white);
+
+    int i, j;
+    for (i = 0; i < width; i++) {
+        for (j = 0; j < height; j++) {
+            mvaddch (yoffset + j, xoffset + i, ' ');
+        }
+    }
+
+    block_t *block = blocks + next;
+    for (i = 0; i < block->width; i++) {
+        for (j = 0; j < block->height; j++) {
+            if (*getcell (block, i, j) == '.') {
+                _set_symb_color ('0' + block->color);
+                mvaddch (yoffset + (height - block->height) / 2 + j, xoffset + (width - block->width) / 2 + i, ' ');
+                set_color (white);
+            }
+        }
+    }
+}
+
 void displayblock (board_t *board, block_t *block, int x, int y)
 {
     int i, j;
index d04169d18196890ca593f008575875afce9fdd02..a26ea1fc2cec5e429318a0286d91c419fc9b0a55 100644 (file)
--- a/display.h
+++ b/display.h
@@ -10,6 +10,10 @@ int helpwindow (char *msg, int xoffset, int yoffset);
 
 void boardwindow (board_t *board);
 
+void scorewindow (int xoffset, int yoffset, int length, int score, int level);
+
+void nextwindow (block_t *blocks, int nb, int xoffset, int yoffset, int next);
+
 void displayblock (board_t *board, block_t *block, int x, int y);
 
 char *savewindow (int length, int xoffset, int yoffset);
index 2e6429dabfc815c6c5d37750c8025873a682a4b0..01de6c62dd6efdb587c2d23e7ccbe84c4757af8e 100644 (file)
@@ -253,6 +253,30 @@ block_t *copyblock (block_t *block)
     return newblock;
 }
 
+int maxblockheight (block_t *blocks, int nb)
+{
+    int i;
+    int height = 0;
+    for (i = 0; i < nb; i++) {
+        if (height < (blocks + i)->height) {
+            height = (blocks + i)->height;
+        }
+    }
+    return height;
+}
+
+int maxblockwidth (block_t *blocks, int nb)
+{
+    int i;
+    int width = 0;
+    for (i = 0; i < nb; i++) {
+        if (width < (blocks + i)->width) {
+            width = (blocks + i)->width;
+        }
+    }
+    return width;
+}
+
 void freeblock (block_t *block)
 {
     if (block) {
index 2cd7c8c8aefdcdc9c2797f718be1d3c77fbbd1bf..9bf01a091375994f7e4c9693be6990cdfce923db 100644 (file)
@@ -45,6 +45,10 @@ block_t *changeblock (block_t *dest, block_t *src);
 
 block_t *copyblock (block_t *block);
 
+int maxblockheight (block_t *blocks, int nb);
+
+int maxblockwidth (block_t *blocks, int nb);
+
 void freeblock (block_t *block);
 
 block_t *rotateblock (block_t *block, int rot);
index 4eda0a2bce60f7b2c9c3a6cabbdaf0dcf713d112..2ed142924ac1489e4a25d9b1a892479618aabb9f 100644 (file)
--- a/tetris.c
+++ b/tetris.c
@@ -27,6 +27,9 @@ int scale = 1;
 int height = 20;
 int width = 9;
 
+int nbdigit = 5;
+int maxscor = 100000;
+
 int savelen = 12;
 int xoffset = 1;
 int yoffset = 1;
@@ -169,8 +172,12 @@ int main (int argc, char *argv[])
     /* window positions */
     int xboard = board->xoffset = xoffset + 1;
     int yboard = board->yoffset = xoffset + 1;
-    int xhelp = xboard + xoffset + 1 + board->xsize;
-    int yhelp = yboard - 1;
+    int xscore = xboard + xoffset + 2 + board->xsize;
+    int yscore = yboard;
+    int xnext = xscore;
+    int ynext = yscore + xoffset + maxblockheight (blocks, nb_blocks);
+    int xhelp = xnext - 1;
+    int yhelp = ynext + 1 + maxblockheight (blocks, nb_blocks) + xoffset;
     int xsave = max (xboard + (board->xsize - savelen) / 2, 1);
     int ysave = yboard + (board->ysize - 1) / 2;
     char *savename = NULL;
@@ -180,13 +187,15 @@ int main (int argc, char *argv[])
 
     /* window positions */
     int xmsg = xboard;
-    int ymsg = max (yboard + xoffset + 1 + board->ysize, yhelp + lhelp + yoffset + 1);
+    int ymsg = max (yboard + xoffset + 1 + board->ysize, yhelp + lhelp + yoffset);
     int lmsg = xhelp - xmsg + strmaxlen (help, '\n');
 
     /* event loop */
+    int level = 1;
     int mode = 0;
-    int speed = 5000;
+    int score = 0;
     int settle = 0;
+    int speed = 5000;
     int stop = 0;
     struct timeval tend = {0, 0};
     while (!stop) {
@@ -195,6 +204,7 @@ int main (int argc, char *argv[])
         switch (mode) {
         case 0:
             halfdelay (0);
+            nextwindow (blocks, nb_blocks, xnext, ynext, current);
             sprintf (msg, "Get ready player One");
             break;
         case 1:
@@ -222,6 +232,7 @@ int main (int argc, char *argv[])
                 mode = 2;
                 sprintf (msg, "End of game");
             }
+            nextwindow (blocks, nb_blocks, xnext, ynext, next);
             break;
         case 2:
             freeblock (cblock);
@@ -231,6 +242,7 @@ int main (int argc, char *argv[])
 
         msgwindow (msg, xmsg, ymsg, lmsg);
         boardwindow (board);
+        scorewindow (xscore, yscore, nbdigit, score, level);
         if (mode == 1) {
             displayblock (board, cblock, xblock, yblock);
         }