-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfood.cpp
More file actions
52 lines (43 loc) · 960 Bytes
/
food.cpp
File metadata and controls
52 lines (43 loc) · 960 Bytes
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
#include "food.h"
using namespace std;
Food::Food(int x, int y) : Fl_Box(x, y, 20, 20)
{
this->box(FL_OFLAT_BOX);
this->color(FL_RED);
this->x = x;
this->y = y;
this->show();
}
void Food::move(int maxX, int maxY, vector<Segment*> body)
{
this->hide();
//jump to a random position within bounds (max x/y)
int newX, newY;
do
{
//randomize coordinates as long as they are invalid (blocked by snake body)
newX = (rand() % maxX/20)*20;
newY = (rand() % maxY/20)*20;
} while (!validPositon(newX, newY, body));
this->position(newX, newY); //move the segment to its new position
this->x = newX; //update segment coordinates
this->y = newY;
this->show();
}
bool Food::validPositon(int x, int y, vector<Segment*> body)
{
for (int i = 0; i < body.size(); i++)
{
if (body.at(i)->getX() == x && body.at(i)->getY() == y)
return false;
}
return true;
}
int Food::getX()
{
return this->x;
}
int Food::getY()
{
return this->y;
}