mirror of
https://github.com/theoleuthardt/DinoGame.git
synced 2026-06-13 09:27:57 +00:00
feat: save mechanic and cactus/dino spawn logic
This commit is contained in:
parent
d5060f7552
commit
c7554cbb81
5 changed files with 63 additions and 6 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
#include "Cactus.hpp"
|
#include "Cactus.hpp"
|
||||||
|
|
||||||
Cactus::Cactus(Texture2D texture, float x) : texture(texture) {
|
Cactus::Cactus(Texture2D texture, float x, float y) : texture(texture) {
|
||||||
rect = {static_cast<float>(x), static_cast<float>(300 - texture.height),
|
rect = {static_cast<float>(x), static_cast<float>(300 - texture.height),
|
||||||
static_cast<float>(texture.width), static_cast<float>(texture.height)};
|
static_cast<float>(texture.width), static_cast<float>(texture.height)};
|
||||||
}
|
}
|
||||||
|
|
@ -18,5 +18,11 @@ bool Cactus::IsOffScreen() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Rectangle Cactus::GetRect() {
|
Rectangle Cactus::GetRect() {
|
||||||
return rect;
|
float hitboxWidth = texture.width * 0.8f;
|
||||||
|
float hitboxHeight = texture.height * 0.7f;
|
||||||
|
|
||||||
|
float hitboxX = rect.x + (texture.width - hitboxWidth) / 2;
|
||||||
|
float hitboxY = rect.y + (texture.height - hitboxHeight);
|
||||||
|
|
||||||
|
return {hitboxX, hitboxY, hitboxWidth, hitboxHeight};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
class Cactus {
|
class Cactus {
|
||||||
public:
|
public:
|
||||||
Cactus(Texture2D texture, float x);
|
Cactus(Texture2D texture, float x, float y);
|
||||||
void Update();
|
void Update();
|
||||||
void Draw();
|
void Draw();
|
||||||
bool IsOffScreen();
|
bool IsOffScreen();
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#include "Dino.hpp"
|
#include "Dino.hpp"
|
||||||
|
|
||||||
Dino::Dino(Texture2D tex) : texture(tex), velocityY(0), isJumping(false) {
|
Dino::Dino(Texture2D tex) : texture(tex), velocityY(0), isJumping(false) {
|
||||||
rect = {50.0f, static_cast<float>(300 - texture.height),
|
rect = {50.0f, static_cast<float>(330 - texture.height),
|
||||||
static_cast<float>(texture.width), static_cast<float>(texture.height)};
|
static_cast<float>(texture.width), static_cast<float>(texture.height)};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
52
src/Game.cpp
52
src/Game.cpp
|
|
@ -15,7 +15,9 @@ Game::Game() {
|
||||||
|
|
||||||
dino = new Dino(dinoTexture);
|
dino = new Dino(dinoTexture);
|
||||||
score = 0;
|
score = 0;
|
||||||
|
highscore = 0;
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
|
cacti.push_back(Cactus(cactusTexture, 800, 330 - cactusTexture.height));
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::~Game() {
|
Game::~Game() {
|
||||||
|
|
@ -27,6 +29,8 @@ Game::~Game() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::Run() {
|
void Game::Run() {
|
||||||
|
Load();
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
Update();
|
Update();
|
||||||
Draw();
|
Draw();
|
||||||
|
|
@ -41,8 +45,27 @@ void Game::Update() {
|
||||||
|
|
||||||
dino->Update();
|
dino->Update();
|
||||||
|
|
||||||
if (GetRandomValue(0, 100) < 2) {
|
static int frameCounter = 0;
|
||||||
cacti.push_back(Cactus(cactusTexture, 800));
|
frameCounter++;
|
||||||
|
int spawnInterval = max(20, 50 - score / 1000);
|
||||||
|
int spawnChance = max(2, 8 - score / 2500);
|
||||||
|
|
||||||
|
if (frameCounter >= spawnInterval) {
|
||||||
|
frameCounter = 0;
|
||||||
|
|
||||||
|
if (GetRandomValue(1, spawnChance) == 1) {
|
||||||
|
bool canSpawn = true;
|
||||||
|
for (auto &cactus : cacti) {
|
||||||
|
if (cactus.GetRect().x > GetScreenWidth() * 2/3) {
|
||||||
|
canSpawn = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (canSpawn) {
|
||||||
|
cacti.push_back(Cactus(cactusTexture, 800, 330 - cactusTexture.height));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &cactus : cacti) {
|
for (auto &cactus : cacti) {
|
||||||
|
|
@ -68,6 +91,7 @@ void Game::Draw() {
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
DrawText(TextFormat("Score: %d", score), 10, 10, 20, BLACK);
|
DrawText(TextFormat("Score: %d", score), 10, 10, 20, BLACK);
|
||||||
|
DrawText(TextFormat("Highscore: %d", highscore), 10, 30, 20, BLACK);
|
||||||
DrawTexture(groundTexture, 0, 300, WHITE);
|
DrawTexture(groundTexture, 0, 300, WHITE);
|
||||||
|
|
||||||
if (!gameOver) {
|
if (!gameOver) {
|
||||||
|
|
@ -87,6 +111,8 @@ void Game::Draw() {
|
||||||
|
|
||||||
int exitTextWidth = MeasureText("Press ESC to close the game!", 20);
|
int exitTextWidth = MeasureText("Press ESC to close the game!", 20);
|
||||||
DrawText("Press ESC to close the game!", GetScreenWidth()/2 - exitTextWidth/2, 210, 20, BLACK);
|
DrawText("Press ESC to close the game!", GetScreenWidth()/2 - exitTextWidth/2, 210, 20, BLACK);
|
||||||
|
|
||||||
|
Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
|
|
@ -96,8 +122,30 @@ void Game::Reset() {
|
||||||
delete dino;
|
delete dino;
|
||||||
dino = new Dino(dinoTexture);
|
dino = new Dino(dinoTexture);
|
||||||
cacti.clear();
|
cacti.clear();
|
||||||
|
highscore = max(highscore, score);
|
||||||
score = 0;
|
score = 0;
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
|
cacti.push_back(Cactus(cactusTexture, 900, 300 - cactusTexture.height));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::Save() {
|
||||||
|
FILE *file = fopen("save.dat", "wb");
|
||||||
|
if (file) {
|
||||||
|
fwrite(&highscore, sizeof(highscore), 1, file);
|
||||||
|
fclose(file);
|
||||||
|
} else {
|
||||||
|
TraceLog(LOG_WARNING, "Failed to save highscore");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::Load() {
|
||||||
|
FILE *file = fopen("save.dat", "rb");
|
||||||
|
if (file) {
|
||||||
|
fread(&highscore, sizeof(highscore), 1, file);
|
||||||
|
fclose(file);
|
||||||
|
} else {
|
||||||
|
TraceLog(LOG_WARNING, "Failed to load highscore");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::CheckCollision(Dino &dino, Cactus &cactus) {
|
bool Game::CheckCollision(Dino &dino, Cactus &cactus) {
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ private:
|
||||||
void Update();
|
void Update();
|
||||||
void Draw();
|
void Draw();
|
||||||
void Reset();
|
void Reset();
|
||||||
|
void Save();
|
||||||
|
void Load();
|
||||||
bool CheckCollision(Dino &dino, Cactus &cactus);
|
bool CheckCollision(Dino &dino, Cactus &cactus);
|
||||||
|
|
||||||
Texture2D dinoTexture;
|
Texture2D dinoTexture;
|
||||||
|
|
@ -25,6 +27,7 @@ private:
|
||||||
Dino *dino;
|
Dino *dino;
|
||||||
vector<Cactus> cacti;
|
vector<Cactus> cacti;
|
||||||
int score;
|
int score;
|
||||||
|
int highscore;
|
||||||
bool gameOver;
|
bool gameOver;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue