Compare commits
4
Commits
0e9ee64c18
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
683917f564 | ||
|
|
4276656917 | ||
|
|
b13d535c27 | ||
|
|
6a54e6d4a2 |
+99
-100
@@ -1,100 +1,107 @@
|
|||||||
#include "gamecube.h"
|
#include "gamecube.h"
|
||||||
|
#include "raylib.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "raylib.h"
|
#include <cstdio>
|
||||||
|
|
||||||
|
// -----------------------------------------------------------
|
||||||
|
// 3D Memory Game – Hauptprogramm
|
||||||
|
// -----------------------------------------------------------
|
||||||
|
|
||||||
enum GameScreen { MENU = 0, GAMEPLAY };
|
enum GameScreen { MENU = 0, GAMEPLAY };
|
||||||
|
|
||||||
static void SetupGame(std::vector<gamecube>& cubes, int pairs)
|
void SetupGame(std::vector<gamecube>& cubes, int pairs)
|
||||||
{
|
{
|
||||||
cubes.clear();
|
cubes.clear();
|
||||||
|
|
||||||
Color colors[] = { RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, WHITE, SKYBLUE };
|
Color colors[] = { RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, WHITE, SKYBLUE };
|
||||||
|
|
||||||
|
int count = pairs * 2;
|
||||||
|
int cols = (count > 6) ? 4 : 3;
|
||||||
|
int rows = (count > 6) ? 3 : 2;
|
||||||
|
|
||||||
std::vector<Vec3> positions;
|
std::vector<Vec3> positions;
|
||||||
|
|
||||||
const int count = pairs * 2;
|
|
||||||
int cols = 3;
|
|
||||||
int rows = 2;
|
|
||||||
|
|
||||||
if (count > 6) {
|
|
||||||
cols = 4;
|
|
||||||
rows = 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (int r = 0; r < rows && index < count; ++r) {
|
for (int r = 0; r < rows && index < count; ++r)
|
||||||
for (int c = 0; c < cols && index < count; ++c) {
|
{
|
||||||
positions.push_back({ (float)c * 2.0f - (cols - 1), 0.0f, (float)r * 2.0f - (rows - 1) });
|
for (int c = 0; c < cols && index < count; ++c)
|
||||||
|
{
|
||||||
|
positions.push_back({ (float)c * 2.0f - (cols - 1), 0.0f,
|
||||||
|
(float)r * 2.0f - (rows - 1) });
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Color> colorPool;
|
std::vector<Color> colorPool;
|
||||||
colorPool.reserve(count);
|
for (int i = 0; i < pairs; ++i)
|
||||||
|
{
|
||||||
for (int i = 0; i < pairs; i++) {
|
|
||||||
colorPool.push_back(colors[i]);
|
colorPool.push_back(colors[i]);
|
||||||
colorPool.push_back(colors[i]);
|
colorPool.push_back(colors[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = (int)colorPool.size() - 1; i > 0; --i) {
|
// Shuffle
|
||||||
|
for (int i = (int)colorPool.size() - 1; i > 0; --i)
|
||||||
|
{
|
||||||
int j = rand() % (i + 1);
|
int j = rand() % (i + 1);
|
||||||
std::swap(colorPool[i], colorPool[j]);
|
std::swap(colorPool[i], colorPool[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; ++i)
|
||||||
cubes.emplace_back(positions[i], colorPool[i]);
|
cubes.emplace_back(positions[i], colorPool[i]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
srand((unsigned)time(NULL));
|
srand((unsigned)time(nullptr));
|
||||||
|
|
||||||
double startTime = 0.0;
|
InitWindow(800, 600, "3D Memory Game");
|
||||||
double endTime = 0.0;
|
|
||||||
bool timerStarted = false;
|
|
||||||
|
|
||||||
GameScreen currentScreen = MENU;
|
|
||||||
int selectedPairs = 3;
|
|
||||||
|
|
||||||
std::vector<gamecube> cubes;
|
|
||||||
gamecube* first = nullptr;
|
|
||||||
gamecube* second = nullptr;
|
|
||||||
float flipSpeed = 5.0f;
|
|
||||||
bool gameWon = false;
|
|
||||||
|
|
||||||
InitWindow(800, 600, "3D Memory Game with Matrix3D Library");
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
Camera3D camera{};
|
Camera3D camera{};
|
||||||
camera.position = { 6.0f, 6.0f, 6.0f };
|
camera.position = { 6.0f, 6.0f, 6.0f };
|
||||||
camera.target = { 0.0f, 0.0f, 0.0f };
|
camera.target = { 0.0f, 0.0f, 0.0f };
|
||||||
camera.up = { 0.0f, 1.0f, 0.0f };
|
camera.up = { 0.0f, 1.0f, 0.0f };
|
||||||
camera.fovy = 45.0f;
|
camera.fovy = 45.0f;
|
||||||
camera.projection = CAMERA_PERSPECTIVE;
|
camera.projection = CAMERA_PERSPECTIVE;
|
||||||
|
|
||||||
|
GameScreen currentScreen = MENU;
|
||||||
|
|
||||||
|
std::vector<gamecube> cubes;
|
||||||
|
gamecube* first = nullptr;
|
||||||
|
gamecube* second = nullptr;
|
||||||
|
|
||||||
|
int selectedPairs = 3;
|
||||||
|
float flipSpeed = 180.0f; // Grad pro Sekunde
|
||||||
|
|
||||||
|
bool gameWon = false;
|
||||||
|
bool timerStarted = false;
|
||||||
|
double startTime = 0.0;
|
||||||
|
double endTime = 0.0;
|
||||||
|
|
||||||
while (!WindowShouldClose())
|
while (!WindowShouldClose())
|
||||||
{
|
{
|
||||||
|
// ---------------- UPDATE ----------------
|
||||||
switch (currentScreen)
|
switch (currentScreen)
|
||||||
{
|
{
|
||||||
case MENU:
|
case MENU:
|
||||||
{
|
{
|
||||||
if (IsKeyPressed(KEY_THREE)) {
|
if (IsKeyPressed(KEY_THREE))
|
||||||
|
{
|
||||||
selectedPairs = 3;
|
selectedPairs = 3;
|
||||||
SetupGame(cubes, selectedPairs);
|
SetupGame(cubes, selectedPairs);
|
||||||
|
currentScreen = GAMEPLAY;
|
||||||
gameWon = false;
|
gameWon = false;
|
||||||
first = second = nullptr;
|
first = second = nullptr;
|
||||||
currentScreen = GAMEPLAY;
|
|
||||||
timerStarted = false;
|
timerStarted = false;
|
||||||
}
|
}
|
||||||
else if (IsKeyPressed(KEY_SIX)) {
|
else if (IsKeyPressed(KEY_SIX))
|
||||||
|
{
|
||||||
selectedPairs = 6;
|
selectedPairs = 6;
|
||||||
SetupGame(cubes, selectedPairs);
|
SetupGame(cubes, selectedPairs);
|
||||||
|
currentScreen = GAMEPLAY;
|
||||||
gameWon = false;
|
gameWon = false;
|
||||||
first = second = nullptr;
|
first = second = nullptr;
|
||||||
currentScreen = GAMEPLAY;
|
|
||||||
timerStarted = false;
|
timerStarted = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -102,63 +109,67 @@ int main()
|
|||||||
|
|
||||||
case GAMEPLAY:
|
case GAMEPLAY:
|
||||||
{
|
{
|
||||||
if (!timerStarted) {
|
if (!timerStarted)
|
||||||
|
{
|
||||||
startTime = GetTime();
|
startTime = GetTime();
|
||||||
timerStarted = true;
|
timerStarted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && (second == nullptr))
|
// Klick-Erkennung mit Ray
|
||||||
|
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && second == nullptr)
|
||||||
{
|
{
|
||||||
Vector2 mouse = GetMousePosition();
|
Ray ray = GetMouseRay(GetMousePosition(), camera);
|
||||||
Ray ray = GetMouseRay(mouse, camera);
|
gamecube* hit = nullptr;
|
||||||
|
|
||||||
gamecube* hitCube = nullptr;
|
|
||||||
float bestDist = 1e9f;
|
float bestDist = 1e9f;
|
||||||
|
|
||||||
for (auto& c : cubes)
|
for (auto& c : cubes)
|
||||||
{
|
{
|
||||||
if (c.IsMatched() || c.IsFlipped()) continue;
|
if (c.IsMatched() || c.IsFlipped()) continue;
|
||||||
|
|
||||||
auto p = c.GetPosition();
|
Vec3 p = c.GetPosition();
|
||||||
|
|
||||||
BoundingBox box;
|
BoundingBox box;
|
||||||
box.min = { p.x - 0.75f, p.y - 0.75f, p.z - 0.75f };
|
box.min = { p.x - 0.75f, p.y - 0.75f, p.z - 0.75f };
|
||||||
box.max = { p.x + 0.75f, p.y + 0.75f, p.z + 0.75f };
|
box.max = { p.x + 0.75f, p.y + 0.75f, p.z + 0.75f };
|
||||||
|
|
||||||
RayCollision col = GetRayCollisionBox(ray, box);
|
RayCollision col = GetRayCollisionBox(ray, box);
|
||||||
if (col.hit && col.distance < bestDist) {
|
if (col.hit && col.distance < bestDist)
|
||||||
|
{
|
||||||
bestDist = col.distance;
|
bestDist = col.distance;
|
||||||
hitCube = &c;
|
hit = &c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hitCube)
|
if (hit)
|
||||||
{
|
{
|
||||||
if (!first) {
|
if (!first)
|
||||||
first = hitCube;
|
{
|
||||||
|
first = hit;
|
||||||
first->FlipForward();
|
first->FlipForward();
|
||||||
}
|
}
|
||||||
else if (hitCube != first) {
|
else if (hit != first)
|
||||||
second = hitCube;
|
{
|
||||||
|
second = hit;
|
||||||
second->FlipForward();
|
second->FlipForward();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& c : cubes) {
|
// Animation
|
||||||
|
for (auto& c : cubes)
|
||||||
c.Update(flipSpeed);
|
c.Update(flipSpeed);
|
||||||
}
|
|
||||||
|
|
||||||
|
// Matching
|
||||||
if (first && second && first->IsFlipped() && second->IsFlipped())
|
if (first && second && first->IsFlipped() && second->IsFlipped())
|
||||||
{
|
{
|
||||||
Color col1 = first->GetColor();
|
if (first->GetColor().r == second->GetColor().r &&
|
||||||
Color col2 = second->GetColor();
|
first->GetColor().g == second->GetColor().g &&
|
||||||
|
first->GetColor().b == second->GetColor().b)
|
||||||
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b) {
|
{
|
||||||
first->SetMatched(true);
|
first->SetMatched(true);
|
||||||
second->SetMatched(true);
|
second->SetMatched(true);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
first->FlipBackward();
|
first->FlipBackward();
|
||||||
second->FlipBackward();
|
second->FlipBackward();
|
||||||
}
|
}
|
||||||
@@ -169,54 +180,42 @@ int main()
|
|||||||
|
|
||||||
if (!gameWon)
|
if (!gameWon)
|
||||||
{
|
{
|
||||||
gameWon = std::all_of(
|
gameWon = std::all_of(cubes.begin(), cubes.end(),
|
||||||
cubes.begin(),
|
[](const gamecube& c){ return c.IsMatched(); });
|
||||||
cubes.end(),
|
if (gameWon)
|
||||||
[](const gamecube& c) { return c.IsMatched(); }
|
|
||||||
);
|
|
||||||
|
|
||||||
if (gameWon) {
|
|
||||||
endTime = GetTime() - startTime;
|
endTime = GetTime() - startTime;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------- DRAW ----------------
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
switch (currentScreen)
|
if (currentScreen == MENU)
|
||||||
{
|
{
|
||||||
case MENU:
|
DrawText("3D MEMORY SPIEL", 200, 80, 40, DARKGRAY);
|
||||||
|
DrawText("[3] = 3 Paare", 200, 200, 24, BLUE);
|
||||||
|
DrawText("[6] = 6 Paare", 200, 240, 24, BLUE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BeginMode3D(camera);
|
||||||
|
for (auto& c : cubes) c.Draw();
|
||||||
|
EndMode3D();
|
||||||
|
|
||||||
|
if (gameWon)
|
||||||
{
|
{
|
||||||
DrawText("3D MEMORY SPIEL",
|
char buf[64];
|
||||||
GetScreenWidth() / 2 - MeasureText("3D MEMORY SPIEL", 50) / 2,
|
sprintf(buf, "Gewonnen in %.2f Sekunden", endTime);
|
||||||
80, 50, DARKGRAY);
|
DrawText(buf, 150, 260, 30, DARKGREEN);
|
||||||
DrawText("Waehlen Sie die Spielgroesse:", 50, 200, 25, BLACK);
|
|
||||||
DrawText("Druecken Sie [3] fuer 3 Paare (6 Wuerfel)", 50, 240, 20, BLUE);
|
|
||||||
DrawText("Druecken Sie [6] fuer 6 Paare (12 Wuerfel)", 50, 280, 20, BLUE);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
case GAMEPLAY:
|
|
||||||
{
|
{
|
||||||
BeginMode3D(camera);
|
char buf[64];
|
||||||
for (auto& c : cubes) c.Draw();
|
sprintf(buf, "Time: %.2f", GetTime() - startTime);
|
||||||
EndMode3D();
|
DrawText(buf, 10, 10, 20, DARKGRAY);
|
||||||
|
|
||||||
if (gameWon) {
|
|
||||||
DrawText("Congrats! You found all pairs!", 150, 260, 30, DARKBLUE);
|
|
||||||
char buffer[64];
|
|
||||||
sprintf(buffer, "Cleared in %.2f seconds", endTime);
|
|
||||||
DrawText(buffer, 150, 300, 28, DARKGREEN);
|
|
||||||
} else {
|
|
||||||
DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY);
|
|
||||||
char liveBuf[64];
|
|
||||||
sprintf(liveBuf, "Time: %.2f", GetTime() - startTime);
|
|
||||||
DrawText(liveBuf, 10, 40, 20, DARKGRAY);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user