-
Notifications
You must be signed in to change notification settings - Fork 0
/
Food.c
66 lines (57 loc) · 1.47 KB
/
Food.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
#include "Food.h"
#include "SDL_Util.h"
/**
* Generate food x,y coordinates
*/
void Food_GenerateLoc(Food *food)
{
int x = 10 * (rand() % 25);
int y = 10 * (rand() % 25);
food->x = x;
food->y = y;
food->bounds.x = x;
food->bounds.y = y;
}
/**
* Creates the food
*/
struct _Food * Food_Create()
{
SDL_Rect r = SDL_CreateRect(10, 10, 0, 0);
struct _Food *food = malloc(sizeof(struct _Food));
food->bounds = r;
Food_GenerateLoc(food);
food->surf = SDL_CreateRGBSurface(0, 10, 10, 32,
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF);
r.h = 10; r.w = 10;
r.x = 0; r.y = 0;
SDL_FillRect(food->surf, &r, 0x666666FF);
SDL_GridFilter(food->surf->pixels, food->surf->pitch, 0xAA, 10, 10, 4);
return food;
}
/**
* Cleans up the food
*/
void Food_Free(Food *food)
{
SDL_FreeSurface(food->surf);
free(food);
}
/**
* Moves the food to a new location
*/
void Food_Move(Food *food, Snake *snake)
{
Food_GenerateLoc(food);
Body *body = (snake)->body;
while (body != NULL) {
if (body->value.x == food->x && body->value.y == food->y) {
Food_Move(food, snake);
break;
}
body = body->next;
}
}