-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFood.cpp
122 lines (99 loc) · 3.45 KB
/
Food.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*!\file Food.cpp
*
* \brief Represents a Food piece.
*
* \author Chad Martin
*
* The parent of all food pieces. If not inherited, Food represents a red or green Apple Logo.
*/
#include "Food.h"
/*! \brief Class Constructor - loads first image, moves to random location.
* Precondition: A new instance of game was created.
* Postcondition: A new piece of food is created with default image.*/
Food::Food()
{
image.load("food1.png");
rect = image.rect();
pickLocation(); //Picks a random location to move Food
rect.moveTo(x,y); // Moves Food to location
}
/*! \brief Class Destructor - Does Nothing*/
Food::~Food()
{
}
/*! \brief Draws Food in a random location.
* Precondition: The Snake head collided with a piece of Food or Food cheat code key was pressed.
* Postcondition: Food is moved to a random location on the screen which is not on top of otherFood.
* \param *otherFood The other piece of Food on the screen.*/
void Food::moveFood(Food *otherFood)
{
do{
pickLocation(); //Picks a random location to move Food to
rect.moveTo(x, y); //moves Food to chosen location
/* Resovles illegal food placement issues */
}while(rect.intersects(otherFood->getRect())); // NO SANDWICHES ALLOWED!!!!
}
/*! \brief Draws Food in a random location.
* Precondition: The Snake head collided with a piece of Food or Food cheat code key was pressed.
* Postcondition: Food is moved to a random location on the screen which is not on top of otherFood.
* \param *otherFood The other piece of Food on the screen.
* \param whichFood The integer switch between Food images.*/
void Food::moveFood(Food *otherFood, int whichFood)
{
do{
pickLocation(); //Picks a random location to move Food to
//switches between images
if(whichFood == 0)
{
image.load("food1.png");
}
if(whichFood == 1)
{
image.load("food2.png");
}
rect.moveTo(x, y); //moves Food to chosen location
/* Resovles illegal food placement issues */
}while(rect.intersects(otherFood->getRect())); // NO SANDWICHES ALLOWED!!!!
}
/*! \brief Draws Food in a random location.
* Precondition: The Snake head collided with a piece of Food or Food cheat code key was pressed.
* Postcondition: Food is moved to a random location on the screen which is not on top of otherFood.
* \param whichFood The integer switch between Food images.*/
void Food::moveFood(int whichFood)
{
pickLocation(); //Picks a random location to move Food to
//switches between images
if(whichFood == 0)
{
image.load("food1.png");
}
if(whichFood == 1)
{
image.load("food2.png");
}
rect.moveTo(x, y);//moves Food to chosen location
}
/*! \brief Picks a random location to move Food to, but does not move Food.
* Precondition: Food needs to be moved to a new location.
* Postcondition: New location is picked for Food to move to.*/
void Food::pickLocation()
{
x = ((rand() % 16)*25)+28; //picks random x position to spawn food
y = ((rand() % 20)*25)+150; //picks random y position to spawn food
}
/*! \brief Returns rect of Food.
* Precondition: Any collision detection or paint event that involes the Food occured.
* Postcondition: The Qrect of the Food is returned.
* \return The rect of Food.*/
QRect Food::getRect()
{
return rect;
}
/*! \brief Returns image of Food.
* Precondition: Instance of Game called a paint event and the game is started.
* Postcondition: The image of the Food will be painted on the screen.
* \return Image of Food.*/
QImage & Food::getImage()
{
return image;
}