-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_game.cpp
More file actions
415 lines (342 loc) · 11.8 KB
/
snake_game.cpp
File metadata and controls
415 lines (342 loc) · 11.8 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// ============================================================
// PREMIUM SNAKE GAME — Console C++
// Uses: arrays, loops, functions, conio.h, windows.h
// ============================================================
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
// ---------------- CONFIG ----------------
const int WIDTH = 50;
const int HEIGHT = 20;
const int MAX_LENGTH = WIDTH * HEIGHT;
const int SPEED_MS = 115;
enum Direction { STOP = 0, UP, DOWN, LEFT, RIGHT };
bool gameOver;
int snakeX[MAX_LENGTH], snakeY[MAX_LENGTH];
int snakeLength;
int foodX, foodY;
int score;
Direction dir;
// ---------------- CONSOLE UTILITIES ----------------
void gotoxy(int x, int y)
{
COORD pos = { (SHORT)x, (SHORT)y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void setColor(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void hideCursor()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(h, &info);
}
// ---------------- PREMIUM HEADER ----------------
void printHeader()
{
setColor(10);
cout << " ============================================================\n";
cout << " ███████╗███╗ ██╗ █████╗ ██╗ ██╗███████╗\n";
cout << " ██╔════╝████╗ ██║██╔══██╗██║ ██╔╝██╔════╝\n";
cout << " ███████╗██╔██╗ ██║███████║█████╔╝ █████╗ \n";
cout << " ╚════██║██║╚██╗██║██╔══██║██╔═██╗ ██╔══╝ \n";
cout << " ███████║██║ ╚████║██║ ██║██║ ██╗███████╗\n";
cout << " ╚══════╝╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝\n";
cout << "\n";
cout << " ██████╗ █████╗ ███╗ ███╗███████╗\n";
cout << " ██╔════╝ ██╔══██╗████╗ ████║██╔════╝\n";
cout << " ██║ ███╗███████║██╔████╔██║█████╗ \n";
cout << " ██║ ██║██╔══██║██║╚██╔╝██║██╔══╝ \n";
cout << " ╚██████╔╝██║ ██║██║ ╚═╝ ██║███████╗\n";
cout << " ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝\n";
cout << " ============================================================\n";
setColor(7);
}
// ---------------- FOOD ----------------
void placeFood()
{
bool invalid = true;
while (invalid)
{
foodX = rand() % WIDTH;
foodY = rand() % HEIGHT;
invalid = false;
for (int i = 0; i < snakeLength; i++)
{
if (snakeX[i] == foodX && snakeY[i] == foodY)
{
invalid = true;
break;
}
}
}
}
// ---------------- SETUP ----------------
void setup()
{
gameOver = false;
dir = STOP;
score = 0;
snakeLength = 4;
snakeX[0] = WIDTH / 2;
snakeY[0] = HEIGHT / 2;
for (int i = 1; i < snakeLength; i++)
{
snakeX[i] = snakeX[0] - i;
snakeY[i] = snakeY[0];
}
placeFood();
}
// ---------------- START SCREEN ----------------
void showStartScreen()
{
system("cls");
printHeader();
setColor(11);
cout << "\n";
cout << " ╔══════════════════════════════════════════════════════════╗\n";
cout << " ║ PREMIUM CONSOLE EDITION ║\n";
cout << " ╠══════════════════════════════════════════════════════════╣\n";
cout << " ║ Objective : Eat food, grow longer, avoid collisions ║\n";
cout << " ║ Controls : W A S D or Arrow Keys ║\n";
cout << " ║ Quit : Q ║\n";
cout << " ╠══════════════════════════════════════════════════════════╣\n";
cout << " ║ Press any key to start the game ║\n";
cout << " ╚══════════════════════════════════════════════════════════╝\n";
setColor(7);
_getch();
system("cls");
}
// ---------------- DRAW GAME ----------------
void draw()
{
gotoxy(0, 0);
setColor(11);
cout << " ╔══════════════════════════════════════════════════════╗\n";
cout << " ║ SNAKE GAME ║\n";
cout << " ╠══════════════════════════════════════════════════════╣\n";
cout << " ║ Objective : Eat food, grow longer, avoid collisions ║\n";
cout << " ╠══════════════════════════════════════════════════════╣\n ║";
setColor(14);
cout << " SCORE: ";
cout.width(5);
cout << left << score;
cout << " LENGTH: ";
cout.width(5);
cout << left << snakeLength;
cout << " MOVE: W/A/S/D QUIT: Q " << " ";
setColor(11);
cout << "║\n";
cout << " ╚══════════════════════════════════════════════════════╝\n";
setColor(7);
cout << " ╔";
for (int i = 0; i < WIDTH; i++) cout << "═";
cout << "╗\n";
for (int y = 0; y < HEIGHT; y++)
{
cout << " ║";
for (int x = 0; x < WIDTH; x++)
{
bool printed = false;
if (x == snakeX[0] && y == snakeY[0])
{
setColor(10);
cout << "O";
setColor(7);
printed = true;
}
if (!printed)
{
for (int i = 1; i < snakeLength; i++)
{
if (x == snakeX[i] && y == snakeY[i])
{
setColor(10);
cout << "o";
setColor(7);
printed = true;
break;
}
}
}
if (!printed && x == foodX && y == foodY)
{
setColor(12);
cout << "*";
setColor(7);
printed = true;
}
if (!printed) cout << " ";
}
cout << "║\n";
}
cout << " ╚";
for (int i = 0; i < WIDTH; i++) cout << "═";
cout << "╝\n";
cout << " Tip: Instant reverse movement is blocked. \n";
}
// ---------------- INPUT ----------------
void handleInput()
{
if (!_kbhit()) return;
int key = _getch();
if (key == 0 || key == 224)
{
key = _getch();
switch (key)
{
case 72: if (dir != DOWN) dir = UP; break;
case 80: if (dir != UP) dir = DOWN; break;
case 75: if (dir != RIGHT) dir = LEFT; break;
case 77: if (dir != LEFT) dir = RIGHT; break;
}
}
else
{
switch (key)
{
case 'w': case 'W': if (dir != DOWN) dir = UP; break;
case 's': case 'S': if (dir != UP) dir = DOWN; break;
case 'a': case 'A': if (dir != RIGHT) dir = LEFT; break;
case 'd': case 'D': if (dir != LEFT) dir = RIGHT; break;
case 'q': case 'Q': gameOver = true; break;
}
}
}
// ---------------- UPDATE LOGIC ----------------
void update()
{
if (dir == STOP) return;
for (int i = snakeLength - 1; i > 0; i--)
{
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
switch (dir)
{
case UP: snakeY[0]--; break;
case DOWN: snakeY[0]++; break;
case LEFT: snakeX[0]--; break;
case RIGHT: snakeX[0]++; break;
default: break;
}
if (snakeX[0] < 0 || snakeX[0] >= WIDTH ||
snakeY[0] < 0 || snakeY[0] >= HEIGHT)
{
gameOver = true;
return;
}
for (int i = 1; i < snakeLength; i++)
{
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i])
{
gameOver = true;
return;
}
}
if (snakeX[0] == foodX && snakeY[0] == foodY)
{
score += 10;
if (snakeLength < MAX_LENGTH)
snakeLength++;
placeFood();
}
}
// ---------------- GAME OVER ----------------
void showGameOver()
{
system("cls");
printHeader();
const int BOX_WIDTH = 58; // total inner width
setColor(12);
cout << "\n";
// Top border
cout << " ╔";
for(int i = 0; i < BOX_WIDTH; i++) cout << "═";
cout << "╗\n";
// Title
string title = "GAME OVER";
int padding = (BOX_WIDTH - title.length()) / 2;
cout << " ║";
for(int i = 0; i < padding; i++) cout << " ";
cout << title;
for(int i = 0; i < BOX_WIDTH - padding - title.length(); i++) cout << " ";
cout << "║\n";
// Divider
cout << " ╠";
for(int i = 0; i < BOX_WIDTH; i++) cout << "═";
cout << "╣\n";
// Score line
string scoreLine = "Final Score : " + to_string(score);
cout << " ║ " << scoreLine;
for(int i = 0; i < BOX_WIDTH - scoreLine.length() - 1; i++) cout << " ";
cout << "║\n";
// Length line
string lenLine = "Snake Length : " + to_string(snakeLength);
cout << " ║ " << lenLine;
for(int i = 0; i < BOX_WIDTH - lenLine.length() - 1; i++) cout << " ";
cout << "║\n";
// Divider
cout << " ╠";
for(int i = 0; i < BOX_WIDTH; i++) cout << "═";
cout << "╣\n";
// Controls
string control = "[R] Restart [Q] Quit";
padding = (BOX_WIDTH - control.length()) / 2;
cout << " ║";
for(int i = 0; i < padding; i++) cout << " ";
cout << control;
for(int i = 0; i < BOX_WIDTH - padding - control.length(); i++) cout << " ";
cout << "║\n";
// Bottom border
cout << " ╚";
for(int i = 0; i < BOX_WIDTH; i++) cout << "═";
cout << "╝\n";
setColor(7);
}
// ---------------- MAIN ----------------
int main()
{
srand((unsigned)time(0));
hideCursor();
showStartScreen();
bool running = true;
while (running)
{
setup();
while (!gameOver)
{
handleInput();
update();
draw();
Sleep(SPEED_MS);
}
showGameOver();
while (true)
{
char ch = _getch();
if (ch == 'r' || ch == 'R')
{
system("cls");
break;
}
if (ch == 'q' || ch == 'Q')
{
running = false;
break;
}
}
}
system("cls");
setColor(10);
cout << "\n Thanks for playing Snake Game!\n\n";
setColor(7);
return 0;
}