Snake Plugin: put state game over handeling into function

This commit is contained in:
JuanJakobo
2022-10-26 19:31:12 +02:00
parent 8304605551
commit b97affa562
+16 -10
View File
@@ -87,7 +87,7 @@ static void snake_game_render_callback(Canvas* const canvas, void* ctx) {
canvas_set_font(canvas, FontSecondary);
char buffer[18];
snprintf(buffer, sizeof(buffer), "Score: %u", snake_state->len - 7);
snprintf(buffer, sizeof(buffer), "Score: %u", snake_state->len);
canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignBottom, buffer);
snprintf(buffer, sizeof(buffer), "Highscore: %d", snake_state->highscore);
@@ -222,6 +222,18 @@ static void snake_game_move_snake(SnakeState* const snake_state, Point const nex
snake_state->points[0] = next_step;
}
static void snake_game_game_over(SnakeState* const snake_state, NotificationApp* notification) {
snake_state->state = GameStateGameOver;
snake_state->len = snake_state->len -7;
if(snake_state->len > snake_state->highscore){
snake_state->isNewHighscore = true;
snake_state->highscore = snake_state->len;
}
notification_message_block(notification, &sequence_fail);
}
static void
snake_game_process_game_step(SnakeState* const snake_state, NotificationApp* notification) {
if(snake_state->state == GameStateGameOver) {
@@ -241,9 +253,7 @@ static void
snake_state->state = GameStateLastChance;
return;
} else if(snake_state->state == GameStateLastChance) {
snake_state->state = GameStateGameOver;
snake_state->highscore = snake_game_save_score_to_file(snake_state->len);
notification_message_block(notification, &sequence_fail);
snake_game_game_over(snake_state, notification);
return;
}
} else {
@@ -254,9 +264,7 @@ static void
crush = snake_game_collision_with_tail(snake_state, next_step);
if(crush) {
snake_state->state = GameStateGameOver;
snake_state->highscore = snake_game_save_score_to_file(snake_state->len);
notification_message_block(notification, &sequence_fail);
snake_game_game_over(snake_state, notification);
return;
}
@@ -264,9 +272,7 @@ static void
if(eatFruit) {
snake_state->len++;
if(snake_state->len >= MAX_SNAKE_LEN) {
snake_state->state = GameStateGameOver;
snake_state->highscore = snake_game_save_score_to_file(snake_state->len);
notification_message_block(notification, &sequence_fail);
snake_game_game_over(snake_state, notification);
return;
}
}