-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
snake.c
244 lines (204 loc) · 4.91 KB
/
snake.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
#include <uvm/syscalls.h>
#include <uvm/utils.h>
#include <uvm/window.h>
#include <stdlib.h>
#include <stdio.h>
#define FRAME_WIDTH 640
#define FRAME_HEIGHT 640
#define GRID_WIDTH 32
#define GRID_HEIGHT 32
#define TILE_SIZE 20
#define MAX_SNAKE_LEN 2048
// RGBA pixels: 640 * 640
u32 frame_buffer[409_600];
// Apple position
int apple_x = 10;
int apple_y = 10;
// Current direction
int dx = 0;
int dy = 1;
int snake_len = 5;
// Snake x/y cell positions, from head to tail
int snake_xs[MAX_SNAKE_LEN];
int snake_ys[MAX_SNAKE_LEN];
void draw_square(int xmin, int ymin, int size, u32 color)
{
for (int j = 0; j < size; ++j)
{
for (int i = 0; i < size; ++i)
{
u32* pix_ptr = frame_buffer + (FRAME_WIDTH) * (ymin + j) + (xmin + i);
*pix_ptr = color;
}
}
}
void draw_circle(int xmin, int ymin, int size, u32 color)
{
int xmax = xmin + size;
int ymax = ymin + size;
int radius = size / 2;
int cx = xmin + radius;
int cy = ymin + radius;
int r2 = (radius - 1) * (radius - 1);
for (int y = ymin; y < ymax; ++y)
{
for (int x = xmin; x < xmax; ++x)
{
int dx = x - cx;
int dy = y - cy;
int dist_sqr = (dx * dx) + (dy * dy);
if (dist_sqr > r2)
continue;
u32* pix_ptr = frame_buffer + (FRAME_WIDTH * y + x);
*pix_ptr = color;
}
}
}
bool snake_collision(int x, int y)
{
for (int i = 0; i < snake_len; ++i)
{
if (snake_xs[i] == x && snake_ys[i] == y)
{
return true;
}
}
return false;
}
void spawn_apple()
{
while (true)
{
int nx = 2 + rand() % (GRID_WIDTH - 4);
int ny = 2 + rand() % (GRID_HEIGHT - 4);
if (snake_collision(nx, ny) == false)
{
apple_x = nx;
apple_y = ny;
break;
}
}
}
void update()
{
// Move the snake body forward
// We do this from tail to head
for (int i = snake_len - 1; i > 0; i = i - 1)
{
snake_xs[i] = snake_xs[i-1];
snake_ys[i] = snake_ys[i-1];
}
int nx = snake_xs[0] + dx;
int ny = snake_ys[0] + dy;
if (snake_collision(nx, ny))
{
puts("snake ran into itself\n");
exit(0);
}
// Move the head forward (after the collision check)
snake_xs[0] = nx;
snake_ys[0] = ny;
if (nx == 0 || nx >= GRID_WIDTH - 1 || ny == 0 || ny == GRID_HEIGHT - 1)
{
puts("snake crashed into wall\n");
exit(0);
}
if (nx == apple_x && ny == apple_y)
{
spawn_apple();
++snake_len;
puts("got the apple, snake length is now ");
print_i64(snake_len);
puts("!\n");
}
// Clear the screen
memset(frame_buffer, 0, sizeof(frame_buffer));
for (int i = 0; i < GRID_WIDTH; ++i)
{
draw_square(i * TILE_SIZE, 0, TILE_SIZE, 0x873E23);
draw_square(i * TILE_SIZE, (GRID_HEIGHT-1) * TILE_SIZE, TILE_SIZE, 0x873E23);
}
for (int j = 0; j < GRID_HEIGHT; ++j)
{
draw_square(0, j * TILE_SIZE, TILE_SIZE, 0x873E23);
draw_square((GRID_WIDTH-1) * TILE_SIZE, j * TILE_SIZE, TILE_SIZE, 0x873E23);
}
// Draw the apple
draw_circle(apple_x * TILE_SIZE, apple_y * TILE_SIZE, TILE_SIZE, 0xFF0000);
// Draw the snake
for (int i = 1; i < snake_len - 1; ++i)
{
draw_square(
snake_xs[i] * TILE_SIZE,
snake_ys[i] * TILE_SIZE,
TILE_SIZE,
0x00FF00
);
}
// Draw the head
draw_square(
snake_xs[0] * TILE_SIZE,
snake_ys[0] * TILE_SIZE,
TILE_SIZE,
0xFF00FF
);
window_draw_frame(0, frame_buffer);
thread_sleep(100);
}
Event event;
void read_keys()
{
while (window_poll_event(&event))
{
if (event.kind == EVENT_QUIT)
{
exit(0);
}
if (event.kind != EVENT_KEYDOWN)
{
continue;
}
// Current snake x/y direction
int sdx = snake_xs[1] - snake_xs[0];
int sdy = snake_ys[1] - snake_ys[0];
if (event.key == KEY_ESCAPE)
{
exit(0);
}
if (event.key == KEY_LEFT && sdx != -1)
{
dx = -1;
dy = 0;
}
else if (event.key == KEY_RIGHT && sdx != 1)
{
dx = 1;
dy = 0;
}
else if (event.key == KEY_UP && sdy != -1)
{
dx = 0;
dy = -1;
}
else if (event.key == KEY_DOWN && sdy != 1)
{
dx = 0;
dy = 1;
}
}
}
void main()
{
window_create(FRAME_WIDTH, FRAME_HEIGHT, "Snake Game Example", 0);
// Initialize the snake positions
for (int i = 0; i < snake_len; ++i)
{
snake_xs[i] = GRID_WIDTH / 2;
snake_ys[i] = (GRID_HEIGHT / 4) - i;
}
for (;;)
{
read_keys();
update();
}
}