mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-07-03 22:23:35 -07:00
Snake Plugin: Add highscore info
This commit is contained in:
@@ -33,6 +33,32 @@ static FlipperFormat* snake_game_open_file(Storage* storage) {
|
||||
return file;
|
||||
}
|
||||
|
||||
int16_t snake_game_save_score_to_file(int32_t len) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperFormat* file = snake_game_open_file(storage);
|
||||
|
||||
len = len - 7;
|
||||
int32_t temp;
|
||||
if(!flipper_format_read_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)){
|
||||
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
|
||||
snake_game_close_file(file);
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
if(len > temp){
|
||||
flipper_format_rewind(file);
|
||||
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
|
||||
snake_game_close_file(file);
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
len = temp;
|
||||
}
|
||||
}
|
||||
snake_game_close_file(file);
|
||||
return len;
|
||||
}
|
||||
|
||||
void snake_game_save_game_to_file(SnakeState* const snake_state) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperFormat* file = snake_game_open_file(storage);
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
#define SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT "CurrentMovement"
|
||||
#define SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT "NextMovement"
|
||||
#define SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS "FruitPoints"
|
||||
#define SNAKE_GAME_CONFIG_HIGHSCORE "Highscore"
|
||||
|
||||
int16_t snake_game_save_score_to_file(int32_t length);
|
||||
|
||||
void snake_game_save_game_to_file(SnakeState* const snake_state);
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ typedef enum {
|
||||
typedef struct {
|
||||
Point points[MAX_SNAKE_LEN];
|
||||
uint16_t len;
|
||||
int16_t highscore;
|
||||
Direction currentMovement;
|
||||
Direction nextMovement; // if backward of currentMovement, ignore
|
||||
Point fruit;
|
||||
|
||||
@@ -77,18 +77,21 @@ static void snake_game_render_callback(Canvas* const canvas, void* ctx) {
|
||||
if(snake_state->state == GameStateGameOver) {
|
||||
// Screen is 128x64 px
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
canvas_draw_box(canvas, 34, 20, 62, 24);
|
||||
canvas_draw_box(canvas, 32, 20, 64, 34);
|
||||
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_draw_frame(canvas, 34, 20, 62, 24);
|
||||
canvas_draw_frame(canvas, 32, 20, 64, 34);
|
||||
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 37, 31, "Game Over");
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
char buffer[12];
|
||||
char buffer[18];
|
||||
snprintf(buffer, sizeof(buffer), "Score: %u", snake_state->len - 7);
|
||||
canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignBottom, buffer);
|
||||
|
||||
snprintf(buffer, sizeof(buffer), "Highscore: %d", snake_state->highscore);
|
||||
canvas_draw_str_aligned(canvas, 64, 51, AlignCenter, AlignBottom, buffer);
|
||||
}
|
||||
|
||||
release_mutex((ValueMutex*)ctx, snake_state);
|
||||
@@ -239,6 +242,7 @@ static void
|
||||
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);
|
||||
return;
|
||||
}
|
||||
@@ -251,6 +255,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);
|
||||
return;
|
||||
}
|
||||
@@ -260,6 +265,7 @@ static void
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user