From 62624e630b66a3fe7e3315d23159cca954e7f212 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sun, 3 Mar 2019 16:05:17 -0500 Subject: add line clear delay --- cetris.c | 25 +++++++++++++++---------- cetris.h | 2 ++ ncurses_ui.c | 15 ++++++++++----- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/cetris.c b/cetris.c index 3b4bfd6..13cb3c5 100644 --- a/cetris.c +++ b/cetris.c @@ -297,28 +297,33 @@ void set_constants(struct cetris_game* g) { } } +void reset_slot(slot* s) { + s->occupied = 0; + s->constant = 0; + s->remove_tick = 0; +} + void wipe_board(struct cetris_game* g) { int lines_cleared = 0; for (int y = 0; y < BOARD_Y; y++) { int clear_line = 1; for (int x = 0; x < BOARD_X; x++) { - if (!g->board[x][y].constant) g->board[x][y].occupied = 0; - if (!g->board[x][y].occupied) clear_line = 0; + if (!g->board[x][y].constant) reset_slot(&g->board[x][y]); + if (g->board[x][y].remove_tick && g->board[x][y].remove_tick <= g->tick) { + reset_slot(&g->board[x][y]); + for (int s = y - 1; s >= 0; s--) { + g->board[x][s + 1] = g->board[x][s]; + } + } + if (!g->board[x][y].occupied || g->board[x][y].remove_tick > 0) clear_line = 0; } if (clear_line) { lines_cleared++; for (int x = 0; x < BOARD_X; x++) { - g->board[x][y].occupied = 0; - g->board[x][y].constant = 0; - } - - for (int s = y - 1; s >= 0; s--) { - for (int x = 0; x < BOARD_X; x++) { - g->board[x][s + 1] = g->board[x][s]; - } + g->board[x][y].remove_tick = g->tick + CETRIS_LINE_CLEAR_DELAY; } } } diff --git a/cetris.h b/cetris.h index 2f7631b..926227f 100644 --- a/cetris.h +++ b/cetris.h @@ -4,6 +4,7 @@ #define CETRIS_HZ 60 #define CETRIS_DAS_DELAY 11 #define CETRIS_DAS_PERIOD 5 +#define CETRIS_LINE_CLEAR_DELAY 40 typedef struct { int x; @@ -25,6 +26,7 @@ struct tetrimino { typedef struct { int occupied; int constant; + int remove_tick; } slot; enum movements { diff --git a/ncurses_ui.c b/ncurses_ui.c index 2a8a2b3..0f5af4e 100644 --- a/ncurses_ui.c +++ b/ncurses_ui.c @@ -37,9 +37,7 @@ void curses_init() { noecho(); keypad(stdscr, TRUE); curs_set(0); - timeout(1000 / 60); - - //resize_term(20, 50); + timeout(1000 / CETRIS_HZ); start_color(); init_pair(0, COLOR_MAGENTA, COLOR_BLACK); @@ -53,8 +51,15 @@ void draw_board(struct cetris_game* g) { mvaddstr(0, 0, PLAY_FIELD_STR); for (int x = 0; x < BOARD_X; x++) { for (int y = 0; y < BOARD_Y; y++) { - if (g->board[x][y].occupied) - mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK); + if (g->board[x][y].occupied) { + if (g->board[x][y].remove_tick > 0) { + if (g->tick % 2 == 0) { + mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK); + } + } else { + mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK); + } + } } } } -- cgit v1.2.3-101-g0448