-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
280 lines (244 loc) · 6.4 KB
/
test.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <unistd.h>
#define GAMESCREEN_WIDTH 50
#define GAMESCREEN_HEIGHT 10
struct SnakeGame
{
int vel_x;
int vel_y;
int food_x;
int food_y;
int score;
int running;
int length;
int top;
int snakeBody[GAMESCREEN_WIDTH * GAMESCREEN_HEIGHT][2];
};
int temp;
struct SnakeGame snakeGame;
char inputCh;
char snakeChar = '0';
char backgroundChar = ' ';
char borderChar = 'X';
char foodChar = 'a';
void putFood()
{
snakeGame.food_x = (rand() % (GAMESCREEN_WIDTH - 2)) + 1;
snakeGame.food_y = (rand() % (GAMESCREEN_HEIGHT - 2)) + 1;
}
void initialize()
{
// Initialize all values of the array
for (int i = 0; i < (GAMESCREEN_HEIGHT * GAMESCREEN_WIDTH); i++)
{
snakeGame.snakeBody[i][0] = -1;
snakeGame.snakeBody[i][1] = -1;
}
snakeGame.score = 0;
snakeGame.snakeBody[0][0] = (int)GAMESCREEN_WIDTH / 2;
snakeGame.snakeBody[0][1] = (int)GAMESCREEN_HEIGHT / 2;
snakeGame.snakeBody[0][0] = (int)(GAMESCREEN_WIDTH / 2) - 1;
snakeGame.snakeBody[0][1] = (int)GAMESCREEN_HEIGHT / 2;
snakeGame.snakeBody[0][0] = (int)(GAMESCREEN_WIDTH / 2) - 2;
snakeGame.snakeBody[0][1] = (int)GAMESCREEN_HEIGHT / 2;
snakeGame.length = 3;
putFood();
snakeGame.vel_x = 1;
snakeGame.vel_y = 0;
}
int checkGameOver()
{
if ((snakeGame.snakeBody[0][0] + snakeGame.vel_x) < (GAMESCREEN_WIDTH - 1) &&
(snakeGame.snakeBody[0][0] + snakeGame.vel_x) > 0 &&
(snakeGame.snakeBody[0][1] + snakeGame.vel_y) < (GAMESCREEN_HEIGHT - 1) &&
(snakeGame.snakeBody[0][1] + snakeGame.vel_y) > 0)
{
return 0;
}
else
{
return 1;
}
}
void gameOver()
{
snakeGame.running = 0;
}
int snakeIsHere(int x, int y)
{
for (int i = 0; i < (GAMESCREEN_WIDTH * GAMESCREEN_HEIGHT); i++)
{
// check x and y
if (x == snakeGame.snakeBody[i][0] && y == snakeGame.snakeBody[i][1])
{
return 1;
}
}
return 0;
}
int hasEatenFood()
{
for (int i = 0; i < (GAMESCREEN_HEIGHT * GAMESCREEN_WIDTH); i++)
{
if (snakeGame.food_x == (snakeGame.snakeBody[i][0] + snakeGame.vel_y) && snakeGame.food_y == (snakeGame.snakeBody[i][1] + snakeGame.vel_y))
{
return 1;
}
}
return 0;
}
void update()
{
if (checkGameOver())
{
gameOver();
}
if (hasEatenFood())
{
snakeGame.length++;
snakeGame.score++;
putFood();
}
// Shift all places to right
for (int i = (GAMESCREEN_HEIGHT * GAMESCREEN_WIDTH) - 1; i >= 0; i--)
{
snakeGame.snakeBody[i + 1][0] = snakeGame.snakeBody[i][0];
snakeGame.snakeBody[i + 1][1] = snakeGame.snakeBody[i][1];
}
snakeGame.snakeBody[0][0] += snakeGame.vel_x;
snakeGame.snakeBody[0][1] += snakeGame.vel_y;
snakeGame.top++;
// Do a cleanup
while (snakeGame.top + 1 > snakeGame.length)
{
snakeGame.snakeBody[snakeGame.top][0] = -1;
snakeGame.snakeBody[snakeGame.top][1] = -1;
snakeGame.top--;
}
}
void render()
{
for (int j = 0; j < GAMESCREEN_HEIGHT; j++)
{
for (int i = 0; i < GAMESCREEN_WIDTH; i++)
{
if (i == 0 || i == GAMESCREEN_WIDTH - 1 || j == 0 || j == GAMESCREEN_HEIGHT - 1)
{
printf("%c", borderChar);
}
else if (snakeIsHere(i, j))
{
printf("%c", snakeChar);
}
else if (i == snakeGame.food_x && j == snakeGame.food_y)
{
printf("%c", foodChar);
}
else
{
printf("%c", backgroundChar);
}
}
printf("\n");
}
printf("SCORE: %d", snakeGame.score);
}
void updateVelocity()
{
if (_kbhit())
{
inputCh = getch();
if ((inputCh == 'w' || inputCh == 'W') && snakeGame.vel_y != 1)
{
snakeGame.vel_x = 0;
snakeGame.vel_y = -1;
}
else if ((inputCh == 'a' || inputCh == 'A') && snakeGame.vel_x != 1)
{
snakeGame.vel_x = -1;
snakeGame.vel_y = 0;
}
else if ((inputCh == 's' || inputCh == 'S') && snakeGame.vel_y != -1)
{
snakeGame.vel_x = 0;
snakeGame.vel_y = 1;
}
else if ((inputCh == 'd' || inputCh == 'D') && snakeGame.vel_x != -1)
{
snakeGame.vel_x = 1;
snakeGame.vel_y = 0;
}
}
}
void resetCursor()
{
// Create a COORD structure and fill in its members.
// This specifies the new position of the cursor that we will set.
COORD coord;
coord.X = 0;
coord.Y = 0;
// Obtain a handle to the console screen buffer.
// (You're just using the standard console, so you can use STD_OUTPUT_HANDLE
// in conjunction with the GetStdHandle() to retrieve the handle.)
// Note that because it is a standard handle, we don't need to close it.
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Finally, call the SetConsoleCursorPosition function.
if (!SetConsoleCursorPosition(hConsole, coord))
{
// Uh-oh! The function call failed, so you need to handle the error.
// You can call GetLastError() to get a more specific error code.
// ...
}
}
void hideCursor()
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = 0; // set the cursor visibility to false
SetConsoleCursorInfo(out, &cursorInfo);
}
void clearScreen()
{
resetCursor();
for (int i = 0; i < GAMESCREEN_HEIGHT + 3; i++)
{
for (int j = 0; j < GAMESCREEN_WIDTH + 3; j++)
{
printf(" ");
}
printf("\n");
}
resetCursor();
}
void main()
{
snakeGame.running = 1;
while (1)
{
// SnakeGame Starts
hideCursor();
initialize();
clearScreen();
// SnakeGame Runs
while (snakeGame.running)
{
update();
render();
Sleep(250);
updateVelocity();
fflush(stdin);
resetCursor(0, 0);
}
// SnakeGame Over
clearScreen();
printf("\nGAME OVER.");
printf("\nPress any key to try again.");
getch();
snakeGame.running = 1;
fflush(stdin);
}
}