#include "bullet.h" #include #include #include #include "enemy.h" #include "game.h" extern Game* game; Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){ setPixmap(QPixmap(":/images/missile2.png")); show(); QTimer* timer = new QTimer(); connect(timer, SIGNAL(timeout()), this, SLOT(move())); timer->start(50); } void Bullet::move(){ // if bullet collides with enemy, destroy both QList 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; } } this->setPos(x(), y()-10); if(pos().y() < 0) { scene()->removeItem(this); delete this; qDebug() << "bullet deleted"; } }