-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfood.cpp
More file actions
52 lines (46 loc) · 1.25 KB
/
food.cpp
File metadata and controls
52 lines (46 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
#include "food.h"
#include "menu.h"
Food::Food()
{
setImages();
}
void Food::setImages()
{
foodImg = newimage();
getimage(foodImg, "./cpp-snake-ege/assets/china.jpg");
// getimage(foodImg, "./assets/china.jpg"); // exe file
}
void Food::draw(vector<Point>& wall_position, Snake& snake, int ifAteFood)
{
generateRandomly(wall_position, snake, ifAteFood);
putimage(food.x, food.y, foodImg);
}
void Food::generateRandomly(vector<Point>& wall_position, Snake& snake, int ifAteFood)
{
if (ifAteFood)
{
int x = rand() % ((WINDOW_WIDTH - 2 * GRID_SIZE) / GRID_SIZE) * GRID_SIZE + GRID_SIZE;
int y = rand() % ((WINDOW_HEIGHT - 2 * GRID_SIZE) / GRID_SIZE) * GRID_SIZE + GRID_SIZE;
for (auto &p : wall_position)
{
if (x == p.x && y == p.y)
{
generateRandomly(wall_position, snake, ifAteFood);
return;
}
}
for (auto &p : snake.getBody())
{
if (x == p.x && y == p.y)
{
generateRandomly(wall_position, snake, ifAteFood);
return;
}
}
food = {x, y};
}
}
Food::~Food()
{
delimage(foodImg);
}