-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.c
90 lines (80 loc) · 2.13 KB
/
controllers.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
#include "controllers.h"
#include "snake.h"
#include "timer.h"
#include "window.h"
#include <raylib.h>
#include <stdbool.h>
void draw_grid(int cols, int rows, float cell_width, float cell_height) {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
DrawRectangleLines(i * cell_width, j * cell_height, cell_width,
cell_height, LIGHTGRAY);
}
}
}
void handle_keys(Snake *snake, GameState *state) {
if (IsKeyDown(KEY_SPACE)) {
*state = PAUSE;
} else if (IsKeyDown(KEY_RIGHT) && snake->direction != LEFT) {
snake->direction = RIGHT;
} else if (IsKeyDown(KEY_LEFT) && snake->direction != RIGHT) {
snake->direction = LEFT;
} else if (IsKeyDown(KEY_UP) && snake->direction != DOWN) {
snake->direction = UP;
} else if (IsKeyDown(KEY_DOWN) && snake->direction != UP) {
snake->direction = DOWN;
}
}
void update_game(Snake *snake, Apple *apple, GameState *state) {
Vector2 *snake_head = &snake->pos[0];
switch (snake->direction) {
case UP:
snake_head->y -= 1;
break;
case DOWN:
snake_head->y += 1;
break;
case LEFT:
snake_head->x -= 1;
break;
case RIGHT:
snake_head->x += 1;
break;
default:
break;
}
// apple eaten?
if (snake_head->x == apple->pos.x && snake_head->y == apple->pos.y) {
start_timer(&apple->timer, apple->timer.lifetime);
PlaySound(apple->eating_sound);
snake->score++;
snake->length++;
apple->eaten = true;
}
// collision happened?
if (snake_head->x < 0 ||
snake_head->x >= COLS ||
snake_head->y < 0 ||
snake_head->y >= ROWS)
{
*state = OVER;
return;
}
// self collision?
for (int i = 1; i < snake->length; i++) {
if (snake_head->x == snake->pos[i].x && snake_head->y == snake->pos[i].y) {
*state = OVER;
return;
}
}
// implementation of the movement of the snake from frame to frame
for (int i = snake->length - 1; i > 0; i--) {
snake->pos[i] = snake->pos[i - 1];
}
update_timer(&apple->timer);
draw_snake(snake);
}
void restart_game(Snake *snake, GameState *state) {
init_snake(snake);
*state = PLAYING;
}