You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

2 years ago
#include "bullet.h"
#include <QTimer>
2 years ago
#include <QGraphicsScene>
#include <QList>
#include "enemy.h"
2 years ago
#include "game.h"
2 years ago
2 years ago
extern Game* game;
2 years ago
2 years ago
Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){
setPixmap(QPixmap(":/images/missile2.png"));
show();
2 years ago
QTimer* timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50);
}
2 years ago
void Bullet::move(){
// if bullet collides with enemy, destroy both
QList<QGraphicsItem *> colliding_items = collidingItems();
for (int i = 0, n = colliding_items.size(); i < n; i++) {
if (typeid(*(colliding_items[i])) == typeid(Enemy)) {
2 years ago
game->score->increase();
2 years ago
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
delete colliding_items[i];
delete this;
return;
}
}
2 years ago
this->setPos(x(), y()-10);
2 years ago
if(pos().y() < 0) {
2 years ago
scene()->removeItem(this);
delete this;
qDebug() << "bullet deleted";
}
2 years ago
}