/
Kurisoff
/
ColourGame
Обзор
Документация
Войти
/
Kurisoff
/
ColourGame
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.cpp
221 строка
6 KB
danchick333
add web compilation
24 май 2026, 03:32
24 май 2026, 03:32
b416b16
Код
Авторство
О чём код?
#include "raylib.h" #include <string> #include <vector> #include <ctime> #include <cstdlib> #include <algorithm> #include <locale.h> #include <random> enum GameScreen { MENU, GAME, GAMEOVER }; struct ColorData { std::string name; Color color; }; const int SCREEN_WIDTH = 1100; const int SCREEN_HEIGHT = 770; std::vector<ColorData> colors = { {"Red", RED}, {"Green", GREEN}, {"Blue", BLUE}, {"Yellow", YELLOW}, {"Purple", PURPLE}, {"Orange", ORANGE} }; GameScreen currentScreen = MENU; int correctColorIndex = 0; int displayedWordIndex = 0; std::vector<int> answers; float totalReactionTime = 0.0f; int correctAnswers = 0; double roundStartTime = 0.0; Rectangle buttons[3]; void GenerateRound() { displayedWordIndex = rand() % colors.size(); do { correctColorIndex = rand() % colors.size(); } while (correctColorIndex == displayedWordIndex); answers.clear(); answers.push_back(correctColorIndex); while (answers.size() < 3) { int r = rand() % colors.size(); if (std::find(answers.begin(), answers.end(), r) == answers.end()) { answers.push_back(r); } } std::random_device rd; std::mt19937 gen(rd()); std::shuffle(answers.begin(), answers.end(), gen); roundStartTime = GetTime(); } void ResetGame() { totalReactionTime = 0.0f; correctAnswers = 0; GenerateRound(); } int main() { // Font font = LoadFont("assets/fonts/arial.ttf"); srand(time(nullptr)); InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Color Reaction Game"); SetTargetFPS(60); buttons[0] = {250, 500, 150, 60}; buttons[1] = {425, 500, 150, 60}; buttons[2] = {600, 500, 150, 60}; while (!WindowShouldClose()) { BeginDrawing(); ClearBackground(DARKGRAY); switch (currentScreen) { case MENU: { DrawText("COLOR REACTION GAME", 250, 150, 40, WHITE); float average = 0.0f; if (correctAnswers > 0) { average = totalReactionTime / correctAnswers; } DrawText(TextFormat("AVERAGE TIME: %.2f SEC", average), 320, 260, 30, YELLOW); Rectangle startButton = {350, 400, 300, 80}; DrawRectangleRec(startButton, BLUE); DrawText("START GAME", 385, 425, 30, WHITE); if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { Vector2 mouse = GetMousePosition(); if (CheckCollisionPointRec(mouse, startButton)) { ResetGame(); currentScreen = GAME; } } } break; case GAME: { DrawText( colors[displayedWordIndex].name.c_str(), 350, 200, 60, colors[correctColorIndex].color ); for (int i = 0; i < 3; i++) { DrawRectangleRec(buttons[i], LIGHTGRAY); DrawText( colors[answers[i]].name.c_str(), buttons[i].x + 10, buttons[i].y + 18, 25, BLACK ); } if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { Vector2 mouse = GetMousePosition(); for (int i = 0; i < 3; i++) { if (CheckCollisionPointRec(mouse, buttons[i])) { if (answers[i] == correctColorIndex) { double reaction = GetTime() - roundStartTime; totalReactionTime += (float)reaction; correctAnswers++; GenerateRound(); } else { currentScreen = GAMEOVER; } } } } } break; case GAMEOVER: { DrawText("DEFEAT", 360, 150, 60, RED); float average = 0.0f; if (correctAnswers > 0) { average = totalReactionTime / correctAnswers; } DrawText(TextFormat("AVERAGE: %.2f SEC", average), 300, 280, 35, WHITE); Rectangle retryButton = {250, 450, 220, 80}; Rectangle menuButton = {530, 450, 220, 80}; DrawRectangleRec(retryButton, GREEN); DrawRectangleRec(menuButton, BLUE); DrawText("REPEAT", 300, 475, 25, BLACK); DrawText("MAIN MENU", 590, 475, 25, WHITE); if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { Vector2 mouse = GetMousePosition(); if (CheckCollisionPointRec(mouse, retryButton)) { ResetGame(); currentScreen = GAME; } if (CheckCollisionPointRec(mouse, menuButton)) { currentScreen = MENU; } } } break; } EndDrawing(); } CloseWindow(); // UnloadFont(font); return 0; }