From 2402e59659bc20fb2c24132fccfadcb63e75ba29 Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Thu, 13 Mar 2025 14:41:10 +0100 Subject: [PATCH] feat: hint for jumping and automatic disappearance after a certain amount of time --- src/Game.cpp | 18 +++++++++++++++++- src/Game.hpp | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Game.cpp b/src/Game.cpp index c00e566..d1618e8 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -5,18 +5,23 @@ using namespace std; Game::Game() { + // initialize the game window and set the target FPS SetConfigFlags(FLAG_VSYNC_HINT); InitWindow(800, 400, "Retro Dino Game"); SetTargetFPS(60); + // load the textures dinoTexture = LoadTexture("assets/dino.png"); cactusTexture = LoadTexture("assets/cactus.png"); groundTexture = LoadTexture("assets/ground.png"); + // create the dino object and set the initial score, highscore and game over state dino = new Dino(dinoTexture); score = 0; highscore = 0; gameOver = false; + + // spawn the first cactus cacti.push_back(Cactus(cactusTexture, 800, 330 - cactusTexture.height)); } @@ -39,10 +44,11 @@ void Game::Run() { void Game::Update() { if (!gameOver) { + hintTimer += GetFrameTime(); + if (IsKeyPressed(KEY_SPACE) || IsKeyPressed(KEY_UP)) { dino->Jump(); } - dino->Update(); static int frameCounter = 0; @@ -99,6 +105,10 @@ void Game::Draw() { for (auto &cactus : cacti) { cactus.Draw(); } + if (hintTimer < 4.0f) { + int hintWidth = MeasureText("Press SPACE to Jump!", 20); + DrawText("Press SPACE to Jump!", GetScreenWidth()/2 - hintWidth/2, 50, 20, DARKGRAY); + } } else { int gameOverTextWidth = MeasureText("GAME OVER", 30); DrawText("GAME OVER", GetScreenWidth()/2 - gameOverTextWidth/2, 150, 30, RED); @@ -122,12 +132,18 @@ void Game::Draw() { } void Game::Reset() { + // create a new dino object and clear the cacti vector delete dino; dino = new Dino(dinoTexture); cacti.clear(); + + // save the highscore and reset the score, game over state and hint timer highscore = max(highscore, score); score = 0; gameOver = false; + hintTimer = 0.0f; + + // spawn the first cactus again cacti.push_back(Cactus(cactusTexture, 900, 300 - cactusTexture.height)); } diff --git a/src/Game.hpp b/src/Game.hpp index 55730eb..ffad293 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -108,6 +108,11 @@ private: * @brief The state if the game is over */ bool gameOver; + + /** + * @brief The timer to manage the jump hint to disappear after a certain time + */ + float hintTimer = 0.0f; }; #endif //GAME_HPP