Merge pull request 'timer for cube gamw' (#8) from game_timer into master

Reviewed-on: #8
This commit is contained in:
Bruno Shehi 2025-12-08 14:53:56 +00:00
commit 2335ecbb37
3 changed files with 66 additions and 2 deletions

14
includes/gametimer.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <chrono>
class Timer {
public:
void start();
void stop();
long long elapsedMs() const;
private:
std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
std::chrono::time_point<std::chrono::high_resolution_clock> endTime;
bool running = false;
};

22
src/gametimer.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "gametimer.h"
void Timer::start() {
running = true;
startTime = std::chrono::high_resolution_clock::now();
}
void Timer::stop() {
running = false;
endTime = std::chrono::high_resolution_clock::now();
}
long long Timer::elapsedMs() const {
if (running) {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - startTime
).count();
}
return std::chrono::duration_cast<std::chrono::milliseconds>(
endTime - startTime
).count();
}

View File

@ -1,4 +1,5 @@
#include "gamecube.h"
#include "gametimer.h"
#include <algorithm>
#include <ctime>
@ -14,6 +15,9 @@ int main()
InitWindow(800, 600, "3D Memory Game with Matrix3D Library");
SetTargetFPS(60);
Timer timer;
timer.start();
Camera3D camera{};
camera.position = {6.0f, 6.0f, 6.0f};
camera.target = {0.0f, 0.0f, 0.0f};
@ -108,9 +112,15 @@ int main()
}
// Gewinnprüfung
if (!gameWon)
gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); });
// Gewinnprüfung
if (!gameWon) {
gameWon = std::all_of(cubes.begin(), cubes.end(),
[](const gamecube &c){ return c.IsMatched(); });
if (gameWon)
timer.stop();
}
// <--- NEU
// -----------------------------------------------------------
// Zeichnen
// -----------------------------------------------------------
@ -128,6 +138,24 @@ int main()
else
DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY);
if (gameWon)
DrawText("Congrats! You found all pairs!", 150, 260, 30, DARKBLUE);
else
DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY);
if (!gameWon) {
long long elapsed = timer.elapsedMs() / 1000; // Sekunden
std::string timeStr = "Time: " + std::to_string(elapsed) + "s";
DrawText(timeStr.c_str(), 10, 40, 20, DARKGRAY);
} else {
// Optional: Endzeit anzeigen
long long totalTime = timer.elapsedMs() / 1000;
std::string endStr = "Finished in " + std::to_string(totalTime) + " seconds!";
DrawText(endStr.c_str(), 150, 300, 25, DARKBLUE);
}
EndDrawing();
}