diff --git a/includes/gametimer.h b/includes/gametimer.h new file mode 100644 index 0000000..480ed74 --- /dev/null +++ b/includes/gametimer.h @@ -0,0 +1,14 @@ +#pragma once +#include + +class Timer { +public: + void start(); + void stop(); + long long elapsedMs() const; + +private: + std::chrono::time_point startTime; + std::chrono::time_point endTime; + bool running = false; +}; diff --git a/src/gametimer.cpp b/src/gametimer.cpp new file mode 100644 index 0000000..fdaa9de --- /dev/null +++ b/src/gametimer.cpp @@ -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::high_resolution_clock::now() - startTime + ).count(); + } + return std::chrono::duration_cast( + endTime - startTime + ).count(); +} diff --git a/src/main.cpp b/src/main.cpp index 287251d..76f57c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "gamecube.h" +#include "gametimer.h" #include #include @@ -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(); }