-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathattackers.c
364 lines (300 loc) · 7.15 KB
/
attackers.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
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
#include <uvm/syscalls.h>
#include <uvm/window.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#define FRAME_WIDTH 800
#define FRAME_HEIGHT 600
#define DOT_SIZE 5
#define BOLT_WIDTH 3
#define BOLT_HEIGHT 12
#define ENEMY_COLS 11
#define ENEMY_ROWS 5
// Ship pixel graphics
char* SHIP_DOTS[1];
// Enemy ship pixel graphics
char* ENEMY_DOTS[4];
// RGBA pixels: 800 * 600
u32 frame_buffer[480_000];
// Left/right arrow currently pressed
bool left_down = false;
bool right_down = false;
// Current enemy i/j (top-left) position
int enemy_i = 0;
int enemy_j = 0;
int enemy_di = 1;
// Bit mask for live enemies
u64 enemies_live = 0x7FFFFFFFFFFFFF;
// Ship position
int ship_x = 400;
int ship_y = 540;
// Ship bolt position
int bolt_x = 0;
int bolt_y = 0;
// Enemy update step counter
unsigned int enemy_steps = 0;
void init()
{
SHIP_DOTS[0] = (
" * \n"
" * \n"
" * *** * \n"
" ******* \n"
" * *** * \n"
"* * *\n"
);
ENEMY_DOTS[0] = (
"** **\n"
" * * \n"
" **** \n"
" * ** * \n"
" ****** \n"
"* *"
);
ENEMY_DOTS[1] = (
" * * \n"
" * * \n"
" **** \n"
" * ** * \n"
" ****** \n"
" * * "
);
}
void draw_dots(int xmin, int ymin, int dot_size, char* dots, u32 color)
{
assert(dots);
int row = 0;
int col = 0;
char* dot = dots;
for (char* dot = dots; *dot; ++dot)
{
char ch = *dot;
int x = xmin + col * dot_size;
int y = ymin + row * dot_size;
col = col + 1;
if (ch == '\n')
{
row = row + 1;
col = 0;
continue;
}
if (ch != '*')
{
continue;
}
draw_rect(x, y, dot_size, dot_size, color);
}
}
int glyph_width(char* dots, int dot_size)
{
size_t row_len = (size_t)strchr(dots, '\n') - (size_t)dots;
return dot_size * (int)row_len;
}
int glyph_height(char* dots, int dot_size)
{
int num_rows = 1;
for (int i = 0;; ++i)
{
char ch = dots[i];
if (ch == '\n')
++num_rows;
if (ch == '\0')
break;
}
return dot_size * num_rows;
}
void draw_rect(int xmin, int ymin, int width, int height, u32 color)
{
for (int j = 0; j < height; ++j)
{
for (int i = 0; i < width; ++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;
}
}
}
void fire_bolt()
{
if (bolt_y > 0)
return;
int ship_width = glyph_width(SHIP_DOTS[0], DOT_SIZE);
bolt_x = ship_x + ship_width / 2;
bolt_y = ship_y - 14;
}
void update_anim()
{
if (left_down && !right_down)
{
if (ship_x > 20)
ship_x = ship_x - 7;
}
else if (right_down && !left_down)
{
if (ship_x < FRAME_WIDTH - 60)
ship_x = ship_x + 7;
}
// If our bolt is active, make it move up
if (bolt_y > 0)
{
bolt_y = bolt_y - 14;
}
// Clear the screen
memset(frame_buffer, 0, sizeof(frame_buffer));
for (int j = 0; j < ENEMY_ROWS; ++j)
{
for (int i = 0; i < ENEMY_COLS; ++i)
{
u64 enemy_bit = (u64)1 << (ENEMY_COLS * j + i);
if ((enemies_live & enemy_bit) == 0)
{
continue;
}
int min_x = 50 + (10 * enemy_i) + (50 * i);
int min_y = 100 + (10 * enemy_j) + (50 * j);
if (min_y > 500)
{
puts("GAME OVER!\n");
exit(0);
}
char* enemy_glyph = ENEMY_DOTS[enemy_steps % 2];
int width = glyph_width(enemy_glyph, DOT_SIZE);
if (bolt_x > min_x && bolt_x < min_x + width)
{
int height = glyph_height(enemy_glyph, DOT_SIZE);
if (bolt_y > min_y && bolt_y < min_y + height)
{
puts("HIT\n");
enemies_live = enemies_live ^ enemy_bit;
bolt_y = 0;
}
}
draw_dots(
min_x,
min_y,
DOT_SIZE,
enemy_glyph,
0xFF_FF_FF
);
}
}
// Draw our ship
draw_dots(
ship_x,
ship_y,
DOT_SIZE,
SHIP_DOTS[0],
0x00_FF_00
);
// Draw our bolt if active
if (bolt_y > 0)
{
draw_rect(
bolt_x,
bolt_y,
BOLT_WIDTH,
BOLT_HEIGHT,
0xFF_FF_FF
);
}
window_draw_frame(0, frame_buffer);
}
// Enemy movement update
void update_enemies()
{
if (enemies_live == 0)
{
puts("VICTORY!\n");
exit(0);
}
enemy_i = enemy_i + enemy_di;
if (enemy_i > 17)
{
enemy_di = -1;
enemy_j = enemy_j + 1;
}
else if (enemy_i < 0)
{
enemy_di = 1;
enemy_j = enemy_j + 1;
}
// Update the step count
enemy_steps = enemy_steps + 1;
}
Event event;
void main()
{
init();
window_create(FRAME_WIDTH, FRAME_HEIGHT, "Galactic Attackers", 0);
for (u64 frame_idx = 0;; frame_idx = frame_idx + 1)
{
while (window_poll_event(&event))
{
if (event.kind == EVENT_QUIT)
{
exit(0);
}
if (event.kind == EVENT_KEYDOWN)
{
if (event.key == KEY_ESCAPE)
{
exit(0);
}
else if (event.key == KEY_LEFT)
{
left_down = true;
}
else if (event.key == KEY_RIGHT)
{
right_down = true;
}
else if (event.key == KEY_SPACE)
{
fire_bolt();
}
}
if (event.kind == EVENT_KEYUP)
{
if (event.key == KEY_LEFT)
{
left_down = false;
}
else if (event.key == KEY_RIGHT)
{
right_down = false;
}
}
}
update_anim();
// Enemy update rate
int enemy_rate = 18 - 2 * enemy_j;
if (enemy_rate <= 2) enemy_rate = 2;
if (frame_idx > 40 && frame_idx % enemy_rate == 0)
update_enemies();
// 60fps max
thread_sleep(1000 / 60);
}
}