From 1146d2787b0bb7e0761c55a8f3e8e14ca2a97395 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 4 Mar 2019 19:55:43 -0500 Subject: improve responsivness, style ncurses frontend more --- cetris.c | 36 ++++++++++++++++++------------------ frontend/ncurses/ncurses_ui.c | 28 ++++++++++++++++++++++------ test.c | 30 ++++++++++++++++++++++++++++-- test.h | 3 ++- 4 files changed, 70 insertions(+), 27 deletions(-) diff --git a/cetris.c b/cetris.c index adba440..875b8ab 100644 --- a/cetris.c +++ b/cetris.c @@ -300,7 +300,7 @@ void init_game(struct cetris_game* g) { memset(g->board, 0, sizeof(slot) * BOARD_X * BOARD_Y); #ifdef BUILD_TESTS - apply_test_board(g, TSPIN); + apply_test_board(g, TSPIN_NO_LINES); #endif g->tick = 0; @@ -427,13 +427,13 @@ void next_piece(struct cetris_game* g) { set_constants(g); + g->current = g->piece_queue[g->current_index]; + g->current_index++; + if (g->current_index >= 7) { g->current_index = 0; shuffle_queue(g); } - - g->current = g->piece_queue[g->current_index]; - g->current_index++; } void set_constants(struct cetris_game* g) { @@ -486,9 +486,11 @@ void overlay_current_matrix(struct cetris_game* g) { void wipe_board(struct cetris_game* g, int did_move) { for (int y = 0; y < BOARD_Y; y++) { for (int x = 0; x < BOARD_X; x++) { + if (!g->board[x][y].constant) { memset(&g->board[x][y], 0, sizeof(slot)); } + if (g->board[x][y].remove_tick && g->board[x][y].remove_tick <= g->tick) { memset(&g->board[x][y], 0, sizeof(slot)); for (int s = y - 1; s >= 0; s--) { @@ -499,20 +501,18 @@ void wipe_board(struct cetris_game* g, int did_move) { } int lines_cleared = 0; - if (did_move) { - for (int y = 0; y < BOARD_Y; y++) { - int clear_line = 1; + 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].occupied || + g->board[x][y].remove_tick > 0 || + !g->board[x][y].constant) + clear_line = 0; + } + if (clear_line) { + lines_cleared++; for (int x = 0; x < BOARD_X; x++) { - if (!g->board[x][y].occupied || - g->board[x][y].remove_tick > 0 || - !g->board[x][y].constant) - clear_line = 0; - } - if (clear_line) { - lines_cleared++; - for (int x = 0; x < BOARD_X; x++) { - g->board[x][y].remove_tick = g->tick + CETRIS_LINE_CLEAR_DELAY; - } + g->board[x][y].remove_tick = g->tick + CETRIS_LINE_CLEAR_DELAY; } } } @@ -520,7 +520,7 @@ void wipe_board(struct cetris_game* g, int did_move) { overlay_current_matrix(g); assert(lines_cleared <= 4); - if (did_move) { + if (did_move || lines_cleared > 0) { add_score(g, lines_cleared); if (lines_cleared > 0) { g->lines += lines_cleared; diff --git a/frontend/ncurses/ncurses_ui.c b/frontend/ncurses/ncurses_ui.c index 71ef49f..3810cb8 100644 --- a/frontend/ncurses/ncurses_ui.c +++ b/frontend/ncurses/ncurses_ui.c @@ -12,12 +12,12 @@ " | | \n"\ " | | \n"\ " | | \n"\ - " | | \n"\ - " | | \n"\ - " | | \n"\ - " | | \n"\ - " | | \n"\ - " | | \n"\ + " | | /---------\\ \n"\ + " | | | | \n"\ + " | | | | \n"\ + " | | | | \n"\ + " | | | | \n"\ + " | | \\---------/ \n"\ " | | \n"\ " | | \n"\ " | | \n"\ @@ -68,9 +68,25 @@ void draw_board(struct cetris_game* g) { attroff(COLOR_PAIR(g->board[x][y].c)); } } + char score[50]; sprintf(score, "%li", g->score); mvaddstr(1, (41 + X_OFFSET) - strlen(score), score); + + int index = g->current_index; + attron(COLOR_PAIR(g->piece_queue[index].c)); + for (int x = 0; x < 4; x++) { + for (int y = 0; y < 4; y++) { + if (g->piece_queue[index].mat[y][x]) { + mvaddstr(7 + y, (x * 2) + 38, BLOCK); + } + } + } + attroff(COLOR_PAIR(g->piece_queue[index].c)); + + char level[20]; + sprintf(level, "Level: %i", g->level); + mvaddstr(4, 36, level); } } diff --git a/test.c b/test.c index c5465c6..e143e7d 100644 --- a/test.c +++ b/test.c @@ -1,6 +1,6 @@ #include "test.h" -int tspin_test_board[20][10] = { +int tspin_board[20][10] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, @@ -23,11 +23,37 @@ int tspin_test_board[20][10] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 } }; +int tspin_no_lines_board[20][10] = { + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 1, 1, 1, 0, 0, 1, 1, 1, 0 }, + { 1, 1, 1, 0, 0, 0, 1, 1, 1, 0 }, + { 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 }, + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 } +}; + void apply_test_board(struct cetris_game* g, enum tests t) { int (*board)[20][10]; switch (t) { case TSPIN: - board = &tspin_test_board; + board = &tspin_board; + break; + case TSPIN_NO_LINES: + board = &tspin_no_lines_board; break; default: return; diff --git a/test.h b/test.h index c4ed20e..7860bc3 100644 --- a/test.h +++ b/test.h @@ -1,7 +1,8 @@ #include "cetris.h" enum tests { - TSPIN + TSPIN, + TSPIN_NO_LINES }; void apply_test_board(struct cetris_game* g, enum tests t); -- cgit v1.2.3-101-g0448