-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.cpp
More file actions
342 lines (301 loc) · 7.69 KB
/
basic.cpp
File metadata and controls
342 lines (301 loc) · 7.69 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <algorithm> // Include for std::find
using namespace std;
// Constants
const int WIDTH = 20;
const int HEIGHT = 20;
const int INITIAL_SLEEP = 300; // Initial speed value
const int INITIAL_SEGMENTS = 5; // Initial snake length
// Direction enum
enum Direction
{
STOP = 0,
LEFT,
RIGHT,
UP,
DOWN
};
// Global variables
vector<pair<int, int>> snake;
int foodX, foodY;
string foodColor; // Store current food color
Direction dir = STOP;
bool gameOver = false;
bool isPaused = false;
double sleepTime = INITIAL_SLEEP; // Sleep time will be modified dynamically
// Add these color codes
const vector<string> rainbowColors = {
"\033[31m", // Red
"\033[33m", // Yellow
"\033[32m", // Green
"\033[36m", // Cyan
"\033[34m", // Blue
"\033[35m" // Magenta
};
// Function prototypes
void InitializeGame();
void Draw();
void Input();
void Logic();
void GenerateFood(string color = "");
void DrawGameOver();
void InputGameOver();
int main()
{
InitializeGame();
while (!gameOver)
{
Draw();
Input();
if (!isPaused)
{
Logic();
Sleep(static_cast<int>(sleepTime));
}
}
DrawGameOver();
return 0;
}
void InitializeGame()
{
snake.clear();
// Create initial snake body segments
for (int i = 0; i < INITIAL_SEGMENTS; i++)
{
snake.push_back({WIDTH / 2 - i, HEIGHT / 2});
}
srand(time(0));
GenerateFood();
gameOver = false;
dir = RIGHT; // Set initial direction to right
sleepTime = INITIAL_SLEEP; // Reset speed for new game
isPaused = false;
}
void Draw()
{
system("cls");
// Draw top border
for (int i = 0; i < WIDTH + 2; i++)
cout << "#";
cout << endl;
// Draw game board
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
if (x == 0)
cout << "#";
bool isSnakePart = false;
for (const auto &segment : snake)
{
if (segment.first == x && segment.second == y)
{
// Find the iterator to the segment
auto it = find(snake.begin(), snake.end(), segment);
if (it != snake.end())
{
// Calculate color index based on position in snake
int colorIndex = (snake.size() - distance(snake.begin(), it)) % rainbowColors.size();
if (&segment == &snake.front())
{
cout << rainbowColors[colorIndex] << "O" << "\033[0m";
foodColor = rainbowColors[colorIndex]; // Update food color to match head
}
else
{
cout << rainbowColors[colorIndex] << "o" << "\033[0m";
}
}
else
{
cout << "O"; // Fallback if not found
}
isSnakePart = true;
break;
}
}
if (!isSnakePart)
{
if (x == foodX && y == foodY)
cout << foodColor << "*" << "\033[0m"; // Use stored food color
else
cout << " ";
}
if (x == WIDTH - 1)
cout << "#";
}
cout << endl;
}
// Draw bottom border and score
for (int i = 0; i < WIDTH + 2; i++)
cout << "#";
cout << endl;
cout << "Score: " << snake.size() - INITIAL_SEGMENTS << endl;
cout << "Current speed: " << (100 + ((INITIAL_SLEEP - sleepTime) * 100.0 / INITIAL_SLEEP)) << "%%" << endl;
// Display pause message
if (isPaused)
{
cout << "********** GAME PAUSED **********" << endl;
cout << "Press Enter to resume" << endl;
}
}
void Input()
{
if (_kbhit())
{
int key = _getch();
// Handle pause/resume with Enter key
if (key == 13)
{ // Enter key
isPaused = !isPaused;
return;
}
// Only process other inputs when not paused
if (isPaused)
return;
// Handle extended keys (arrow keys)
if (key == 0 || key == 224)
{
key = _getch();
switch (key)
{
case 75: // Left arrow
if (dir != RIGHT)
dir = LEFT;
break;
case 77: // Right arrow
if (dir != LEFT)
dir = RIGHT;
break;
case 72: // Up arrow
if (dir != DOWN)
dir = UP;
break;
case 80: // Down arrow
if (dir != UP)
dir = DOWN;
break;
}
}
// Handle 'x' to exit
else if (key == 'x' || key == 'X')
{
gameOver = true;
}
}
}
void Logic()
{
if (dir == STOP)
return;
// Calculate new head position
int newHeadX = snake.front().first;
int newHeadY = snake.front().second;
switch (dir)
{
case LEFT:
newHeadX--;
break;
case RIGHT:
newHeadX++;
break;
case UP:
newHeadY--;
break;
case DOWN:
newHeadY++;
break;
}
// Check wall collision
if (newHeadX < 0 || newHeadX >= WIDTH || newHeadY < 0 || newHeadY >= HEIGHT)
{
gameOver = true;
return;
}
// Check self collision
for (size_t i = 1; i < snake.size(); i++)
{
if (snake[i].first == newHeadX && snake[i].second == newHeadY)
{
gameOver = true;
return;
}
}
// Add new head
snake.insert(snake.begin(), {newHeadX, newHeadY});
// Check food collision
if (newHeadX == foodX && newHeadY == foodY)
{
// Generate new food with the current head color
GenerateFood(foodColor);
// Increase speed by 1.4%
sleepTime = sleepTime * 0.986;
}
// Remove tail if no food eaten
else
{
snake.pop_back();
}
}
void GenerateFood(string color)
{
bool onSnake;
do
{
onSnake = false;
foodX = rand() % WIDTH;
foodY = rand() % HEIGHT;
for (const auto &segment : snake)
{
if (segment.first == foodX && segment.second == foodY)
{
onSnake = true;
break;
}
}
} while (onSnake);
// Set food color to the specified color or use the current head color
if (color.empty())
{
// Calculate new food color based on current head color
auto headColorIndex = (snake.size() - 1) % rainbowColors.size();
foodColor = rainbowColors[headColorIndex];
}
else
{
foodColor = color;
}
}
void DrawGameOver()
{
system("cls");
cout << "********** GAME OVER **********" << endl;
cout << "Your score: " << snake.size() - INITIAL_SEGMENTS << endl;
cout << "Press Enter to play again" << endl;
cout << "Press X to exit" << endl;
InputGameOver();
}
void InputGameOver()
{
while (true)
{
if (_kbhit())
{
int key = _getch();
if (key == 13)
{ // Enter key
InitializeGame();
main(); // Restart the game
}
else if (key == 'x' || key == 'X')
{
exit(0); // Exit the game
}
}
}
}