-
Notifications
You must be signed in to change notification settings - Fork 1
/
bullet.cpp
executable file
·40 lines (34 loc) · 1.03 KB
/
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
#include "bullet.h"
#include "constants.h"
#include <QTimer>
#include <QGraphicsScene>
#include "enemy.h"
#include "score.h"
#include "game.h"
extern Game *game;
Bullet::Bullet(QGraphicsItem *parent) : QObject(), QGraphicsPixmapItem(parent)
{
setPixmap(QPixmap(":/images/bullet.png"));
QTimer *timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
timer->start(BULLET_MOVE_PAUSE);
}
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() - BULLET_MOVE_POINTS);
if((pos().y() + this->pixmap().height()) < 0){
scene()->removeItem(this);
delete this;
}
}