forked from Rustam-Z/cpp-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake-game.cpp
More file actions
297 lines (263 loc) · 5.99 KB
/
Copy pathsnake-game.cpp
File metadata and controls
297 lines (263 loc) · 5.99 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
#include <iostream>
#include <string>
#include <conio.h>
#include <windows.h>
using namespace std;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
void welcome();
void instructions();
void record();
void incorrectChoice();
void lostgame();
void win();
//game initial settings
bool game = false;
const int width = 50;
const int height = 20;
//variable for snake position and fruit position and also score
int x, y, fx, fy, score;
// tails array
int tx[100], ty[100];
int nt;
enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN };
Direction dir;
//Initial values when game start
void Init() {
game = true;
dir = STOP;
x = width / 2;
y = height / 2;
fx = rand() % width;
fy = rand() % height;
score = 0;
tx[100] = {};
ty[100] = {};
}
//Creating a map container where snake and fruit appears
void Map() {
system("cls");
SetConsoleTextAttribute(hConsole, 180);
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else if (i == fy && j == fx)
cout << "X";
else
{
bool print = false;
for (int k = 0; k < nt; k++) {
if (tx[k] == j && ty[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
cout << " ";
}
if (j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
cout << "Score is " << score << endl;
}
// managing input of keyboard to control snake
void Input() {
if (_kbhit())
{
switch (_getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
game = false;
break;
case 'r':
dir = STOP;
welcome();
default:
break;
}
}
}
// logic how snake should grow in size and when game stops
void Logic() {
int prevx = tx[0];
int prevy = ty[0];
int prev2x, prev2y;
tx[0] = x;
ty[0] = y;
for (int i = 1; i < nt; i++) {
prev2x = tx[i];
prev2y = ty[i];
tx[i] = prevx;
ty[i] = prevy;
prevx = prev2x;
prevy = prev2y;
}
switch (dir)
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
default:
break;
}
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;
for (int i = 0; i < nt; i++) {
if (tx[i] == x && ty[i] == y) {
lostgame();
}
}
if (x == fx && y == fy) {
score += 1;
nt++;
fx = rand() % width;
fy = rand() % height;
}
}
//condition when a player wins
void win() {
if (score == 100) {
system("cls");
cout << endl << endl << endl << endl << endl;
cout << " ";
cout << "Congratulations you won the game " << endl;
cout << endl;
cout << " ";
cout << "To go back please enter any key" << endl;
char back;
cin >> back;
if (back) {
welcome();
game = false;
}
}
}
//starting game
void startGame() {
Init();
while (game) {
Map();
Input();
Logic();
win();
Sleep(10);
}
}
void welcome() {
system("cls");
cout << endl << endl << endl << endl << endl << endl << endl;
cout << " ";
cout << "Welcome to the snake game!" << endl;
cout << " " << "1. If you want to start game press 1" << endl;
cout << " " << "2. To see instruction press 2" << endl;
cout << " " << "3. To see record press 3" << endl;
char choice;
cin >> choice;
switch (choice) {
case '1':
system("cls");
startGame();
case '2':
instructions();
case '3':
record();
default:
incorrectChoice();
}
}
void instructions() {
system("cls");
cout << endl << endl << endl << endl << endl << endl << endl;
cout << " ";
cout << "Welcome to the snake game!" << endl;
cout << endl;
cout << " " << "Here are instructions" << endl;
cout << " " << "1. If you reach current record you won the game" << endl;
cout << endl;
cout << " " << "2. When you grow up in size you cannot touch yourself, " << endl << " " << "if you touch you will lose the game" << endl;
cout << endl;
cout << " " << "3. When you grow up in size you cannot touch yourself," << endl << " " << "if you move backwards when you body is backward" << endl << " " << "you automatically lose the game" << endl;
cout << endl;
char back;
cout << " " << "To go back press any key" << endl;
cin >> back;
if (back) welcome();
}
void record() {
system("cls");
cout << endl << endl << endl << endl << endl << endl << endl;
cout << " ";
cout << "Welcome to the snake game!" << endl;
cout << endl;
cout << " " << "Current record is 100" << endl;
cout << endl;
cout << " " << "If you reach 100 you will win the game" << endl;
char back;
cout << endl;
cout << " " << "To go back press any key" << endl;
cin >> back;
if (back) welcome();
}
void incorrectChoice() {
cout << "Please try again next time" << endl;
cout << endl;
char back;
cout << "To go back press any key" << endl;
cin >> back;
if (back) welcome();
}
void lostgame() {
game = false;
system("cls");
cout << endl << endl << endl << endl << endl << endl << endl;
cout << " ";
cout << "Sorry you lost the game " << endl;
cout << " " << "Your score was " << score << endl;
cout << " ";
cout << "To to back press any button" << endl;
char back;
cin >> back;
if (back) {
game = false;
welcome();
}
}
int main() {
SetConsoleTextAttribute(hConsole, 116);
welcome();
return 0;
}