diff --git a/semestralka1/src/game/game_over.cpp b/semestralka1/src/game/game_over.cpp new file mode 100644 index 0000000..232408c --- /dev/null +++ b/semestralka1/src/game/game_over.cpp @@ -0,0 +1,13 @@ +// src/game/game_over.cpp +#include "game_over.h" +#include + +void show_game_over_screen(int seconds) { + printf("\033[2J\033[H"); + printf("==================================\r\n"); + printf(" G A M E O V E R \r\n"); + printf("==================================\r\n"); + printf(" Vydrzal si %d sekund. \r\n", seconds); + printf("==================================\r\n"); + fflush(stdout); +} diff --git a/semestralka1/src/game/game_over.h b/semestralka1/src/game/game_over.h new file mode 100644 index 0000000..d4b0db6 --- /dev/null +++ b/semestralka1/src/game/game_over.h @@ -0,0 +1,7 @@ +// src/game/game_over.h +#pragma once +#include "mbed.h" + +// Displays game over message with elapsed time. +// Takes a Timer that was measuring game duration. +void show_game_over_screen(int seconds); diff --git a/semestralka1/src/game/game_state.h b/semestralka1/src/game/game_state.h index c0bd079..c60d65c 100644 --- a/semestralka1/src/game/game_state.h +++ b/semestralka1/src/game/game_state.h @@ -26,6 +26,7 @@ struct GameState { int current_speed; ObstacleRenderData obstacles[MAX_RENDER_OBSTACLES]; + int elapsed_seconds; bool game_over; bool need_redraw; }; diff --git a/semestralka1/src/render/loop.cpp b/semestralka1/src/render/loop.cpp index 261dc33..0f44285 100644 --- a/semestralka1/src/render/loop.cpp +++ b/semestralka1/src/render/loop.cpp @@ -6,6 +6,7 @@ #include "mbed.h" #include "player.h" #include "background.h" +#include "../game/game_over.h" #include "../game/game_state.h" #include "../game/state.h" #include "../game/animation.h" @@ -46,6 +47,8 @@ void logic_loop(void *arg) { int anim_tick_counter = 0; int tick_counter = 0; bool game_over = false; + Timer game_timer; + game_timer.start(); // Spawn first obstacle if (speed >= 5) @@ -138,8 +141,11 @@ void logic_loop(void *arg) { ThisThread::sleep_for(25ms); } - // final update + game_timer.stop(); + int seconds = static_cast(game_timer.elapsed_time().count() / 1000000); + g_state_mutex.lock(); + g_state.elapsed_seconds = seconds; g_state.game_over = true; g_state_mutex.unlock(); } @@ -154,8 +160,7 @@ void render_loop_thread() { g_state_mutex.unlock(); if (over) { - printf("\033[2J\033[H"); - printf("GAME OVER\r\n"); + show_game_over_screen(local.elapsed_seconds); break; }