如何在QT中创建动画爆炸

时间:2015-12-11 18:17:25

标签: c++ qt animation

我正在创造太空射击游戏,并希望添加一些特效:)我的意思是,当子弹击中目标时,敌人会爆炸。 我有一些爆炸的图片(步骤),我也使用QGraphicsPixmapItem类和setPixmap()方法来设置敌人的pixmap。 我的子弹和敌人类:

bullet.h

#ifndef BULLET_H
#define BULLET_H

#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QObject>
#include <QPropertyAnimation>

class Bullet : public QPropertyAnimation, public QGraphicsPixmapItem{
    Q_OBJECT
public:
    Bullet(QGraphicsItem * parent=0);
    static int howManyKilled() { return killed; }
    static int howManyMissed() { return missed; }
    static void resetStats() { killed = 0; missed = 0; } // to avoid bad multipler calculate
    void setAccurancy() { accurancy = killed/missed * 100; }
    static double accurancyInPercentage() { return accurancy; }
public slots:
    void move();
private:
    static int killed;
    static int missed;
    static double accurancy;
};

#endif // BULLET_H

bullet.cpp

#include "Bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "Enemy.h"
#include "Game.h"
#include <typeinfo>
#include "levels.h"

extern Game * game; // there is an external global object called game
int Bullet::killed = 0;
int Bullet::missed = 0;
double Bullet::accurancy = 0;

Bullet::Bullet(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
    // draw graphics
    setPixmap(QPixmap(":/images/res/images/bullets/bullet.png"));
    missed++; // increse missed when bullet is created

    // make/connect a timer to move() the bullet every so often
    QTimer * timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(move()));

    // start the timer
    timer->start(3);
}
/*
void Bullet::explosion()
{
    setPixmap(QPixmap(":/images/res/images/effects/explosion/1_12.png"));
}
*/
void Bullet::move(){
    // get a list of all the items currently colliding with this bullet
    QList<QGraphicsItem *> colliding_items = collidingItems();

    // if one of the colliding items is an Enemy, destroy both the bullet and the enemy
    for (int i = 0, n = colliding_items.size(); i < n; ++i){
        if (typeid(*(colliding_items[i])) == typeid(Enemy)){
            // increase the score
            game->score->increase();

            //play explosion animation
            //Bullet::explosion();
            //QTimer::singleShot()
            //explosion = new QPropertyAnimation(this);
            //explosion->setDuration(300);
            //explosion->


            // remove them from the scene (still on the heap)
            scene()->removeItem(colliding_items[i]);
            scene()->removeItem(this);

            // delete them from the heap to save memory
            delete colliding_items[i];
            delete this;

            killed++;
            missed--; // decrese missed if bullet colide with enemy
            if((killed+1) % 9 == 0)
            {
                game->level->Levels::incrementLevels();
                game->score->Score::addToSum(); /// TODO
            }

            //qDebug() << "Already killed: " << killed;
            //qDebug() << "Already missed: " << missed;
            // return (all code below refers to a non existint bullet)
            return;
        }
    }

    // if there was no collision with an Enemy, move the bullet forward
    setPos(x(),y()-1);
    // if the bullet is off the screen, destroy it
    if (pos().y() < 0){
        scene()->removeItem(this);
        delete this;
    }
}

enemy.h

#ifndef ENEMY_H
#define ENEMY_H

#include <QGraphicsPixmapItem>
#include <QObject>
#include <QGraphicsItem>

class Enemy: public QObject,public QGraphicsPixmapItem{
    Q_OBJECT
public:
    Enemy(QGraphicsItem * parent=0);
public slots:
    void move();
};

#endif // ENEMY_H

enemy.cpp

#include "Enemy.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include <stdlib.h> // rand()
#include <ctime>
#include "Game.h"
#include "hp.h"
#include <typeinfo>

extern Game * game;
extern unsigned int width;
extern unsigned int height;

Enemy::Enemy(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){
    //set random x position
    //srand( time( NULL ) );
    int random_number = rand()% (width-200) + 100;
    //qDebug() << random_number;
    setPos(random_number,0);

    // drew the rect
    setPixmap(QPixmap(":/images/res/images/enemies/3.png"));
    setTransformOriginPoint(50,50);
    //setRotation(180);

    // make/connect a timer to move() the enemy every so often
    QTimer * timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(move()));

    // start the timer
    timer->start(10);
}

void Enemy::move(){
    ///////////////////////// COLISIONS /////////////////////////////////////
    // get a list of all the items currently colliding with this bullet
    QList<QGraphicsItem *> colliding_items = collidingItems();

    // if one of the colliding items is an Enemy, destroy both the bullet and the enemy
    for (int i = 0, n = colliding_items.size(); i < n; ++i){
        if (typeid(*(colliding_items[i])) == typeid(Player)){
            // increase the score
            game->hp->decrementLives();

            // remove them from the scene (still on the heap)
            scene()->removeItem(this);

            // delete them from the heap to save memory
            delete this;

            // return (all code below refers to a non existint bullet)
            return;
        }
    }
    ///////////////////////////////////////////////////////////////////////////////////////
    // if there was no collision with an Enemy, move the bullet forward
    // move enemy down

    if (Levels::howManyLevels() < 10)
    {
        setPos(x(),y() + Levels::howManyLevels());
    }
    else
    {
        setPos(x(),y() + 10);
    }

    // destroy enemy when it goes out of the screen
    if (pos().y() > height){
        //decrease the health
        game->hp->decrementLives();

        scene()->removeItem(this);
        delete this;
    }
}

0 个答案:

没有答案