-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake_game.cpp
330 lines (298 loc) · 7.97 KB
/
Snake_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
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
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <string.h>
#include <fstream>
#include <sstream>
using namespace std;
// global variables
bool gameOver;
const int width = 40;
const int height = 20;
int x, y, frogX, frogY, score;
int tailX[100], tailY[100];
int nTail;
string name;
int style;
// creating a datatype to set the direction of motion
enum movement
{
stop = 0,
Left,
Right,
upward,
downward
} direction;
// creating structure to store values for printing it on screen
struct HighestScore
{
int bestscore;
string bestplayer;
} data; //creating variable
// printing introduction
void structure()
{
cout << " Enter your name : ";
cin >> name;
cout << "\n\n";
cout << " Would style would you like to play ?" <<endl;;
cout << " Traditional [press 1]" <<endl;
cout << " Free [press2]\n" <<endl;
cout << " Command : ";
cin >> style;
}
// putting the default highscore when the file is empty
void firstsave()
{
fstream txtfile("Snake_game.txt");
if (txtfile.peek() == EOF) // checking if file is empty
{
if (txtfile.is_open())
{
txtfile << 0 << endl;
txtfile << "Gyan" << endl;
data.bestscore = 0; // updating struct members
data.bestplayer = "Gyan";
}
else
{
cout << "File can't be opened" << endl;
}
}
else
{
txtfile >> data.bestscore;
txtfile >> data.bestplayer;
}
txtfile.close();
}
// initiating the global variables
void start()
{
gameOver = false;
direction = stop;
x = width / 2;
y = height / 2;
srand(time(0)); // generating a frog at a random position
frogX = rand() % width;
frogY = rand() % height;
score = 0;
}
// hidind the cursor
void ShowConsoleCursor(bool showFlag)
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); // storing the handle of std output stream
// in out variable
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo); // getting the cursor info and storing it in cursorinfo
cursorInfo.bVisible = showFlag; // set the cursor visibility according to
// the value in showflag
SetConsoleCursorInfo(out, &cursorInfo); // updating the cursor info in out
}
// building the interface of the game
void Build()
{
// clearing the console and setting cursor position to top left
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
for (int i = 0; i < width + 2; i++) // the upper boundary
{
cout << "+";
}
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
{
cout << "+"; // left boundary
}
if (i == y && j == x)
{
cout << "o"; // snake head
}
else if (i == frogY && j == frogX)
{
cout << "*"; // frog
}
else
{
bool print = false;
for (int k = 0; k < nTail; k++) // snake tail
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
{
cout << " ";
}
}
if (j == width - 1) // right boundary
{
cout << "+";
}
}
cout << endl;
}
for (int i = 0; i < width + 2; i++) // lower boundary
{
cout << "+";
}
cout << "\n\n";
cout << " Score : " << score << "\n\n"; // displaying score
cout << " Highest score : " << data.bestscore << " [" << data.bestplayer << "]";
}
// taking the direction of motion as input
void Input()
{
if (_kbhit()) // to check if any key is pressed
{
switch (_getch())
{
case 'a':
direction = Left;
break;
case 'd':
direction = Right;
break;
case 'w':
direction = upward;
break;
case 's':
direction = downward;
break;
case 'x':
gameOver = true;
break;
}
}
}
// controlling the movement of the snake
void Logic()
{
for (int i = nTail; i > 0; i--) // updating the position of each part
{ // of the snake's body
tailX[i] = tailX[i - 1];
tailY[i] = tailY[i - 1];
}
tailX[0] = x;
tailY[0] = y;
switch (direction) // changing the direction according to input
{
case Left:
x--;
break;
case Right:
x++;
break;
case upward:
y--;
break;
case downward:
y++;
break;
default:
break;
}
if (style == 1) {
if (x > width || x < 0 || y > height || y < 0) { // to end game when snake comes
gameOver = true; // in contact with the boundary
}
}
else if (style == 2) {
if (x >= width) // to continue from opposite side when
x = 0; // snake collides with the boundary
else if (x < 0)
x = width - 1;
if (y >= height)
y = 0;
else if (y < 0)
y = height - 1;
}
for (int i = 0; i < nTail; i++) // to check if snakes head (x,y) is not
if (tailX[i] == x && tailY[i] == y) // coinciding with it's body
gameOver = true;
if (x == frogX && y == frogY)
{
score += 10; // updating score
frogX = rand() % width; // regenerating frog at a random position
frogY = rand() % height;
nTail++; // updating number of tails
}
}
// updating the highest score in the snake_game.txt file
void savescore()
{
string x;
int i = 0;
bool change = false;
int oldscoreInt;
fstream txtfile("Snake_game.txt"); // opening the file
if (txtfile.is_open())
{
while (getline(txtfile, x))
{ // reading each line
if (i % 2 == 0)
{
stringstream convert(x); // converting string to int for comparison
convert >> oldscoreInt;
if (oldscoreInt <= score)
{
// if score is higher than highest score
string scoreline = to_string(score) + "\n";
x = scoreline;
data.bestscore = score;
change = true;
}
}
else
{
if (change == true) // replacing name
{
x = name + "\n";
data.bestplayer = name;
}
}
i++;
}
txtfile.close(); // closing the file
// reopen the file in write mode and write the updated scores and names back to the file
fstream txtfile2("Snake_game.txt", ios::out | ios::trunc);
if (txtfile2.is_open())
{
txtfile2 << data.bestscore << endl;
txtfile2 << data.bestplayer << endl;
txtfile2.close(); // closing the file
}
else
{
cout << "Error: Unable to open file for writing." << endl;
}
txtfile2.close();
}
else
{
cout << " File is empty..." << endl;
}
}
int main()
{
structure();
firstsave();
start();
while (!gameOver)
{
ShowConsoleCursor(false);
Build();
Input();
Logic();
Sleep(100);
}
savescore();
return 0;
}