Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug in Tetris #381

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 40 additions & 45 deletions applications/plugins/tetris_game/tetris_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,59 +296,54 @@ static void
tetris_game_process_step(TetrisState* tetris_state, Piece* newPiece, bool wasDownMove) {
if(tetris_state->gameState == GameStateGameOver) return;

tetris_game_remove_curr_piece(tetris_state);

if(wasDownMove) {
if(tetris_game_piece_at_bottom(tetris_state, newPiece)) {
furi_timer_stop(tetris_state->timer);

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) {
for(int i = 0; i < numLines; i++) {
// zero out row
for(int j = 0; j < FIELD_WIDTH; j++) {
tetris_state->playField[lines[i]][j] = false;
}
// move all above rows down
for(int k = lines[i]; k >= 0; k--) {
for(int m = 0; m < FIELD_WIDTH; m++) {
tetris_state->playField[k][m] =
(k == 0) ? false : tetris_state->playField[k - 1][m];
}
}
if(wasDownMove && tetris_game_piece_at_bottom(tetris_state, newPiece)) {
furi_timer_stop(tetris_state->timer);

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) {
for(int i = 0; i < numLines; i++) {
// zero out row
for(int j = 0; j < FIELD_WIDTH; j++) {
tetris_state->playField[lines[i]][j] = false;
}

uint16_t oldNumLines = tetris_state->numLines;
tetris_state->numLines += numLines;
if((oldNumLines / 10) % 10 != (tetris_state->numLines / 10) % 10) {
nextFallSpeed =
tetris_state->fallSpeed - (100 / (tetris_state->numLines / 10));
if(nextFallSpeed >= MIN_FALL_SPEED) {
tetris_state->fallSpeed = nextFallSpeed;
// move all above rows down
for(int k = lines[i]; k >= 0; k--) {
for(int m = 0; m < FIELD_WIDTH; m++) {
tetris_state->playField[k][m] =
(k == 0) ? false : tetris_state->playField[k - 1][m];
}
}
}

// Check for game over
Piece* spawnedPiece = &shapes[rand() % 7];
if(!tetris_game_is_valid_pos(tetris_state, spawnedPiece->p)) {
tetris_state->gameState = GameStateGameOver;
} else {
memcpy(&tetris_state->currPiece, spawnedPiece, sizeof(tetris_state->currPiece));
furi_timer_start(tetris_state->timer, tetris_state->fallSpeed);
uint16_t oldNumLines = tetris_state->numLines;
tetris_state->numLines += numLines;
if((oldNumLines / 10) % 10 != (tetris_state->numLines / 10) % 10) {
nextFallSpeed =
tetris_state->fallSpeed - (100 / (tetris_state->numLines / 10));
if(nextFallSpeed >= MIN_FALL_SPEED) {
tetris_state->fallSpeed = nextFallSpeed;
}
}
}
}

if(tetris_game_is_valid_pos(tetris_state, newPiece->p)) {
memcpy(&tetris_state->currPiece, newPiece, sizeof(tetris_state->currPiece));
// Check for game over
Piece* spawnedPiece = &shapes[rand() % 7];
if(!tetris_game_is_valid_pos(tetris_state, spawnedPiece->p)) {
tetris_state->gameState = GameStateGameOver;
} else {
memcpy(&tetris_state->currPiece, spawnedPiece, sizeof(tetris_state->currPiece));
furi_timer_start(tetris_state->timer, tetris_state->fallSpeed);
}
}else{
tetris_game_remove_curr_piece(tetris_state);
if(tetris_game_is_valid_pos(tetris_state, newPiece->p)) {
memcpy(&tetris_state->currPiece, newPiece, sizeof(tetris_state->currPiece));
}
}

tetris_game_render_curr_piece(tetris_state);
}

Expand Down Expand Up @@ -480,4 +475,4 @@ int32_t tetris_game_app() {
free(tetris_state);

return 0;
}
}