-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnack.cpp
391 lines (349 loc) · 7.59 KB
/
snack.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
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
/*
Snake Game ""
*/
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <queue>
using namespace std;
char buffer[80][24];
//0 = null
//1 = menu
//2 = play
//3 = high score
//4 = about
//5 = Game over
int score;
int scene = 0;
int currentMenu = 0;
int inGame = 0;
//0 = north
//1 = south
//2 = east
//3 = west
int direction = 0;
string Menulist[4] = {"> play <", "high_score", "about", "exit"};
struct snakeBody {
int x;
int y;
};
queue<snakeBody> snake;
void render(){
string temp;
system("cls");
for(int y = 0; y <= 23; y++){
for(int x = 0; x <= 79; x++){
temp += buffer[x][y];
//cout << buffer[x][y];
}
}
cout << temp;
}
void drawBox(int x, int y, int hight, int width, char bg){
//make border
//head and bottom
for(int n = x + 1; n <= x + width - 1; n++){
buffer[n][y] = 205;
buffer[n][y + hight] = 205;
}
buffer[x][y] = 201;
buffer[x][y + hight] = 200;
buffer[x + width][y] = 187;
buffer[x + width][y + hight] = 188;
//mid
for(int n = y + 1; n <= y + hight - 1; n++){
buffer[x][n] = 186;
buffer[x + width][n] = 186;
}
//draw BG
for(int n = y + 1; n <= y + hight - 1; n++){
for(int n2 = x + 1; n2 <= x + width - 1; n2++){
buffer[n2][n] = bg;
}
}
}
void intGraphic(){
//draw box
//head and bottom
for(int n = 0; n <= 79; n++){
buffer[n][0] = 223;
buffer[n][23] = 220;
}
buffer[0][0] = 219;
buffer[79][0] = 219;
buffer[0][23] = 219;
buffer[79][23] = 219;
//mid
for(int n = 1; n <= 22; n++){
buffer[0][n] = 219;
for(int n2 = 1; n2 <= 78; n2++){
buffer[n2][n] = 250;
}
buffer[79][n] = 219;
}
}
void drawMenu(){
//make border
//head and bottom
for(int n = 23; n <= 55; n++){
buffer[n][8] = 205;
buffer[n][15] = 205;
}
buffer[23][8] = 201;
buffer[23][15] = 200;
buffer[56][8] = 187;
buffer[56][15] = 188;
//mid
for(int n = 9; n <= 14; n++){
buffer[23][n] = 186;
buffer[56][n] = 186;
}
//drawbox 6*32
for(int n = 9; n <= 14; n++){
for(int n2 = 24; n2 <= 55; n2++){
buffer[n2][n] = ' ';
}
}
//find mid point & print menu
for(int n = 10; n <= 13; n++){
int mid = (int)((32 - Menulist[n-10].length()) / 2) + 24;
for(int n2 = 0; n2 <= Menulist[n-10].length() - 1; n2++){
buffer[mid + n2][n] = (Menulist[n-10])[n2];
}
}
}
void moveSnack(snakeBody nextMove, int addNew);
void update(){
//menu update
if(scene == 1){
if(GetAsyncKeyState(VK_UP) != 1 && GetAsyncKeyState(VK_UP) != 0){
//play sound
cout << "\a";
//scroll manu up
if(currentMenu == 0){
currentMenu = 3;
Menulist[0] = "play";
Menulist[3] = "> exit <";
} else if(currentMenu == 1){
currentMenu = 0;
Menulist[1] = "high_score";
Menulist[0] = "> play <";
} else if(currentMenu == 2){
currentMenu = 1;
Menulist[2] = "about";
Menulist[1] = "> high_score <";
} else if(currentMenu == 3){
currentMenu = 2;
Menulist[3] = "exit";
Menulist[2] = "> about <";
}
drawMenu();
} else if(GetAsyncKeyState(VK_DOWN) != 1 && GetAsyncKeyState(VK_DOWN) != 0){
//play sound
cout << "\a";
//scroll manu down
if(currentMenu == 0){
currentMenu = 1;
Menulist[0] = "play";
Menulist[1] = "> high_score <";
} else if(currentMenu == 1){
currentMenu = 2;
Menulist[1] = "high_score";
Menulist[2] = "> about <";
} else if(currentMenu == 2){
currentMenu = 3;
Menulist[2] = "about";
Menulist[3] = "> exit <";
} else if(currentMenu == 3){
currentMenu = 0;
Menulist[3] = "exit";
Menulist[0] = "> play <";
}
drawMenu();
} else if(GetAsyncKeyState(VK_RETURN) != 1 && GetAsyncKeyState(VK_RETURN) != 0){
//play sound
cout << "\a";
if(currentMenu == 0){
scene = 2;
}else if(currentMenu == 1){
scene = 3;
}else if(currentMenu == 2){
scene = 4;
return;
}else if(currentMenu == 3){
exit(1);
}
}
}
//play scene
if(scene == 2 && inGame == 0){
//clear screen
intGraphic();
for(int n = 1; n <= 22; n++){
for(int n2 = 1; n2 <= 78; n2++){
buffer[n2][n] = ' ';
}
}
//clear score
score = 0;
string text = "Score : 0000";
for(int n = 67; n <= 78; n++){
buffer[n][0] = text[n-67];
}
buffer[66][0] = 35;
//make starter snake
snakeBody temp;
for(int n = 35; n <= 38; n++){
temp.x = n;
temp.y = 13;
snake.push(temp);
}
temp.x = 38;
temp.y = 13;
moveSnack(temp, 0);
direction = 2;
//random ball
buffer[rand() % 78 + 1][rand() % 22 + 1] = 15;
inGame = 1;
}else if(scene == 2 && inGame == 1){
snakeBody next;
//check key
if(GetAsyncKeyState(VK_UP) && direction != 1){
direction = 0;
}else if(GetAsyncKeyState(VK_DOWN) && direction != 0){
direction = 1;
}else if(GetAsyncKeyState(VK_RIGHT) && direction != 3){
direction = 2;
}else if(GetAsyncKeyState(VK_LEFT) && direction != 2){
direction = 3;
}
//move snake
if(direction == 0){
next.x = snake.back().x;
next.y = snake.back().y - 1;
moveSnack(next, 0);
}else if(direction == 1){
next.x = snake.back().x;
next.y = snake.back().y + 1;
moveSnack(next, 0);
}else if(direction == 2){
next.x = snake.back().x + 1;
next.y = snake.back().y;
moveSnack(next, 0);
}else if(direction == 3){
next.x = snake.back().x - 1;
next.y = snake.back().y;
moveSnack(next, 0);
}
}
//about
if(scene == 4){
//clear screen
intGraphic();
//check key prass
if(GetAsyncKeyState(VK_RETURN) != 1 && GetAsyncKeyState(VK_RETURN) != 0){
scene = 1;
currentMenu = 0;
intGraphic();
drawMenu();
return;
}
//make border
//head and bottom
for(int n = 23; n <= 55; n++){
buffer[n][7] = 205;
buffer[n][16] = 205;
}
buffer[23][7] = 201;
buffer[23][16] = 200;
buffer[56][7] = 187;
buffer[56][16] = 188;
//mid
for(int n = 8; n <= 15; n++){
buffer[23][n] = 186;
buffer[56][n] = 186;
}
//drawbox 6*32
for(int n = 8; n <= 15; n++){
for(int n2 = 24; n2 <= 55; n2++){
buffer[n2][n] = ' ';
}
}
//find mid point & print about
string about[3] = {"Snakey", "By", "KorlaMarch"};
for(int n = 10; n <= 12; n++){
int mid = (int)((32 - about[n-10].length()) / 2) + 24;
for(int n2 = 0; n2 <= about[n-10].length() - 1; n2++){
buffer[mid + n2][n] = (about[n-10])[n2];
}
}
buffer[35][10] = 273;
buffer[44][10] = 272;
buffer[33][12] = 270;
buffer[46][12] = 270;
string printtext = "Enter to back";
for(int n2 = 0; n2 <= printtext.length() - 1; n2++){
buffer[43 + n2][8] = printtext[n2];
}
}
}
void snakeEatEvent(){
//makeNewBall
while(1){
int x = rand() % 78 + 1;
int y = rand() % 22 + 1;
if(buffer[x][y] != 'O'){
buffer[x][y] = 15;
break;
}
}
//update score
score += 10;
buffer[78][0] = score % 10 + 48;
buffer[77][0] = ((score % 100) - (score % 10)) / 10 + 48;
buffer[76][0] = ((score % 1000)- (score % 100) - (score % 10)) / 100 + 48;
buffer[75][0] = ((score % 10000) - (score % 1000) - (score % 100) - (score % 10)) / 1000 + 48;
}
void moveSnack(snakeBody nextMove, int addNew){
//front = tail
//back = head
queue<snakeBody> Tempsnack;
snake.push(nextMove);
//check death
if(buffer[nextMove.x][nextMove.y] != ' '){
if(buffer[nextMove.x][nextMove.y] != 15){
scene = 5;
return;
}else{
addNew = 1;
//play sound
cout << "\a";
snakeEatEvent();
}
}
//remove old snake tail
if(addNew != 1){
buffer[snake.front().x][snake.front().y] = ' ';
snake.pop();
}
while(!snake.empty()){
//pop out and render
buffer[snake.front().x][snake.front().y] = 'O';
Tempsnack.push(snake.front());
snake.pop();
}
//return old snake value
snake = Tempsnack;
}
int main() {
system("mode 80, 25");
intGraphic();
drawMenu();
scene = 1;
while(true){
update();
render();
Sleep(100);
}
return 1;
}