feat: game class for managing the game and running it in main

This commit is contained in:
theoleuthardt 2025-03-13 10:55:41 +01:00
parent 9ff73a95e3
commit 6fd379cead
3 changed files with 137 additions and 3 deletions

102
src/Game.cpp Normal file
View file

@ -0,0 +1,102 @@
#include "Game.hpp"
#include "raylib.h"
Game::Game() {
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(800, 400, "Retro Dino Game");
SetTargetFPS(60);
dinoTexture = LoadTexture("assets/dino.png");
cactusTexture = LoadTexture("assets/cactus.png");
groundTexture = LoadTexture("assets/ground.png");
dino = new Dino(dinoTexture);
score = 0;
gameOver = false;
}
Game::~Game() {
UnloadTexture(dinoTexture);
UnloadTexture(cactusTexture);
UnloadTexture(groundTexture);
delete dino;
CloseWindow();
}
void Game::Run() {
while (!WindowShouldClose()) {
Update();
Draw();
}
}
void Game::Update() {
if (!gameOver) {
if (IsKeyPressed(KEY_SPACE) || IsKeyPressed(KEY_UP)) {
dino->Jump();
}
dino->Update();
if (GetRandomValue(0, 100) < 2) {
cacti.push_back(Cactus(cactusTexture, 800));
}
for (auto &cactus : cacti) {
cactus.Update();
}
cacti.erase(std::remove_if(cacti.begin(), cacti.end(),
[](Cactus &c) { return c.IsOffScreen(); }),
cacti.end());
for (auto &cactus : cacti) {
if (CheckCollision(*dino, cactus)) {
gameOver = true;
}
}
score++;
}
}
void Game::Draw() {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText(TextFormat("Score: %d", score), 10, 10, 20, BLACK);
DrawTexture(groundTexture, 0, 300, WHITE);
if (!gameOver) {
dino->Draw();
for (auto &cactus : cacti) {
cactus.Draw();
}
} else {
int gameOverTextWidth = MeasureText("GAME OVER", 30);
DrawText("GAME OVER", GetScreenWidth()/2 - gameOverTextWidth/2, 150, 30, RED);
int restartTextWidth = MeasureText("Press R to restart!", 20);
DrawText("Press R to restart!", GetScreenWidth()/2 - restartTextWidth/2, 190, 20, BLACK);
if (IsKeyPressed(KEY_R)) {
Reset();
}
int exitTextWidth = MeasureText("Press ESC to close the game!", 20);
DrawText("Press ESC to close the game!", GetScreenWidth()/2 - exitTextWidth/2, 210, 20, BLACK);
}
EndDrawing();
}
void Game::Reset() {
delete dino;
dino = new Dino(dinoTexture);
cacti.clear();
score = 0;
gameOver = false;
}
bool Game::CheckCollision(Dino &dino, Cactus &cactus) {
return CheckCollisionRecs(dino.GetRect(), cactus.GetRect());
}

31
src/Game.hpp Normal file
View file

@ -0,0 +1,31 @@
#ifndef GAME_HPP
#define GAME_HPP
#include "raylib.h"
#include "Dino.hpp"
#include "Cactus.hpp"
#include <vector>
using namespace std;
class Game {
public:
Game();
~Game();
void Run();
private:
void Update();
void Draw();
void Reset();
bool CheckCollision(Dino &dino, Cactus &cactus);
Texture2D dinoTexture;
Texture2D cactusTexture;
Texture2D groundTexture;
Dino *dino;
vector<Cactus> cacti;
int score;
bool gameOver;
};
#endif //GAME_HPP

View file

@ -1,6 +1,7 @@
#include <iostream>
#include "Game.hpp"
int main() {
std::cout << "Hello, World!" << std::endl;
Game game;
game.Run();
return 0;
}