-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbullet2.cpp
More file actions
63 lines (55 loc) · 1.63 KB
/
bullet2.cpp
File metadata and controls
63 lines (55 loc) · 1.63 KB
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
#include "bullet2.h"
#include "enemy.h"
#include "enemy2.h"
#include "galaga.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include <typeinfo>
//we can access global object Galaga
extern Galaga * galaga;
BulletTwo::BulletTwo(QGraphicsItem * parent):QObject(), QGraphicsPixmapItem(parent)
{
//making bullet
setPixmap(QPixmap(":/images/bullet2.png"));
//connecting timer(signal) to slot
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
//every 50 ms the bullet will move
timer->start(50);
}
void BulletTwo::move()
{
//checking to see if it collides with enemy
QList<QGraphicsItem*> colliding = collidingItems();
for(int i = 0; i<colliding.size(); i++){
if(typeid(*(colliding[i])) == typeid(Enemy)){
//increases the score
galaga->score->increaseScore();
//remove both
scene()->removeItem(colliding[i]);
scene()->removeItem(this);
//delete both
delete colliding[i];
delete this;
return;
}
if(typeid(*(colliding[i])) == typeid(EnemyTwo)){
//increases the score
galaga->score->increaseScore();
//remove both
scene()->removeItem(colliding[i]);
scene()->removeItem(this);
//delete both
delete colliding[i];
delete this;
return;
}
}
//move bullet up
setPos(x(),y()-20);
if (pos().y() < 0){
scene()->removeItem(this);
delete this;
}
}