-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroid.cpp
155 lines (130 loc) · 2.14 KB
/
asteroid.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
#include <cstdlib>
#include "asteroid.h"
#include "mainwindow.h"
using namespace std;
/**
* Constructor
*
* @param coordinate x
* @param coordinate y
* @param pointer to parent window
* @param pointer to parent scene
*/
Asteroid::Asteroid(double nx, double ny, MainWindow *mw, QGraphicsScene *s) :
QGraphicsRectItem(nx, ny, 60, 60) {
x = nx;
y = ny;
width = 60;
height = 60;
velocityX = 4;
velocityY = 2;
life= 2;
window= mw;
scene= s;
image= new QGraphicsPixmapItem(QPixmap("images/asteroid.png"), this);
}
/**
* Destructor
*/
Asteroid::~Asteroid(){
}
/**
* Take away one life
*/
bool Asteroid::hit(){
--life;
if(life<1){
return true;
}
else{
return false;
}
}
/**
* Accessor for velocityX
*/
int Asteroid::getVelocityX() {
return velocityX;
}
/**
* Accessor for velocityY
*/
int Asteroid::getVelocityY() {
return velocityY;
}
/**
* Change velocityX
*
* @param new value
*/
void Asteroid::setVelocityX( int vx ) {
velocityX = vx;
}
/**
* Change velocityY
*
* @param new value
*/
void Asteroid::setVelocityY( int vy ) {
velocityY = vy;
}
/**
* Move the object
*
* @param length of window
* @param height of window
*/
void Asteroid::move( int windowMaxX, int windowMaxY ) {
x += velocityX;
y += velocityY;
if ( x < 0 ) {
velocityX = -velocityX;
x +=velocityX;
}
if ( y < -100 ) {
velocityY = -velocityY;
y +=velocityY;
}
if ( (x+width) > windowMaxX+200 ) {
velocityX = -velocityX;
x+=velocityX;
}
if ( (y+height) > windowMaxY+200 ) {
velocityX= 0;
velocityY= 0;
window->destroyAsteroid(this);
}
QPointF p( x, y );
QRectF r( rect() );
r.moveTo(p);
setRect( r );
image->setPos(x,y);
}
/**
* Accessor for X
*/
int Asteroid::getX() {
return x;
}
/**
* Accessor for Y
*/
int Asteroid::getY() {
return y;
}
/**
* Change X
*
* @param new x coordinate
*/
void Asteroid::setX(int nx) {
x = nx;
}
/**
* Change Y
*
* @param new y coordinate
*/
void Asteroid::setY(int ny) {
y = ny;
}