-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.cpp
45 lines (32 loc) · 996 Bytes
/
bullet.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
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "bullet.h"
#include "enemy.h"
#include"gamebase.h"
extern GameBase *game;
Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){
QPixmap pixmap(":/new/images/resource/images/fire.jpg");
setPixmap(pixmap.scaled(10,10));
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
timer->start(50);
}
void Bullet::move(){
QList<QGraphicsItem *> colliding_items = collidingItems();
for (int i = 0, n = colliding_items.size(); i < n; ++i){
if (typeid(*(colliding_items[i])) == typeid(Enemy)){
game->score->increase();
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
delete colliding_items[i];
delete this;
return;
}
}
setPos(x(),y()-10);
if (pos().y() < 0){
scene()->removeItem(this);
delete this;
}
}