-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.jazz
478 lines (396 loc) · 14.2 KB
/
snake.jazz
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import "allegro.jazz"
import "std/libc.jazz"
constexpr BOARD_WIDTH = 800
constexpr BOARD_HEIGHT = 600
constexpr BOARD_CELL = 20
pub struct Position
{
x: i32,
y: i32
}
pub struct Node {
position: Position,
next: *u8
}
pub alias List = *Node
pub func InitializeList(list: *List) void {
*list = malloc(sizeof(Node)) as *Node;
(*list).next = null;
return;
}
pub func deleteList(list: *List) void {
var temp: *Node;
while *list as *u8 != null {
temp = (*list).next as List;
free(*list as *u8);
*list = temp;
}
return;
}
pub func AddNode(list: *List) void {
var new = malloc(sizeof(Node)) as *Node;
var temp = *list;
if new as *u8 == null {
return;
}
new.next = null;
while temp.next != null {
temp = temp.next as List;
}
temp.next = new as *u8;
return;
}
pub struct Snake {
head: *Node,
length: i32
}
pub struct Fruit {
pos: Position
}
pub func (v: *Snake) extend() void {
AddNode(&v.head);
v.length = v.length + 1;
return;
}
pub func (v: *Snake) update() void {
var current = v.head.position;
var prev: Position = Position {x: 0,y: 0};
var temp = v.head.next as *Node;
while temp as *u8 != null {
prev = temp.position;
temp.position = current;
current = prev;
temp = temp.next as *Node;
}
return;
}
pub func (v: *Snake) head() *Node {
return v.head as *Node;
}
pub func InitializeSnake(snake: *Snake) void {
InitializeList(&snake.head);
snake.length = 1;
snake.extend();
var head = snake.head();
head.position.x = BOARD_WIDTH / 2;
head.position.y = BOARD_HEIGHT/ 2;
return;
}
pub func (v: *Snake) draw() void {
var temp = v.head as *Node;
while temp as *u8 != null {
al_draw_filled_rectangle(
temp.position.x as f32,
temp.position.y as f32,
(temp.position.x + BOARD_CELL) as f32,
(temp.position.y + BOARD_CELL) as f32,
al_map_rgb(0 as u8,0 as u8,0 as u8)
)
temp = temp.next as *Node;
}
return;
}
pub func randomFruit(fruit: *Fruit,snake: *Snake) void {
var temp = snake.head as *Node;
var widthEdge = (BOARD_WIDTH / BOARD_CELL) - 2;
var heightEdge = (BOARD_HEIGHT / BOARD_CELL) - 2;
srand(time(null as *u64));
var seed = rand() as i32;
seed = seed % 100;
var fruitX = (rand() as i32 % widthEdge + 1) * BOARD_CELL;
var fruitY = (rand() as i32 % heightEdge + 1) * BOARD_CELL;
while temp as *u8 != null {
if fruitX == temp.position.x && fruitY == temp.position.y {
fruitX = (rand() as i32 % widthEdge + 1) * BOARD_CELL;
fruitY = (rand() as i32 % heightEdge + 1) * BOARD_CELL;
temp = snake.head as *Node;
}
temp = temp.next as *Node;
}
fruit.pos.x = fruitX;
fruit.pos.y = fruitY;
return;
}
pub func (v: *Fruit) draw() void {
al_draw_filled_rectangle(
v.pos.x as f32,
v.pos.y as f32,
(v.pos.x + BOARD_CELL) as f32,
(v.pos.y + BOARD_CELL) as f32,
al_map_rgb(255 as u8,0 as u8,0 as u8)
)
return;
}
constexpr DOWN = 0
constexpr UP = 1
constexpr LEFT = 2
constexpr RIGHT = 3
constexpr ESCAPE = 4
constexpr ENTER = 5
constexpr MENU = 0
constexpr GAMEPLAY = 1
constexpr GAMEOVER = 2
pub struct Game {
display: *ALLEGRO_DISPLAY,
timer: *ALLEGRO_TIMER,
eventQueue: *ALLEGRO_EVENT_QUEUE,
font_24: *ALLEGRO_FONT,
font_30: *ALLEGRO_FONT,
font_50: *ALLEGRO_FONT,
point: *ALLEGRO_SAMPLE,
lose: *ALLEGRO_SAMPLE,
keys: i32,
draw: bool,
pressed: bool,
score: i32,
highScore: i32,
gameOver: bool,
state: i32,
snake: Snake,
fruit: Fruit
}
extern func exit(c: i32) void;
pub func SetupGame(game: *Game) void {
if !al_init {
exit(-1);
}
var display = al_create_display(BOARD_WIDTH,BOARD_HEIGHT + 50);
game.display = display;
al_set_window_position(game.display as *ALLEGRO_DISPLAY,560,420);
al_set_window_title(game.display as *ALLEGRO_DISPLAY,"Snake");
al_install_audio();
al_install_keyboard();
al_init_primitives_addon();
al_init_font_addon();
al_init_ttf_addon();
al_init_acodec_addon();
game.timer = al_create_timer(1.0 / 20.0) as *ALLEGRO_TIMER;
var queue = al_create_event_queue();
al_register_event_source(queue,al_get_keyboard_event_source());
al_register_event_source(queue,al_get_timer_event_source(game.timer as *ALLEGRO_TIMER));
al_register_event_source(queue,al_get_display_event_source(game.display as *ALLEGRO_DISPLAY));
al_start_timer(game.timer as *ALLEGRO_TIMER);
game.eventQueue = queue as *ALLEGRO_EVENT_QUEUE;
game.font_24 = al_load_font("FiraCode-Medium.otf",24,0) as *ALLEGRO_FONT;
game.font_30 = al_load_font("FiraCode-Medium.otf",30,0) as *ALLEGRO_FONT;
game.font_50 = al_load_font("FiraCode-Medium.otf",50,0) as *ALLEGRO_FONT;
game.point = al_load_sample("point.wav") as *ALLEGRO_SAMPLE;
game.lose = al_load_sample("lose.wav") as *ALLEGRO_SAMPLE;
al_reserve_samples(2);
game.draw = true;
game.pressed = false;
game.gameOver = false;
game.state = MENU;
return;
}
pub func (game: *Game) run() void {
printf("Running loop\n");
while !game.gameOver {
game.input();
if game.draw && al_is_event_queue_empty(game.eventQueue as *ALLEGRO_EVENT_QUEUE) {
game.draw = false;
if game.state == MENU {
al_draw_filled_rectangle(0.0 as f32, 0.0 as f32, BOARD_WIDTH as f32, BOARD_HEIGHT as f32 + 50.0, al_map_rgb(255 as u8, 255 as u8, 255 as u8));
al_draw_text(
game.font_50 as *ALLEGRO_FONT,
al_map_rgb(0 as u8, 0 as u8, 0 as u8),
BOARD_WIDTH as f32 / 2 as f32,
BOARD_HEIGHT as f32 / 2 as f32 - 50.0 as f32,
ALLEGRO_ALIGN_CENTRE,
"Snake"
);
al_draw_text(
game.font_50 as *ALLEGRO_FONT,
al_map_rgb(0 as u8, 0 as u8, 0 as u8),
BOARD_WIDTH as f32 / 2 as f32,
BOARD_HEIGHT as f32 / 2 as f32,
ALLEGRO_ALIGN_CENTRE,
"Press enter to start"
);
} else if game.state == GAMEPLAY {
game.draw();
game.logic();
game.snake.update();
} else if game.state == GAMEOVER {
al_draw_filled_rectangle(0.0 as f32, 0.0 as f32, BOARD_WIDTH as f32, BOARD_HEIGHT as f32 + 50.0, al_map_rgb(255 as u8, 255 as u8, 255 as u8));
al_draw_textf(
game.font_50 as *ALLEGRO_FONT,
al_map_rgb(0 as u8, 0 as u8, 0 as u8),
BOARD_WIDTH as f32 / 2 as f32,
BOARD_HEIGHT as f32 / 2 as f32 - 80.0 as f32,
ALLEGRO_ALIGN_CENTRE,
"Your score: %d",game.score
);
al_draw_text(
game.font_24 as *ALLEGRO_FONT,
al_map_rgb(0 as u8, 0 as u8, 0 as u8),
BOARD_WIDTH as f32 / 2 as f32,
BOARD_HEIGHT as f32 / 2 as f32 + 10.0 as f32,
ALLEGRO_ALIGN_CENTRE,
"Press ENTER to start again"
);
al_draw_text(
game.font_24 as *ALLEGRO_FONT,
al_map_rgb(0 as u8, 0 as u8, 0 as u8),
BOARD_WIDTH as f32 / 2 as f32,
BOARD_HEIGHT as f32 / 2 as f32 + 40.0 as f32,
ALLEGRO_ALIGN_CENTRE,
"Press ESCAPE to exit"
);
}
al_flip_display();
al_clear_to_color(al_map_rgb(0 as u8,0 as u8,0 as u8));
}
}
return;
}
pub func (game: *Game) input() void {
var events = malloc(512 as usize) as *ALLEGRO_EVENT;
var timeout: *ALLEGRO_TIMEOUT = malloc(sizeof(ALLEGRO_TIMEOUT)) as *ALLEGRO_TIMEOUT;
al_init_timeout(timeout,0.06);
var get_event = al_wait_for_event_until(game.eventQueue as *ALLEGRO_EVENT_QUEUE,events,timeout);
if get_event {
if events.ty == ALLEGRO_EVENT_KEY_DOWN {
var state: *ALLEGRO_KEYBOARD_STATE = malloc(512 as usize) as *ALLEGRO_KEYBOARD_STATE;
al_get_keyboard_state(state);
//var keyboard = events as *ALLEGRO_KEYBOARD_EVENT;
if al_key_down(state,ALLEGRO_KEY_UP) || al_key_down(state,ALLEGRO_KEY_W) {
if game.keys != DOWN && !game.pressed {
game.keys = UP;
}
game.pressed = true;
} else if al_key_down(state,ALLEGRO_KEY_DOWN) || al_key_down(state,ALLEGRO_KEY_S) {
if game.keys != UP && !game.pressed {
game.keys = DOWN;
}
game.pressed = true;
} else if al_key_down(state,ALLEGRO_KEY_LEFT) || al_key_down(state,ALLEGRO_KEY_A) {
if game.keys != RIGHT && !game.pressed {
game.keys = LEFT;
}
game.pressed = true;
} else if al_key_down(state,ALLEGRO_KEY_RIGHT) || al_key_down(state,ALLEGRO_KEY_D) {
if game.keys != LEFT && !game.pressed {
game.keys = RIGHT;
}
game.pressed = true;
} else if al_key_down(state,ALLEGRO_KEY_ESCAPE) {
game.keys = ESCAPE;
} else if al_key_down(state,ALLEGRO_KEY_ENTER) {
game.keys = ENTER;
}
} else if events.ty == ALLEGRO_EVENT_DISPLAY_CLOSE {
game.gameOver = true;
}
if events.ty == ALLEGRO_EVENT_TIMER {
game.draw = true;
if game.state == MENU {
if game.keys == ENTER {
SetGame(game);
} else if game.keys == ESCAPE {
game.gameOver = true;
}
} else if game.state == GAMEPLAY {
if game.keys == UP {
var head = game.snake.head as *Node;
head.position.y = head.position.y - BOARD_CELL;
game.pressed = false;
} else if game.keys == DOWN {
var head = game.snake.head as *Node;
head.position.y = head.position.y + BOARD_CELL;
game.pressed = false;
} else if game.keys == LEFT {
var head = game.snake.head as *Node;
head.position.x = head.position.x - BOARD_CELL;
game.pressed = false;
} else if game.keys == RIGHT {
var head = game.snake.head as *Node;
head.position.x = head.position.x + BOARD_CELL;
game.pressed = false;
} else if game.keys == ESCAPE {
game.gameOver = true;
}
} else if game.state == GAMEOVER {
if game.keys == ENTER {
SetGame(game)
} else if game.keys == ESCAPE
{
game.highScore = game.score;
game.gameOver = true;
}
}
}
}
return;
}
pub func (game: *Game) logic() void {
var temp = game.snake.head as *Node;
var headX = temp.position.x;
var headY = temp.position.y;
if headY < 0 {
al_play_sample(game.lose as *ALLEGRO_SAMPLE,0.25F,0.0F,1.0F,ALLEGRO_PLAYMODE_ONCE,null as *ALLEGRO_SAMPLE_ID);
game.state = GAMEOVER;
}
if headY > BOARD_HEIGHT - BOARD_CELL {
al_play_sample(game.lose as *ALLEGRO_SAMPLE,0.25F,0.0F,1.0F,ALLEGRO_PLAYMODE_ONCE,null as *ALLEGRO_SAMPLE_ID);
game.state = GAMEOVER;
}
if headX < 0 || headX > BOARD_WIDTH - BOARD_CELL {
al_play_sample(game.lose as *ALLEGRO_SAMPLE, 0.25F, 0.0F, 1.0F, ALLEGRO_PLAYMODE_ONCE, null as *ALLEGRO_SAMPLE_ID);
game.state = GAMEOVER;
}
temp = temp.next as *Node;
if game.snake.length > 4 {
var stop = false;
while temp as *u8 != null && !stop {
if headX == temp.position.x && headY == temp.position.y {
al_play_sample(game.lose as *ALLEGRO_SAMPLE, 0.25F, 0.0F, 1.0F, ALLEGRO_PLAYMODE_ONCE, null as *ALLEGRO_SAMPLE_ID);
game.state = GAMEOVER;
stop = true;
}
temp = temp.next as *Node;
}
}
if headX == game.fruit.pos.x && headY == game.fruit.pos.y {
al_play_sample(game.point as *ALLEGRO_SAMPLE, 0.25F, 0.0F, 1.0F, ALLEGRO_PLAYMODE_ONCE, null as *ALLEGRO_SAMPLE_ID);
game.score = game.score + 1;
game.snake.extend();
randomFruit(&game.fruit,&game.snake);
}
return;
}
pub func SetGame(game: *Game) void {
if game.state == GAMEOVER {
if game.score > game.highScore {
game.highScore = game.score;
}
deleteList(&game.snake.head as *Node);
}
InitializeSnake(&game.snake);
randomFruit(&game.fruit,&game.snake);
printf("SETGAME\n");
game.state = GAMEPLAY;
game.keys = DOWN;
game.score = 0;
return;
}
pub func (game: *Game) draw() void {
al_draw_filled_rectangle(0.0F,0.0F,BOARD_WIDTH as f32,BOARD_HEIGHT as f32 + 50 as f32,al_map_rgb(255 as u8,255 as u8,255 as u8));
al_draw_line(0.0F,BOARD_HEIGHT as f32,BOARD_WIDTH as f32,BOARD_HEIGHT as f32,al_map_rgb(0 as u8,0 as u8,0 as u8),2.0F);
al_draw_textf(game.font_30 as *ALLEGRO_FONT,al_map_rgb(0 as u8,0 as u8,0 as u8),BOARD_WIDTH as f32 / 2.0F,BOARD_HEIGHT as f32 + 15.0F,ALLEGRO_ALIGN_CENTRE,"Score %d",game.score);
game.snake.draw();
game.fruit.draw();
return;
}
pub func (game: *Game) update() void {
game.snake.update();
return;
}
pub func main() void {
var game: *Game = malloc(sizeof(Game)) as *Game;
printf("Game state created\n");
SetupGame(game);
printf("Game setup complete\n");
game.run();
return;
}