-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollision.c
More file actions
57 lines (45 loc) · 1.25 KB
/
collision.c
File metadata and controls
57 lines (45 loc) · 1.25 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
#include "collision.h"
#include <unistd.h>
#include <stdbool.h>
static bool within_screen(Snake* snake, Screen* screen) {
if (snake->head->x_pos >= screen->max_x || snake->head->y_pos >= screen->max_y
|| snake->head->x_pos < 0 || snake->head->y_pos < 0) {
return false; // The snake has hit a wall
} else {
return true;
}
}
static bool check_autophagy(Snake* snake) {
Segment* head = snake->head;
Segment* seg = head->next;
while(seg != NULL) {
if (seg->x_pos == head->x_pos && seg->y_pos == head->y_pos) {
return true;
}
seg = seg->next;
}
return false;
}
static bool check_food(Snake* snake, FoodList* food_list) {
Segment* head = snake->head;
FoodItem* fi = food_list->first;
while(fi != NULL) {
if (fi->x_pos == head->x_pos && fi->y_pos == head->y_pos) {
return true;
}
fi = fi->next;
}
return false;
}
Collision find_collision(Snake* snake, FoodList* food_list, Screen* screen) {
if (!within_screen(snake, screen)) {
return SCREEN;
}
if (check_autophagy(snake)) {
return SNAKE;
}
if (check_food(snake, food_list)) {
return FOOD;
}
return NONE;
}