-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathColor_Game.cpp
93 lines (79 loc) · 2.1 KB
/
Color_Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <windows.h>
using namespace std;
vector<string> colors = {"Red", "Orange", "White", "Black", "Green", "Blue", "Brown", "Purple", "Cyan", "Yellow", "Pink", "Magenta"};
int score = 0;
string displayedWordColor = "";
bool gameRunning = false;
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void printScore() {
gotoxy(0, 2);
cout << "Your Score: " << score;
}
void printTimeLeft(int secondsLeft) {
gotoxy(0, 3);
cout << "Game Ends in: " << secondsLeft << "s";
}
void printGameDescription() {
cout << "Game Description: Enter the color of the words displayed below.\nAnd keep in mind not to enter the word text itself" << endl;
}
void startGame() {
if (!gameRunning) {
gameRunning = true;
score = 0;
system("cls");
printGameDescription();
printScore();
printTimeLeft(60);
displayedWordColor = colors[rand() % colors.size()];
}
}
void stopGame() {
gameRunning = false;
system("cls");
cout << "Game Over!" << endl;
}
void nextWord() {
if (gameRunning) {
string displayedWordText = colors[rand() % colors.size()];
gotoxy(0, 5);
cout << displayedWordText;
gotoxy(0, 6);
cout << "Enter the color: ";
displayedWordColor = colors[rand() % colors.size()];
}
}
void checkWord(string userInput) {
if (gameRunning) {
transform(userInput.begin(), userInput.end(), userInput.begin(), ::tolower);
if (userInput == displayedWordColor) {
score++;
printScore();
}
nextWord();
}
}
int main() {
srand(static_cast<unsigned int>(time(0)));
while (true) {
if (GetAsyncKeyState(VK_SPACE)) {
startGame();
}
if (GetAsyncKeyState(VK_RETURN)) {
string userInput;
getline(cin, userInput);
checkWord(userInput);
}
Sleep(10);
}
return 0;
}