-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
358 lines (307 loc) · 11.4 KB
/
snake.cpp
File metadata and controls
358 lines (307 loc) · 11.4 KB
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
#include "brick.h"
#include "cleananimation.h"
#include "winanimation.h"
#include "snake_levels.h"
#include <windows.h>
#include <time.h>
/* Interface static function prototypes */
static void snake_init(void);
static void snake_right_key_press(void);
static void snake_left_key_press(void);
static void snake_right_key_release(void);
static void snake_left_key_release(void);
static void snake_up_key_press(void);
static void snake_up_key_release(void);
static void snake_down_key_press(void);
static void snake_down_key_release(void);
static void snake_next_figure_accepted(void);
static void snake_game_loop(void);
static void snake_restart_level(void);
/* Other static function prototypes */
static void snake_init_after_cleananimation();
static void snake_tick(void);
static void add_block_to_redraw_rectangle(char x, char y);
static void place_food(void);
#define SNAKE_MAX_SEGMENTS 30
#define SNAKE_SPEED 150
#define SNAKE_STATE_NORMAL 0
#define SNAKE_STATE_GAMEOVER 1
#define SNAKE_STATE_CLEANANIMATION 2
#define SNAKE_STATE_WINANIMATION 3
typedef struct {
segment_t segments[SNAKE_MAX_SEGMENTS];
short head;
short tail;
char move;
char delayed_move;
char turn_in_progress;
char state;
char increase_length;
char wait_before_hit;
char collected_food;
} snake_t;
static snake_t snake = {{{5, 0}, {5, 1}, {5, 2}}, 1, 0, SNAKE_MOVE_DOWN, -1, 0, SNAKE_STATE_NORMAL, 0, 2, 0};
static segment_t food = {0, 0};
static unsigned int snake_timer = 0;
/******************************************************************************
* Exported functions.
******************************************************************************/
void snake_init_interface(game_interface_t *iface) {
iface->game_init = snake_init;
iface->game_right_key_press = snake_right_key_press;
iface->game_left_key_press = snake_left_key_press;
iface->game_right_key_release = snake_right_key_release;
iface->game_left_key_release = snake_left_key_release;
iface->game_up_key_press = snake_up_key_press;
iface->game_up_key_release = snake_up_key_release;
iface->game_down_key_press = snake_down_key_press;
iface->game_down_key_release = snake_down_key_release;
iface->game_next_figure_accepted = snake_next_figure_accepted;
iface->game_loop = snake_game_loop;
iface->game_restart_level = snake_restart_level;
}
/******************************************************************************
* Static functions.
******************************************************************************/
static void snake_init(void) {
snake.state = SNAKE_STATE_CLEANANIMATION;
brick_s.level = 1;
brick_s.score = 0;
brick_s.lives = 9;
cleananimation_init();
}
static void snake_init_after_cleananimation() {
int i, j;
for (i = 0; i < BRICK_PLAYFIELD_WIDTH; i++) {
for (j = 0; j < BRICK_PLAYFIELD_HEIGHT; j++) {
if (snake_levels[brick_s.level - 1].level[j][i] == 0)
brick_s.playfield[i][j] = BRICK_FIELD_EMPTY;
else
brick_s.playfield[i][j] = BRICK_FIELD_OCCUPIED_OUTER;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
brick_s.next[i][j] = BRICK_FIELD_OCCUPIED;
}
}
brick_s.score_changed = 1;
brick_s.level_changed = 1;
brick_s.lives_changed = 1;
brick_s.game_over_notification_flag = false;
brick_s.winning_notification_flag = false;
brick_s.rr.left = 0;
brick_s.rr.top = 0;
brick_s.rr.right = BRICK_PLAYFIELD_WIDTH;
brick_s.rr.bottom = BRICK_PLAYFIELD_HEIGHT;
brick_s.rr.clean = 0;
brick_s.next_changed = true;
snake.head = 2;
snake.tail = 0;
snake.segments[0] = snake_levels[brick_s.level - 1].start_segments[0];
snake.segments[1] = snake_levels[brick_s.level - 1].start_segments[1];
snake.segments[2] = snake_levels[brick_s.level - 1].start_segments[2];
snake.move = snake_levels[brick_s.level - 1].start_move;
snake.turn_in_progress = 0;
snake.delayed_move = -1;
snake.state = SNAKE_STATE_NORMAL;
snake.increase_length = 0;
snake.wait_before_hit = 2;
snake.collected_food = 0;
for (i = snake.tail; i <= snake.head; i++) {
brick_s.playfield[snake.segments[i].x][snake.segments[i].y] = BRICK_FIELD_OCCUPIED_ORANGE;
}
srand(time(NULL));
place_food();
}
static void snake_right_key_press(void) {
if (snake.move != SNAKE_MOVE_LEFT) {
if (!snake.turn_in_progress) {
snake.move = SNAKE_MOVE_RIGHT;
snake.turn_in_progress = true;
} else if (snake.delayed_move < 0) {
snake.delayed_move = SNAKE_MOVE_RIGHT;
}
}
}
static void snake_left_key_press(void) {
if (snake.move != SNAKE_MOVE_RIGHT) {
if (!snake.turn_in_progress) {
snake.move = SNAKE_MOVE_LEFT;
snake.turn_in_progress = true;
} else if (snake.delayed_move < 0) {
snake.delayed_move = SNAKE_MOVE_LEFT;
}
}
}
static void snake_right_key_release(void) {
}
static void snake_left_key_release(void) {
}
static void snake_up_key_press(void) {
if (snake.move != SNAKE_MOVE_DOWN) {
if (!snake.turn_in_progress) {
snake.move = SNAKE_MOVE_UP;
snake.turn_in_progress = true;
} else if (snake.delayed_move < 0) {
snake.delayed_move = SNAKE_MOVE_UP;
}
}
}
static void snake_up_key_release(void) {
}
static void snake_down_key_press(void) {
if (snake.move != SNAKE_MOVE_UP) {
if (!snake.turn_in_progress) {
snake.move = SNAKE_MOVE_DOWN;
snake.turn_in_progress = true;
} else if (snake.delayed_move < 0) {
snake.delayed_move = SNAKE_MOVE_DOWN;
}
}
}
static void snake_down_key_release(void) {
}
static void snake_next_figure_accepted(void) {
brick_s.next_changed = false;
}
static void snake_game_loop(void) {
if (snake.state == SNAKE_STATE_CLEANANIMATION) {
cleananimation();
if (cleananimation() == CLEANANIMATION_DONE)
snake_init_after_cleananimation();
return;
}
if (snake.state == SNAKE_STATE_WINANIMATION) {
winanimation();
return;
}
if (snake.state == SNAKE_STATE_GAMEOVER)
return;
unsigned int t = GetTickCount();
int speed = SNAKE_SPEED;
if (t > snake_timer + speed) {
snake_tick();
}
while (t > snake_timer + speed) {
snake_timer += speed;
}
}
static void snake_restart_level(void) {
}
static void snake_tick(void) {
short next_head = (snake.head + 1) % SNAKE_MAX_SEGMENTS;
short next_tail = (snake.tail + 1) % SNAKE_MAX_SEGMENTS;
// Move head forward
switch (snake.move) {
case SNAKE_MOVE_UP:
if (snake.segments[snake.head].y == 0)
goto game_over;
snake.segments[next_head].x = snake.segments[snake.head].x;
snake.segments[next_head].y = snake.segments[snake.head].y - 1;
break;
case SNAKE_MOVE_DOWN:
if (snake.segments[snake.head].y == BRICK_PLAYFIELD_HEIGHT - 1)
goto game_over;
snake.segments[next_head].x = snake.segments[snake.head].x;
snake.segments[next_head].y = snake.segments[snake.head].y + 1;
break;
case SNAKE_MOVE_RIGHT:
if (snake.segments[snake.head].x == BRICK_PLAYFIELD_WIDTH - 1)
goto game_over;
snake.segments[next_head].x = snake.segments[snake.head].x + 1;
snake.segments[next_head].y = snake.segments[snake.head].y;
break;
case SNAKE_MOVE_LEFT:
if (snake.segments[snake.head].x == 0)
goto game_over;
snake.segments[next_head].x = snake.segments[snake.head].x - 1;
snake.segments[next_head].y = snake.segments[snake.head].y;
break;
}
// Check if we bumped in ourselves
if ((brick_s.playfield[snake.segments[next_head].x][snake.segments[next_head].y] != BRICK_FIELD_EMPTY) &&
!(snake.segments[next_head].x == food.x && snake.segments[next_head].y == food.y))
goto game_over;
brick_s.playfield[snake.segments[next_head].x][snake.segments[next_head].y] = BRICK_FIELD_OCCUPIED_ORANGE;
add_block_to_redraw_rectangle(snake.segments[next_head].x, snake.segments[next_head].y);
snake.head = next_head;
// Check if we reached the maximum length, in that case do not increase the length
if (((next_head + 1) % SNAKE_MAX_SEGMENTS == snake.tail) ||
(snake.increase_length == 0)) {
brick_s.playfield[snake.segments[snake.tail].x][snake.segments[snake.tail].y] = BRICK_FIELD_EMPTY;
add_block_to_redraw_rectangle(snake.segments[snake.tail].x, snake.segments[snake.tail].y);
snake.tail = next_tail;
}
if (snake.increase_length > 0)
snake.increase_length--;
brick_s.rr.clean = 0;
if (snake.delayed_move >= 0) {
snake.move = snake.delayed_move;
snake.delayed_move = -1;
} else {
snake.turn_in_progress = false;
}
// Check if we hit the food
if (snake.segments[next_head].x == food.x &&
snake.segments[next_head].y == food.y) {
brick_s.score += 100;
brick_s.score_changed = 1;
snake.increase_length++;
brick_s.next[3 - snake.collected_food % 4][3 - snake.collected_food / 4] = BRICK_FIELD_EMPTY;
brick_s.next_changed = true;
snake.collected_food++;
if (snake.collected_food >= 16) {
if (brick_s.level > SNAKE_LEVELS - 1) {
winanimation_init();
snake.state = SNAKE_STATE_WINANIMATION;
brick_s.winning_notification_flag = true;
return;
}
brick_s.level++;
snake.state = SNAKE_STATE_CLEANANIMATION;
cleananimation_init();
return;
}
place_food();
}
snake.wait_before_hit = 2;
return;
game_over:
snake.turn_in_progress = false;
if (snake.wait_before_hit > 0) {
snake.wait_before_hit--;
return;
}
if (brick_s.lives > 0) {
brick_s.lives--;
brick_s.lives_changed = true;
brick_s.score -= snake.collected_food * 100;
brick_s.score_changed = true;
snake.state = SNAKE_STATE_CLEANANIMATION;
cleananimation_init();
} else {
brick_s.game_over_notification_flag = true;
snake.state = SNAKE_STATE_GAMEOVER;
}
return;
}
static void add_block_to_redraw_rectangle(char x, char y) {
if (y < brick_s.rr.top)
brick_s.rr.top = y;
if (y > brick_s.rr.bottom)
brick_s.rr.bottom = y;
if (x < brick_s.rr.left)
brick_s.rr.left = x;
if (x > brick_s.rr.right)
brick_s.rr.right = x;
}
static void place_food(void) {
// Find an empty spot and place the food there.
do {
food.x = rand() % BRICK_PLAYFIELD_WIDTH;
food.y = rand() % BRICK_PLAYFIELD_HEIGHT;
} while (brick_s.playfield[food.x][food.y] != BRICK_FIELD_EMPTY);
brick_s.playfield[food.x][food.y] = BRICK_FIELD_OCCUPIED_INNER;
add_block_to_redraw_rectangle(food.x, food.y);
}