-
Notifications
You must be signed in to change notification settings - Fork 1
/
brick.cpp
218 lines (188 loc) · 5.48 KB
/
brick.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @file brick.cpp
* @brief Definintion of member functions for the Brick class
* @author Bronson Schoen
* @date 3/6/2016
*/
#include "brick.h"
/** Default constructor for Brick.
*/
Brick::Brick()
{
//set to not hit yet
hit = false;
//initialize animation settings
width = 60;
height = 20;
pen_width = 1;
//set powerups
shrink_powerup = false;
grow_powerup = false;
slower_powerup = false;
faster_powerup = false;
//give us starting position
start_vector.setX(250);
start_vector.setY(250);
setPos(mapToParent(start_vector.x(), start_vector.y()));
}
/** Constructor for Brick. Sets brick at given x,y position.
* @param start_x_position starting x coordinate in scene
* @param start_y_position starting y coordinate in scene
*/
Brick::Brick(int start_x_position, int start_y_position)
{
//set to not hit yet
hit = false;
//initialize animation settings
width = 60;
height = 20;
pen_width = 1;
//set powerups
shrink_powerup = false;
grow_powerup = false;
slower_powerup = false;
faster_powerup = false;
//give us starting position
start_vector.setX(start_x_position);
start_vector.setY(start_y_position);
setPos(mapToParent(start_vector.x(), start_vector.y()));
}
/** Constructor for Brick. Sets brick at given x,y position. Sets powerups for Brick.
* @param start_x_position starting x coordinate in scene
* @param start_y_position starting y coordinate in scene
* @param shrink_powerup_brick boolean value for whether or not brick contains the shrink powerup
* @param grow_powerup_brick boolean value for whether or not brick contains the grow powerup
* @param slower_powerup_brick boolean value for whether or not brick contains the slower powerup
* @param faster_powerup_brick boolean value for whether or not brick contains the faster powerup
*/
Brick::Brick(int start_x_position, int start_y_position,
bool shrink_powerup_brick,
bool grow_powerup_brick,
bool slower_powerup_brick,
bool faster_powerup_brick)
{
//set to not hit yet
hit = false;
//initialize animation settings
width = 60;
height = 20;
pen_width = 1;
//set powerups
shrink_powerup = shrink_powerup_brick;
grow_powerup = grow_powerup_brick;
slower_powerup = slower_powerup_brick;
faster_powerup = faster_powerup_brick;
//give us starting position
start_vector.setX(start_x_position);
start_vector.setY(start_y_position);
setPos(mapToParent(start_vector.x(), start_vector.y()));
}
/** Set bounding rectangle for drawing and collisions
@returns bounding rectange for shape
*/
QRectF Brick::boundingRect() const
{
return QRectF(0,
0,
width + pen_width,
height + pen_width);
}
/** Paints object with correct shape in local coordinates. Determines if hit and colors according to powerup.
@param *painter QPainter object resposible for painting the object in the scene
@param *option provides style options for the item, such as its state, exposed area and its level-of-detail hints
@param *widget Optional. If provided, it points to the widget that is being painted on; otherwise, it is 0.
*/
void Brick::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->setBrush(Qt::black);
if(scene()->collidingItems(this).isEmpty() && !hit)
{
//color according to powerup
if(shrink_powerup)
painter->setBrush(Qt::yellow);
else if(grow_powerup)
painter->setBrush(Qt::cyan);
else if(slower_powerup)
painter->setBrush(Qt::red);
else if(faster_powerup)
painter->setBrush(Qt::green);
//color normal bricks blue
else
painter->setBrush(Qt::blue);
}
else
{
hit=true;
}
//should match shape in Brick::shape
painter->drawRect(0, 0, width, height);
painter->setBrush(Qt::NoBrush);
}
/** Gives an accurate shape, which is used by QGraphicsScene to animate.
@returns path QPainterPath to draw representing shape
*/
//Gives an accurate shape, which is used by QGraphicsScene to animate
QPainterPath Brick::shape() const
{
QPainterPath path;
//should match shape in Brick::shape
path.addRect(0, 0, width, height);
return path;
}
/** Getter for width member variable
* @return returns value of width member variable
*/
qreal Brick::get_width()
{
return width;
}
/** Getter for height member variable
* @return returns value of height member variable
*/
qreal Brick::get_height()
{
return height;
}
/** Set hit member variable to hit_value
* @param hit_value value to set member variable hit to
*/
void Brick::set_hit(bool hit_value)
{
hit=hit_value;
}
/** Getter for hit member variable
* @return boolean value of hit
*/
bool Brick::is_hit()
{
return hit;
}
/** Getter for shrink_powerup member variable
* @return boolean value of shrink_powerup
*/
bool Brick::is_shrink_powerup()
{
return shrink_powerup;
}
/** Getter for grow_powerup member variable
* @return boolean value of grow_powerup
*/
bool Brick::is_grow_powerup()
{
return grow_powerup;
}
/** Getter for slower_powerup member variable
* @return boolean value of slower_powerup
*/
bool Brick::is_slower_powerup()
{
return slower_powerup;
}
/** Getter for faster_powerup member variable
* @return boolean value of faster_powerup
*/
bool Brick::is_faster_powerup()
{
return faster_powerup;
}