From a52f64acfda905e229e1835a326064882054f14d Mon Sep 17 00:00:00 2001 From: p3ngu19z Date: Thu, 16 Feb 2023 11:23:46 +0200 Subject: [PATCH] Improve fall speed algorithm --- applications/plugins/tetris_game/tetris_game.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/applications/plugins/tetris_game/tetris_game.c b/applications/plugins/tetris_game/tetris_game.c index b317d376a..9b492cc8e 100644 --- a/applications/plugins/tetris_game/tetris_game.c +++ b/applications/plugins/tetris_game/tetris_game.c @@ -16,6 +16,9 @@ #define FIELD_WIDTH 11 #define FIELD_HEIGHT 24 +#define MAX_FALL_SPEED 500 +#define MIN_FALL_SPEED 100 + typedef struct Point { // Also used for offset data, which is sometimes negative int8_t x, y; @@ -169,7 +172,7 @@ static void tetris_game_input_callback(InputEvent* input_event, FuriMessageQueue static void tetris_game_init_state(TetrisState* tetris_state) { tetris_state->gameState = GameStatePlaying; tetris_state->numLines = 0; - tetris_state->fallSpeed = 500; + tetris_state->fallSpeed = MAX_FALL_SPEED; memset(tetris_state->playField, 0, sizeof(tetris_state->playField)); memcpy(&tetris_state->currPiece, &shapes[rand() % 7], sizeof(tetris_state->currPiece)); @@ -303,6 +306,7 @@ static void tetris_game_render_curr_piece(tetris_state); uint8_t numLines = 0; uint8_t lines[] = {0, 0, 0, 0}; + uint16_t nextFallSpeed; tetris_game_check_for_lines(tetris_state, lines, &numLines); if(numLines > 0) { @@ -323,7 +327,10 @@ static void uint16_t oldNumLines = tetris_state->numLines; tetris_state->numLines += numLines; if((oldNumLines / 10) % 10 != (tetris_state->numLines / 10) % 10) { - tetris_state->fallSpeed -= 50; + nextFallSpeed = tetris_state->fallSpeed - (100 / (tetris_state->numLines / 10)); + if (nextFallSpeed >= MIN_FALL_SPEED){ + tetris_state->fallSpeed = nextFallSpeed; + } } }