-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
261 lines (221 loc) · 6.73 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
#include "game.h"
#include "util.h"
static element_t s_map[MAP_HEIGHT][MAP_WIDTH] = { { 0, } };
static snake_t s_snake[MAP_HEIGHT * MAP_WIDTH] = { 0, };
static size_t s_tail_index = 0;
static size_t s_score = 0;
void init(void)
{
HANDLE console_handle;
CONSOLE_CURSOR_INFO console_cursor;
system("mode con cols=51 lines=30 | title SNAKE GAME");
console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
console_cursor.bVisible = 0;
console_cursor.dwSize = 1;
SetConsoleCursorInfo(console_handle, &console_cursor);
}
void draw_title(void)
{
printf("\n\n\n\n\n\n\n");
printf(" ### # # # # # ##### \n");
printf(" # ## # # # # # # \n");
printf(" #### # # # ##### ## ##### \n");
printf(" # # ## # # # # # \n");
printf(" #### # # # # # # ##### \n");
}
menu_t draw_menu(void)
{
const size_t MENU_Y = 20;
size_t x = 22;
size_t y = (size_t)MENU_Y;
goto_xy_print(x - 2, y, "> GAME START");
goto_xy_print(x, y + 1, "GAME INFO");
goto_xy_print(x, y + 2, " EXIT ");
while (true) {
input_key_t entered_key = key_control();
switch (entered_key)
{
case INPUT_KEY_UP:
if (y > MENU_Y) {
goto_xy_print(x - 2, y, " ");
goto_xy_print(x - 2, --y, ">");
}
break;
case INPUT_KEY_DOWN:
if (y < MENU_Y + 2) {
goto_xy_print(x - 2, y, " ");
goto_xy_print(x - 2, ++y, ">");
}
break;
case INPUT_KEY_SUBMIT:
return y - MENU_Y;
default:
continue;
}
Sleep(100);
}
}
void draw_info(void)
{
system("cls");
printf("\n\n");
printf(" [ Interface ]\n\n");
printf(" MOVE: W, A, S, D \n");
printf(" PAUSE: P \n");
printf(" SELECT: SPACEBAR \n\n\n\n\n\n\n");
printf(" DEVELOPER: tedlee\n\n");
printf(" When you click SPACEBAR, you can go to main page.");
while (true) {
if (key_control() == INPUT_KEY_SUBMIT) {
break;
}
}
}
void reset(void)
{
size_t i = 0;
size_t j = 0;
for (i = 0; i < MAP_WIDTH * MAP_HEIGHT; ++i) {
s_snake[i].x = 0;
s_snake[i].y = 0;
}
for (i = 0; i < MAP_HEIGHT; ++i) {
for (j = 0; j < MAP_WIDTH; ++j) {
s_map[i][j] = 0;
}
}
s_tail_index = 0;
set_element(ELEMENT_SNAKE_HEAD);
set_element(ELEMENT_FOOD);
}
void draw_map(void)
{
size_t x;
size_t y;
system("cls");
set_color(COLOR_WHITE, COLOR_WHITE);
for(x = MAP_X; x < MAP_WIDTH + MAP_X + 2; ++x){
goto_xy_print(x, MAP_Y, "-");
goto_xy_print(x, MAP_HEIGHT + MAP_Y + 1, "-");
}
for(y = MAP_Y; y < MAP_HEIGHT + MAP_Y + 1; ++y){
goto_xy_print(MAP_X, y, "-");
goto_xy_print(MAP_WIDTH + MAP_X + 1, y, "-");
}
for (y = 0; y < MAP_HEIGHT; ++y) {
for (x = 0; x < MAP_WIDTH; ++x) {
switch (s_map[y][x])
{
case ELEMENT_EMPTY:
set_color(COLOR_WHITE, COLOR_BLACK);
goto_xy_print(x + ITEM_X, y + ITEM_Y, " ");
continue;
case ELEMENT_SNAKE_BODY:
set_color(COLOR_GREEN, COLOR_BLACK);
break;
case ELEMENT_SNAKE_HEAD:
set_color(COLOR_RED, COLOR_BLACK);
break;
case ELEMENT_FOOD:
set_color(COLOR_LIGHT_BLUE, COLOR_BLACK);
break;
default:
continue;
}
goto_xy_print(x + ITEM_X, y + ITEM_Y, "%c", s_map[y][x]);
}
}
set_color(COLOR_WHITE, COLOR_BLACK);
goto_xy_print(MAP_X, MAP_HEIGHT + MAP_Y + 4, " Score:0");
}
game_status_t move(const direction_t direction, size_t* game_speed_millie_p)
{
int head_x = s_snake[0].x;
int head_y = s_snake[0].y;
set_color(COLOR_GREEN, COLOR_BLACK);
goto_xy_print(head_x + ITEM_X, head_y + ITEM_Y, "%c", ELEMENT_SNAKE_BODY);
s_map[head_y][head_x] = ELEMENT_SNAKE_BODY;
set_color(COLOR_WHITE, COLOR_BLACK);
switch (direction)
{
case NORTH:
--head_y;
break;
case SOUTH:
++head_y;
break;
case WEST:
--head_x;
break;
case EAST:
++head_x;
break;
default:
assert(0);
break;
}
if (head_x < 0 || head_y < 0 || head_x >= MAP_WIDTH || head_y >= MAP_HEIGHT) {
return GAME_OVER;
}
switch (s_map[head_y][head_x])
{
size_t i;
case ELEMENT_EMPTY:
set_color(COLOR_RED, COLOR_BLACK);
goto_xy_print(head_x + ITEM_X, head_y + ITEM_Y, "%c", ELEMENT_SNAKE_HEAD);
s_map[head_y][head_x] = ELEMENT_SNAKE_HEAD;
goto_xy_print(s_snake[s_tail_index].x + ITEM_X, s_snake[s_tail_index].y + ITEM_Y, "%c", ' ');
s_map[s_snake[s_tail_index].y][s_snake[s_tail_index].x] = ELEMENT_EMPTY;
for (i = s_tail_index; i > 0; --i) {
s_snake[i].x = s_snake[i - 1].x;
s_snake[i].y = s_snake[i - 1].y;
}
s_snake[0].x = head_x;
s_snake[0].y = head_y;
break;
case ELEMENT_SNAKE_BODY:
return GAME_OVER;
case ELEMENT_FOOD:
set_color(COLOR_RED, COLOR_BLACK);
goto_xy_print(head_x + ITEM_X, head_y + ITEM_Y, "%c", ELEMENT_SNAKE_HEAD);
s_map[head_y][head_x] = ELEMENT_SNAKE_HEAD;
++s_tail_index;
for (i = s_tail_index; i > 0; --i) {
s_snake[i].x = s_snake[i - 1].x;
s_snake[i].y = s_snake[i - 1].y;
}
s_snake[0].x = head_x;
s_snake[0].y = head_y;
set_element(ELEMENT_FOOD);
s_score += 100;
goto_xy_print(44, MAP_HEIGHT + MAP_Y + 4, "%d", s_score);
if (*game_speed_millie_p > 100) {
*game_speed_millie_p -= 50;
}
break;
default:
assert(0);
break;
}
set_color(COLOR_WHITE, COLOR_BLACK);
return GAME_RESUME;
}
void set_element(const element_t element)
{
int x;
int y;
do {
srand((unsigned int)time(NULL));
x = rand() % MAP_WIDTH;
y = rand() % MAP_HEIGHT;
} while (s_map[y][x] != ELEMENT_EMPTY);
if (element == ELEMENT_SNAKE_HEAD) {
s_snake[0].x = x;
s_snake[0].y = y;
} else if (element == ELEMENT_FOOD){
set_color(COLOR_LIGHT_BLUE, COLOR_BLACK);
goto_xy_print(x + ITEM_X, y + ITEM_Y, "%c", element);
set_color(COLOR_WHITE, COLOR_BLACK);
}
s_map[y][x] = element;
}