-
Notifications
You must be signed in to change notification settings - Fork 1
/
paddle.cpp
224 lines (185 loc) · 5.61 KB
/
paddle.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
219
220
221
222
/**
* @file paddle.cpp
* @brief Definintion of member functions for the Paddle class
* @author Bronson Schoen
* @date 3/6/2016
*/
#include "paddle.h"
/** Default constructor for Paddle
*/
Paddle::Paddle()
{
//initialize animation settings
width = 200;
height = 15;
starting_width = width;
starting_height = height;
pen_width = 1;
//set movement distance for key events
default_movement_distance = 20;
starting_movement_distance = default_movement_distance;
//set size_change_amount for shrinking and growing
size_change_amount = 50;
//set speed_change_amount for faster and slower
speed_change_amount = 6;
//give us starting position
start_vector.setX(170);
start_vector.setY(440);
setPos(mapToParent(start_vector.x(), start_vector.y()));
}
/** Constructor for Paddle. Sets paddle at given x,y position.
* @param start_x_position starting x coordinate in scene
* @param start_y_position starting y coordinate in scene
*/
Paddle::Paddle(int start_x_position, int start_y_position)
{
//initialize animation settings
width = 200;
height = 15;
starting_width = width;
starting_height = height;
pen_width = 1;
//set movement distance for key events
default_movement_distance = 20;
starting_movement_distance = default_movement_distance;
//set size_change_amount for shrinking and growing
size_change_amount = 50;
//set speed_change_amount for faster and slower
speed_change_amount = 6;
//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 Paddle::boundingRect() const
{
return QRectF(0,
0,
width,
height);
}
/** Paints object with correct shape in local coordinates. 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 Paddle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
//color according to paddle speed
//color faster paddle same color as faster powerup
if(default_movement_distance > starting_movement_distance)
painter->setBrush(Qt::green);
//color slower paddle same color as slower powerup
else if(default_movement_distance < starting_movement_distance)
painter->setBrush(Qt::red);
//color normal paddle
else
painter->setBrush(Qt::magenta);
//should match shape in Paddle::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
*/
QPainterPath Paddle::shape() const
{
QPainterPath path;
//should match shape in Paddle::shape
path.addRect(0, 0, width, height);
return path;
}
/** Move the paddle default_movement_distance number of units to the left
*/
void Paddle::move_left()
{
//update position based on moving to the left
QPointF updated_position = mapToParent(-default_movement_distance,0);
//if the new position would be out of bounds, don't allow the movement
if(!scene()->sceneRect().contains((updated_position)))
{
return;
}
else
setPos(updated_position);
}
/** Move the paddle default_movement_distance number of units to the right
*/
void Paddle::move_right()
{
//update position based on moving to the right
QPointF updated_position = mapToParent(default_movement_distance,0);
//adjust for width of paddle
QPointF updated_adjusted_position = mapToParent(default_movement_distance+width,0);
//if the new position would be out of bounds, don't allow the movement
if(!scene()->sceneRect().contains((updated_adjusted_position)))
{
return;
}
else
setPos(updated_position);
}
/** Getter for width member variable
* @return returns value of width member variable
*/
qreal Paddle::get_width()
{
return width;
}
/** Getter for height member variable
* @return returns value of height member variable
*/
qreal Paddle::get_height()
{
return height;
}
/** Setter for width member variable
* @param new_width new width value to set width to
*/
void Paddle::set_width(qreal new_width)
{
width = new_width;
}
/** Setter for height member variable
* @param new_height new height value to set height to
*/
void Paddle::set_height(qreal new_height)
{
height = new_height;
}
/** Decrease width of paddle by size_change_amount
*/
void Paddle::shrink()
{
width -= size_change_amount;
}
/** Increase width of paddle by size_change_amount
*/
void Paddle::grow()
{
width += size_change_amount;
}
/** Decrease default_movement_distance of paddle by speed_change_amount
*/
void Paddle::slower()
{
default_movement_distance -= speed_change_amount;
}
/** Increase default_movement_distance of paddle by speed_change_amount
*/
void Paddle::faster()
{
default_movement_distance += speed_change_amount;
}
/** Undoes the effect of any powerups by returning paddle back to starting width, height, and movement_distance
*/
void Paddle::reset_powerups()
{
width = starting_width;
height = starting_height;
default_movement_distance = starting_movement_distance;
}