Repository navigation
Expand file tree
/
Copy pathsnake.c
More file actions
433 lines (361 loc) · 9.61 KB
/
Copy pathsnake.c
File metadata and controls
433 lines (361 loc) · 9.61 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "snake.h"
#include "fb.h"
#include "keyboard.h"
#define GAME_WIDTH 40
#define GAME_HEIGHT 20
#define MAX_SNAKE_LENGTH 100
/* Direction constants */
#define DIR_UP 0
#define DIR_DOWN 1
#define DIR_LEFT 2
#define DIR_RIGHT 3
/* Snake segment structure */
struct segment {
int x;
int y;
};
/* Game state */
static struct segment snake[MAX_SNAKE_LENGTH];
static int snake_length;
static int direction;
static int next_direction;
static struct segment food;
static int score;
static int game_over;
static int game_started;
/* Forward declarations */
void draw_snake(void);
void draw_food(void);
/* Simple random number generator */
static unsigned int seed = 12345;
int rand(void) {
seed = seed * 1103515245 + 12345;
return (seed / 65536) % 32768;
}
void srand(unsigned int s) {
seed = s;
}
/* Delay function */
void delay(unsigned int count) {
volatile unsigned int i, j;
for (i = 0; i < count; i++) {
for (j = 0; j < 10000; j++) {
/* Just waste time */
}
}
}
/* Integer to string conversion */
void int_to_str(int num, char *str) {
int i = 0;
int is_negative = 0;
if (num == 0) {
str[0] = '0';
str[1] = '\0';
return;
}
if (num < 0) {
is_negative = 1;
num = -num;
}
while (num > 0) {
str[i++] = '0' + (num % 10);
num /= 10;
}
if (is_negative) {
str[i++] = '-';
}
str[i] = '\0';
/* Reverse the string */
int start = 0;
int end = i - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
/* Draw a character at a specific position */
void draw_at(int x, int y, char c) {
if (x < 0 || x >= GAME_WIDTH || y < 0 || y >= GAME_HEIGHT) {
return;
}
/* Move to the position (top offset of 2 lines for UI) */
int pos = (y + 2) * 80 + (x + 20); /* Center the game on screen */
/* Direct framebuffer access */
char *fb = (char *) 0x000B8000;
fb[pos * 2] = c;
fb[pos * 2 + 1] = 0x0F; /* White on black */
}
/* Draw centered text */
void draw_centered_text(int y, const char *text) {
int len = 0;
int i;
while (text[len] != '\0') len++;
int start_x = (GAME_WIDTH - len) / 2;
for (i = 0; i < len; i++) {
draw_at(start_x + i, y, text[i]);
}
}
/* Clear the game area */
void clear_game_area(void) {
int x, y;
for (y = 0; y < GAME_HEIGHT; y++) {
for (x = 0; x < GAME_WIDTH; x++) {
draw_at(x, y, ' ');
}
}
}
/* Draw the border */
void draw_border(void) {
int i;
/* Top and bottom borders */
for (i = 0; i < GAME_WIDTH; i++) {
draw_at(i, 0, '#');
draw_at(i, GAME_HEIGHT - 1, '#');
}
/* Left and right borders */
for (i = 0; i < GAME_HEIGHT; i++) {
draw_at(0, i, '#');
draw_at(GAME_WIDTH - 1, i, '#');
}
}
/* Generate new food position */
void generate_food(void) {
int valid;
int i;
do {
valid = 1;
food.x = (rand() % (GAME_WIDTH - 2)) + 1;
food.y = (rand() % (GAME_HEIGHT - 2)) + 1;
/* Check if food spawned on snake */
for (i = 0; i < snake_length; i++) {
if (snake[i].x == food.x && snake[i].y == food.y) {
valid = 0;
break;
}
}
} while (!valid);
}
/* Calculate delay based on score */
int calculate_delay(void) {
int base_delay = 50;
int speed_increase = score / 50; /* Increase speed every 50 points */
int new_delay = base_delay - (speed_increase * 5);
if (new_delay < 10) {
new_delay = 10; /* Minimum delay to keep game playable */
}
return new_delay;
}
/* Initialize the game */
void init_game(void) {
/* Clear screen and draw UI */
fb_clear();
fb_puts("SNAKE GAME - Use (W A S D) to move, Q to quit\n");
fb_puts("Score: 0\n");
/* Initialize snake in the middle */
snake_length = 3;
snake[0].x = GAME_WIDTH / 2;
snake[0].y = GAME_HEIGHT / 2;
snake[1].x = snake[0].x - 1;
snake[1].y = snake[0].y;
snake[2].x = snake[1].x - 1;
snake[2].y = snake[1].y;
direction = DIR_RIGHT;
next_direction = DIR_RIGHT;
score = 0;
game_over = 0;
game_started = 0;
/* Seed random with snake position */
srand(snake[0].x * snake[0].y * 12345);
/* Generate first food */
generate_food();
/* Draw game area */
clear_game_area();
draw_border();
/* Display start message in center */
draw_centered_text(GAME_HEIGHT / 2 - 1, "Press any key to START");
/* Wait for key press to start */
while (keyboard_get_char() == 0) {
/* Wait */
}
/* Clear the start message and draw game */
clear_game_area();
draw_border();
draw_snake();
draw_food();
game_started = 1;
}
/* Update the score display */
void update_score(void) {
char score_str[20];
char *fb = (char *) 0x000B8000;
int i;
/* Clear the score line */
for (i = 0; i < 20; i++) {
fb[(1 * 80 + 7 + i) * 2] = ' ';
}
/* Write new score */
int_to_str(score, score_str);
i = 0;
while (score_str[i] != '\0') {
fb[(1 * 80 + 7 + i) * 2] = score_str[i];
i++;
}
}
/* Draw the snake */
void draw_snake(void) {
int i;
for (i = 0; i < snake_length; i++) {
if (i == 0) {
draw_at(snake[i].x, snake[i].y, 'O'); /* Head */
} else {
draw_at(snake[i].x, snake[i].y, 'o'); /* Body */
}
}
}
/* Draw the food */
void draw_food(void) {
draw_at(food.x, food.y, '*');
}
/* Check collision with walls or self */
int check_collision(int x, int y) {
int i;
/* Check walls */
if (x <= 0 || x >= GAME_WIDTH - 1 || y <= 0 || y >= GAME_HEIGHT - 1) {
return 1;
}
/* Check self collision */
for (i = 1; i < snake_length; i++) {
if (snake[i].x == x && snake[i].y == y) {
return 1;
}
}
return 0;
}
/* Update game state */
void update_game(void) {
int i;
int new_x, new_y;
int ate_food = 0;
/* Update direction (prevent 180-degree turns) */
if ((direction == DIR_UP && next_direction != DIR_DOWN) ||
(direction == DIR_DOWN && next_direction != DIR_UP) ||
(direction == DIR_LEFT && next_direction != DIR_RIGHT) ||
(direction == DIR_RIGHT && next_direction != DIR_LEFT)) {
direction = next_direction;
}
/* Calculate new head position */
new_x = snake[0].x;
new_y = snake[0].y;
switch (direction) {
case DIR_UP:
new_y--;
break;
case DIR_DOWN:
new_y++;
break;
case DIR_LEFT:
new_x--;
break;
case DIR_RIGHT:
new_x++;
break;
}
/* Check collision */
if (check_collision(new_x, new_y)) {
game_over = 1;
return;
}
/* Check if ate food */
if (new_x == food.x && new_y == food.y) {
ate_food = 1;
score += 10;
update_score();
if (snake_length < MAX_SNAKE_LENGTH) {
snake_length++;
}
generate_food();
}
/* Erase tail if didn't eat food */
if (!ate_food) {
draw_at(snake[snake_length - 1].x, snake[snake_length - 1].y, ' ');
}
/* Move snake body */
for (i = snake_length - 1; i > 0; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
/* Update head position */
snake[0].x = new_x;
snake[0].y = new_y;
}
/* Process input */
void process_input(void) {
char c = keyboard_get_char();
if (c != 0) {
switch (c) {
case 'w':
case 'W':
next_direction = DIR_UP;
break;
case 's':
case 'S':
next_direction = DIR_DOWN;
break;
case 'a':
case 'A':
next_direction = DIR_LEFT;
break;
case 'd':
case 'D':
next_direction = DIR_RIGHT;
break;
case 'q':
case 'Q':
game_over = 2; /* 2 means quit to shell */
break;
}
}
}
/* Main game function */
void snake_game(void) {
int quit_game = 0;
while (!quit_game) {
init_game();
/* Game loop */
while (!game_over) {
/* Process input */
process_input();
/* Update game state */
update_game();
/* Draw everything */
draw_snake();
draw_food();
/* Delay for game speed (increases with score) */
delay(calculate_delay());
}
/* Game over screen */
if (game_over == 1) {
fb_clear();
fb_puts("GAME OVER!\n\n");
fb_puts("Final Score: ");
char score_str[20];
int_to_str(score, score_str);
fb_puts(score_str);
fb_puts("\n\nPress any key to restart or Q to quit...\n");
/* Wait for key press */
char c = 0;
while (c == 0) {
c = keyboard_get_char();
}
if (c == 'q' || c == 'Q') {
quit_game = 1;
}
/* Otherwise loop back to start a new game */
} else if (game_over == 2) {
quit_game = 1;
}
}
}