mirror of
https://github.com/theoleuthardt/DinoGame.git
synced 2026-06-13 09:27:57 +00:00
feat: hint for jumping and automatic disappearance after a certain amount of time
This commit is contained in:
parent
06d5c33258
commit
2402e59659
2 changed files with 22 additions and 1 deletions
18
src/Game.cpp
18
src/Game.cpp
|
|
@ -5,18 +5,23 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Game::Game() {
|
Game::Game() {
|
||||||
|
// initialize the game window and set the target FPS
|
||||||
SetConfigFlags(FLAG_VSYNC_HINT);
|
SetConfigFlags(FLAG_VSYNC_HINT);
|
||||||
InitWindow(800, 400, "Retro Dino Game");
|
InitWindow(800, 400, "Retro Dino Game");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
|
// load the textures
|
||||||
dinoTexture = LoadTexture("assets/dino.png");
|
dinoTexture = LoadTexture("assets/dino.png");
|
||||||
cactusTexture = LoadTexture("assets/cactus.png");
|
cactusTexture = LoadTexture("assets/cactus.png");
|
||||||
groundTexture = LoadTexture("assets/ground.png");
|
groundTexture = LoadTexture("assets/ground.png");
|
||||||
|
|
||||||
|
// create the dino object and set the initial score, highscore and game over state
|
||||||
dino = new Dino(dinoTexture);
|
dino = new Dino(dinoTexture);
|
||||||
score = 0;
|
score = 0;
|
||||||
highscore = 0;
|
highscore = 0;
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
|
|
||||||
|
// spawn the first cactus
|
||||||
cacti.push_back(Cactus(cactusTexture, 800, 330 - cactusTexture.height));
|
cacti.push_back(Cactus(cactusTexture, 800, 330 - cactusTexture.height));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,10 +44,11 @@ void Game::Run() {
|
||||||
|
|
||||||
void Game::Update() {
|
void Game::Update() {
|
||||||
if (!gameOver) {
|
if (!gameOver) {
|
||||||
|
hintTimer += GetFrameTime();
|
||||||
|
|
||||||
if (IsKeyPressed(KEY_SPACE) || IsKeyPressed(KEY_UP)) {
|
if (IsKeyPressed(KEY_SPACE) || IsKeyPressed(KEY_UP)) {
|
||||||
dino->Jump();
|
dino->Jump();
|
||||||
}
|
}
|
||||||
|
|
||||||
dino->Update();
|
dino->Update();
|
||||||
|
|
||||||
static int frameCounter = 0;
|
static int frameCounter = 0;
|
||||||
|
|
@ -99,6 +105,10 @@ void Game::Draw() {
|
||||||
for (auto &cactus : cacti) {
|
for (auto &cactus : cacti) {
|
||||||
cactus.Draw();
|
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 {
|
} else {
|
||||||
int gameOverTextWidth = MeasureText("GAME OVER", 30);
|
int gameOverTextWidth = MeasureText("GAME OVER", 30);
|
||||||
DrawText("GAME OVER", GetScreenWidth()/2 - gameOverTextWidth/2, 150, 30, RED);
|
DrawText("GAME OVER", GetScreenWidth()/2 - gameOverTextWidth/2, 150, 30, RED);
|
||||||
|
|
@ -122,12 +132,18 @@ void Game::Draw() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::Reset() {
|
void Game::Reset() {
|
||||||
|
// create a new dino object and clear the cacti vector
|
||||||
delete dino;
|
delete dino;
|
||||||
dino = new Dino(dinoTexture);
|
dino = new Dino(dinoTexture);
|
||||||
cacti.clear();
|
cacti.clear();
|
||||||
|
|
||||||
|
// save the highscore and reset the score, game over state and hint timer
|
||||||
highscore = max(highscore, score);
|
highscore = max(highscore, score);
|
||||||
score = 0;
|
score = 0;
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
|
hintTimer = 0.0f;
|
||||||
|
|
||||||
|
// spawn the first cactus again
|
||||||
cacti.push_back(Cactus(cactusTexture, 900, 300 - cactusTexture.height));
|
cacti.push_back(Cactus(cactusTexture, 900, 300 - cactusTexture.height));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,11 @@ private:
|
||||||
* @brief The state if the game is over
|
* @brief The state if the game is over
|
||||||
*/
|
*/
|
||||||
bool gameOver;
|
bool gameOver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The timer to manage the jump hint to disappear after a certain time
|
||||||
|
*/
|
||||||
|
float hintTimer = 0.0f;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //GAME_HPP
|
#endif //GAME_HPP
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue