-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.cpp
332 lines (311 loc) · 6.52 KB
/
Snake.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
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
#include <iostream>
using namespace std;
#include <deque>
#include <conio.h>
#include <thread>
#include <chrono>
#include <ctime>
string SnakeHead = "@";
string SnakeBody = "o";
string Snakefruit="Q";
string background_board=" ";
string board[20][20];
int board_size = sizeof(board) / sizeof(board[0]);
int Snake_size = 1;
int score = 0;
int gamelevel=1;
int speedmultiplier=1;
void start();
struct Snakesegment
{
int pos_x;
int pos_y;
Snakesegment(int x, int y)
{
this->pos_x = x;
this->pos_y = y;
}
};
deque<Snakesegment> snake;
void Draw_Snake()
{
for (const auto segment : snake)
{
if (segment.pos_x == snake.front().pos_x && segment.pos_y == snake.front().pos_y)
{
board[segment.pos_x][segment.pos_y] = SnakeHead;
}
else
board[segment.pos_x][segment.pos_y] = SnakeBody;
}
}
void Create_Fruit(int s)
{
srand(time(0));
int x = rand() % (board_size - 1);
int y = rand() % (board_size - 1);
while (true)
{
if (board[x][y] == background_board)
{
board[x][y] = Snakefruit;
break;
}
else
{
x = rand() % (board_size - 1);
y = rand() % (board_size - 1);
}
}
}
void reset()
{
score=0;
gamelevel=1;
Snake_size=1;
snake.clear();
}
void initialize_Board()
{
reset();
for (int i = 0; i < board_size; i++)
{
for (int j = 0; j < board_size; j++)
{
board[i][j] = background_board;
}
}
Create_Fruit(1);
}
void Display_Board()
{
string Snake_board[20][20];
for (int i = 0; i < board_size; i++)
{
for (int j = 0; j < board_size; j++)
{
Snake_board[i][j] = board[i][j];
}
}
string output;
output += "\n\n\t\t|==============================================|\n";
output += "\t\t| Score: "+to_string(score) +"\t\t\t"+"Level: "+to_string(gamelevel)+" |\n";
output += "\t\t|==============================================|\n";
output += "\t\t|//////////////////////////////////////////////|\n";
for (int i = 0; i < board_size; i++)
{
output += "\t\t|/| ";
for (int j = 0; j < board_size; j++)
{
output +=Snake_board[i][j] + " ";
}
output += " |/|\n";
}
output += "\t\t|//////////////////////////////////////////////|\n";
output += "\t\t|==============================================|";
cout << output;
}
bool isGameOver(int x, int y)
{
if (board[x][y] == Snakefruit)
{
score++;
if(score%5==0)
{
gamelevel++;
speedmultiplier*=2;
}
Snake_size++;
Create_Fruit(1);
return false;
}
else if (board[x][y] == SnakeBody)
{
cout << "\n\n\t Game Over! Your Score: " << score << endl;
chrono::milliseconds duration(2000);
this_thread::sleep_for(duration);
return true;
}
return false;
}
void removebody(int x, int y)
{
board[x][y]=background_board;
}
void snake_position(int x, int y)
{
snake.push_front(Snakesegment(x, y));
if (snake.size() > Snake_size)
{
Snakesegment tail= snake.back();
snake.pop_back();
removebody(tail.pos_x,tail.pos_y);
}
}
int pause()
{
char c;
cout << "\n\nGame Paused";
cout << "\n\n1. Resume \n2. Quit\n";
while (true)
{
c = _getch();
switch (c)
{
case 27:
case '1':
return 0;
case '2':
return 1;
default:
break;
}
}
}
void replay()
{
cout<<"Press any key to continue....."<<endl;
_getch();
cout<<"\nPress 1 to play again "<<endl;
cout<<"\nPress 2 to Quit Game "<<endl;
char temp=getch();
if(temp=='2')
exit(0);
else if(temp=='1')
start();
}
void SnakeMovements(int Head_X, int Head_Y)
{
char lastkey='d';
while (true)
{
if (isGameOver(Head_X, Head_Y))
{
replay();
return;
}
snake_position(Head_X, Head_Y);
Draw_Snake();
system("cls");
Display_Board();
int delay_factor = 200 - ((gamelevel - 1) * 10 *speedmultiplier);
if (delay_factor < 50)
delay_factor = 50;
chrono::milliseconds duration(delay_factor);
this_thread::sleep_for(duration);
if (_kbhit())
{
char key = _getch();
if(key==27) //ESC character
{
switch(pause())
{
case 0:
break;
case 1:
return;
}
}
if(key=='w'|| key=='s'|| key=='a'|| key=='d')
{
if(lastkey=='w'&& key!='s'
|| lastkey=='s'&& key!='w'|| lastkey=='a'&& key!='d'|| lastkey=='d'&& key!='a')
lastkey=key;
}
switch (lastkey)
{
case 'w':
lastkey = 'w';
break;
case 's':
lastkey = 's';
break;
case 'a':
lastkey = 'a';
break;
case 'd':
lastkey = 'd';
break;
default:
break;
}
}
if (lastkey == 'w')
Head_X--;
else if (lastkey == 's')
Head_X++;
else if (lastkey == 'a')
Head_Y--;
else if (lastkey == 'd')
Head_Y++;
if(Head_Y<0)
Head_Y=board_size-1;
if(Head_Y>=board_size)
Head_Y=0;
if(Head_X<0)
Head_X=board_size-1;
if(Head_X>=board_size)
Head_X=0;
}
}
void start()
{
initialize_Board();
int x = board_size / 2;
int y = board_size / 2;
SnakeMovements(x, y);
}
void instructions()
{
while (true)
{
system("cls");
cout << "\n\n<======================= Snake Game ===========================>\n";
cout << "\n Gameplay Instructions\n";
cout << "\n\n\t\tSnake Movement\n";
cout << "\n\t\t Move up\t: W \n";
cout << "\n\t\t Move Down\t: S \n";
cout << "\n\t\t Move Left\t: A \n";
cout << "\n\t\t Move Right\t: D \n";
cout << "\n\t\t Pause\t: Esc \n";
cout << "\n\n\t\t The Snake speed will incease as \n\t\tthe game approaches Higher level\n\n";
cout << "\n\t\tPress Esc to Go Back\n\n";
char c;
c = _getch();
switch (c)
{
case 27:
return;
default:
break;
}
}
}
int main()
{
while (true)
{
system("cls");
cout << "\n\n<======================= Snake Game ===========================>\n";
cout << "\n\n\n\t\t\t1. Start Game";
cout << "\n\n\n\t\t\t2. Game Instruction";
cout << "\n\n\n\t\t\t3. Exit\n\n";
char c;
c = _getch();
switch (c)
{
case '1':
start();
break;
case '2':
instructions();
break;
case '3':
cout << "\n\n\t\tExiting...\n\n";
return 0;
default:
break;
}
}
return 0;
}