-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.c
372 lines (329 loc) · 7.01 KB
/
game.c
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
#pragma once
#include "collision.h"
#include "basic.h"
#include "fruit.h"
#include "game.h"
#include "map.h"
#include "queue.h"
#include "snake.h"
extern const int MAP_SIZE;
extern const int DEFAULT_X;
extern const int DEFAULT_Y;
extern const int NORMAL;
extern const int RIGHT;
extern const int LEFT;
extern const int DOWN;
extern const int UP;
extern const int LIGHTGREEN;
extern const int LIGHTRED;
extern const int YELLOW;
extern const int WHITE;
extern const int LIGHTGRAY;
/*
getKeyDown function is checking keyboard input through _kbhit()
return value is keyboard input
*/
int getKeyDown(void)
{
if (_kbhit())
{
return _getch();
}
return -1;
}
/*
Console cursor move to (x,y)
*/
void gotoxy(int x, int y)
{
COORD Pos;
Pos.X = (short)(2 * x);
Pos.Y = (short)y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
/*
Hide cursor before game start
*/
void hidecursor(void)
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
/*
Ignore input key that reverse to peviousKey
*/
int isOverlap(int previousKey, int key)
{
if (previousKey == UP && key == DOWN)
{
return TRUE;
}
if (previousKey == DOWN && key == UP)
{
return TRUE;
}
if (previousKey == LEFT && key == RIGHT)
{
return TRUE;
}
if (previousKey == RIGHT && key == LEFT)
{
return TRUE;
}
return FALSE;
}
/*
Delete all queue and record best score
When game ends, call this function
*/
void Game_GameOver(int mode, int score, int best, Queue *pq, int stage, int * scoreArr)
{
HANDLE hand = GetStdHandle(STD_OUTPUT_HANDLE);
FILE * wfp;
int errCode;
if (mode == 1)
{
if (score >= best)
{
scoreArr[stage - 1] = score;
}
else
{
scoreArr[stage - 1] = best;
}
}
else
{
if (score >= best)
{
scoreArr[stage - 1 + 4] = score;
}
else
{
scoreArr[stage - 1 + 4] = best;
}
}
errCode = fopen_s(&wfp, "score.txt", "w");
fprintf(wfp, "%d %d %d %d %d %d %d %d", scoreArr[0], scoreArr[1], scoreArr[2], scoreArr[3], scoreArr[4], scoreArr[5], scoreArr[6], scoreArr[7]);
fclose(wfp);
SetConsoleTextAttribute(hand, YELLOW);
gotoxy(MAP_SIZE / 2 - 4, MAP_SIZE / 2 - 5);
printf("===<GAME OVER>===\n");
gotoxy(MAP_SIZE / 2 - 3, MAP_SIZE / 2 - 3);
printf("Your Score : %d\n", score);
gotoxy(DEFAULT_X + 8, MAP_SIZE + 5);
printf("\n");
SetConsoleTextAttribute(hand, LIGHTGRAY);
// Delete all queue
while (!isEmpty(pq))
{
Dequeue(pq);
}
}
/*
Main game loop{ 22 = MAP_SIZE }
*/
void Game_Start(MapData map[22][22], int stage, int * scoreArr, int mode)
{
HANDLE hand = GetStdHandle(STD_OUTPUT_HANDLE);
SnakePos snakeHead = { 22 / 4 - 2, 22 / 4 + 1 };
SnakePos snakeNeck;
SnakePos snakeTail;
FruitPos fruit;
Queue queue;
int previousKey = 0;
int key = 0;
int bestScore = 0;
int score = 0;
unsigned int repeatTimes = 0;
//120000 = 120sec
double wholeTime = 120000;
double refreshInterval = 1200;
int innerTimer = 0;
// special fruit exist = 1 , nonexist = 0
int specialFruit = FALSE;
// special fruit appear time = 1
int specialTime = FALSE;
int removeTail = FALSE;
QueueInit(&queue);
fruit.numOfFruit = 0;
//Choose best score array
if (mode == 1)
{
bestScore = scoreArr[stage - 1];
}
else
{
bestScore = scoreArr[stage - 1 + 4];
}
// Stage init
if (stage == 1)
{
Map_GamemapInitStage1(map);
}
else if (stage == 2)
{
Map_GamemapInitStage2(map);
}
else if (stage == 3)
{
Map_GamemapInitStage3(map);
}
else
{
Map_GamemapInitStage4(map);
}
Map_GamemapDrawWall(map);
Game_PlayDrawHead(map, snakeHead.x, snakeHead.y);
while (key != 't')
{
//Console refresh
// 1200 / 10 = 0.12sec
Sleep(refreshInterval / (DWORD)NORMAL);
innerTimer++;
//Minimal interval => 500
if (refreshInterval >= 500)
{
if ((refreshInterval * innerTimer) >= 150000)
{
refreshInterval -= 150;
innerTimer = 0;
specialTime = TRUE;
}
}
// Draw fruit
if (specialTime == TRUE)
{
// If special fruit nonexist
if (specialFruit == FALSE)
{
// If normal fruit exist
if (fruit.numOfFruit == 1)
{
// Normal fruit delete
Game_RemoveFruit(map, &fruit);
}
// Make set special fruit
Game_DrawFruit(map, &fruit, LIGHTRED);
specialFruit = TRUE;
}
else
{
if ((refreshInterval * innerTimer) >= 50000)
{
Game_RemoveFruit(map, &fruit);
specialFruit = FALSE;
specialTime = FALSE;
}
}
}
if (fruit.numOfFruit == 0)
{
Game_DrawFruit(map, &fruit, LIGHTGREEN);
}
Map_GamemapDrawScoreboard(score, bestScore, stage);
//Checking collision between snake and fruit
if (isColWithFruit(&snakeHead, &fruit) == TRUE)
{
(fruit.numOfFruit)--;
// INdicates that the tail collides
removeTail = FALSE;
if (specialFruit)
{
score += 10;
specialFruit = FALSE;
specialTime = FALSE;
}
else
{
score += 5;
}
}
// Wait for keyboard input
while (previousKey == 0)
{
if (_kbhit() != 0 && _getch() == 224)
{
previousKey = _getch();
key = previousKey;
break;
}
}
//If key input exist
if (_kbhit() != 0)
{
key = _getch();
//Game exit
if (key == 't' || key == 'T')
{
return;
}
//Game pause
else if (key == 'p' || key == 'P')
{
system("pause");
//Delete "press any key to continue..."
gotoxy(DEFAULT_X, MAP_SIZE + 6);
printf(" ");
gotoxy(DEFAULT_X, DEFAULT_Y);
}
// If key is arrowkey
else if (key == 224)
{
key = _getch();
// Reverse key check
if (isOverlap(previousKey, key) == TRUE)
{
key = previousKey;
}
}
//If key is not t,p,arrow
else
{
key = previousKey;
}
}
///////////// Snake Move Section
// Save head position
snakeNeck = snakeHead;
previousKey = Game_PlayMoveSnake(map, &snakeHead, key);
Enqueue(&queue, snakeNeck);
Game_PlayDrawTail(map, snakeNeck.x, snakeNeck.y);
// If snake can't eat fruit
if (removeTail == TRUE)
{
snakeTail = Dequeue(&queue);
Game_PlayRemoveTail(map, snakeTail.x, snakeTail.y);
}
//If snake eat fruit
else
{
removeTail = TRUE;
}
// Checking collision (wall, tail)
if (isCollision(previousKey))
{
Game_GameOver(mode, score, bestScore, &queue, stage, scoreArr);
return;
}
//Time limit mode
if (mode == 2)
{
wholeTime = wholeTime - (refreshInterval / 10);
SetConsoleTextAttribute(hand, WHITE);
gotoxy(DEFAULT_X, DEFAULT_Y + 25);
printf("%.1lf", wholeTime / 1000);
if (wholeTime <= 0.0)
{
gotoxy(1, 1);
printf(" > Time Over ");
Game_GameOver(mode, score, bestScore, &queue, stage, scoreArr);
return;
}
}
repeatTimes++;
}
}