1

私は Qt にまったく慣れていませんが、2D ゲームの作成を実験するために Qt に取り掛かりました。非常にラフでシンプルなゲームを開始しましたが、問題があります。ヘルスが 0 になっても、ゲームは終了しません。「ゲームオーバー」画面を作成する前に、ゲームを終了する方法と、この終了コマンドをどこに置くかを知りたいだけです。私のコードは以下のとおりです。私が把握できることから、 QApplication::quit() が Game.cpp ファイルに含まれていると想定しています。これを行うには、Health.cpp と Health.h からヘルス整数を取得し、それを Game.cpp に入れます。どんな助けでも大歓迎です。これが答えがあると思うコードです。さらに情報が必要な場合は、尋ねてください。

ゲーム.h

#ifndef GAME_H
#define GAME_H

#include <QGraphicsView>
#include <QWidget>
#include <QGraphicsScene>
#include "Player.h"
#include "Score.h"
#include "Health.h"
#include "Level.h"
#include "Main.h"

class Game: public QGraphicsView{
public:
    Game(QWidget * parent=0);

    QGraphicsScene * scene;
    Player * player;
    Score * score;
    Health * health;
    Level * level;
    Main * close;
    int end();
};

#endif // GAME_H

ゲーム.cpp

#include "Game.h" 
#include <QTimer>
#include <QGraphicsTextItem>
#include <QFont>
#include "Enemy.h"
#include <QMediaPlayer>
#include <QBrush>
#include <QImage>
#include <QApplication>

Game::Game(QWidget *parent){
    // create the scene
    scene = new QGraphicsScene();
    scene->setSceneRect(0,0,800,600); // make the scene 800x600        instead of infinity by infinity (default)
    setBackgroundBrush(QBrush(QImage(":/images/bg.png")));

    // make the newly created scene the scene to visualize (since Game is a QGraphicsView Widget,
    // it can be used to visualize scenes)
    setScene(scene);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setFixedSize(800,600);

    // create the player
    player = new Player();
    player->setPos(400,500); // TODO generalize to always be in the     middle bottom of screen
    // make the player focusable and set it to be the current focus
    player->setFlag(QGraphicsItem::ItemIsFocusable);
    player->setFocus();
    // add the player to the scene
    scene->addItem(player);

    // create the score/health
    score = new Score();
    scene->addItem(score);
    health = new Health();
    health->setPos(health->x(),health->y()+25);
    scene->addItem(health);
    level = new Level();
    scene->addItem(level);Bull
    level->setPos(level->x(),level->y()+50);

    // spawn enemies
    QTimer * timer = new QTimer();
    QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
    timer->start(2000);

    // play background music
    QMediaPlayer * music = new QMediaPlayer();
    music->setMedia(QUrl("qrc:/sounds/bgsound.mp3"));
    music->play();

    show();
}

int Game::end(){
    if (health == 0){
        QApplication::quit();
    }
    return 0;
}

Health.h

#ifndef HEALTH_H
#define HEALTH_H

#include <QGraphicsTextItem>

class Health: public QGraphicsTextItem{
public:
    Health(QGraphicsItem * parent=0);
    void decrease();
    int getHealth();
private:
    int health;
};

#endif // HEALTH_H

健康.cpp

#include "Health.h"
#include <QFont>
#include <QApplication>

Health::Health(QGraphicsItem *parent): QGraphicsTextItem(parent){
    // initialize the score to 0
    health = 3;

    // draw the text
    setPlainText(QString("Health: ") + QString::number(health)); //   Health: 3
    setDefaultTextColor(Qt::red);
    setFont(QFont("times",16));
}

void Health::decrease(){
    health--;
    setPlainText(QString("Health: ") + QString::number(health)); //     Health: 2
}

int Health::getHealth(){
    return health;
}

main.cpp

#include <QApplication>
#include "Game.h"
#include "Main.h"

Game * game;

int main(int argc, char *argv[]){
    QApplication a(argc, argv);

    game = new Game();
    game->show();

    return a.exec();
}
4

2 に答える 2

2

関数が呼び出されることはありませend()ん。

目的を達成するための最良の方法は、 Qt のシグナル/スロット メカニズムを使用することです。イベント (シグナル) をアクション (スロット) に接続するのは簡単です。

  • Q_OBJECTクラスにマクロをHealth追加しGame、コンパイル環境の mocが 2 つのヘッダー ファイルであることを確認します。
  • 名前付きHealthで宣言するsignaldead()
  • から信号を発するHealth::decrease()
  • Game::end()なるslot_void
  • 接続Health::dead()するGame::end()

その後、ゼロGame::end()になるとすぐに呼び出されHealthます。

class Health: public QGraphicsTextItem 
{
    Q_OBJECT
public:
    Health(QGraphicsItem * parent=0);
    void decrease();
    int getHealth();
signals:
    void dead();
private:
    int health;
};

...

class Game: public QGraphicsView{
    Q_OBJECT
public:
    ...

public slots:
    void end();
};

...

void Health::decrease(){
    health--;
    setPlainText(QString("Health: ") + QString::number(health));
    if ( health == 0 )
        emit dead();
}

...

Game::Game(QWidget *parent){

    ...

    connect( health, SIGNAL(dead()), this, SLOT(end()) );

}

...

void Game::end(){
    // no need to test health anymore, as signal is emited when health is zero
    // do some extra stuff before exiting
    QApplication::quit();
}

end()のみを呼び出す場合はQApplication::quit()、それを削除して、次のQApplication::quit()ように信号を に直接接続できます。

connect( health, SIGNAL(dead()), qApp, SLOT(quit()) );

また、 でテストhealth == 0していましGame::end()たがhealth、 はポインターであり、コードを見ると決してそうではないことに注意してください0( if ( health->getHealth() == 0 ).

于 2016-02-08T19:03:10.377 に答える
0

Health::decrease信号を発信するか、「プレーヤーが撃たれた」領域にそのロジックを配置します 。しかし、それを行うには、ヘッダー内のシグナルとスロットを取得するようにHealth、ロジックを備えたクラスにする必要があります。QObjectEDITQGraphicsTextItemはすでにQObject. コメントを参照してください。

close()Health クラスまたは Player クラスがインスタンス化された後、信号をビューの右側に接続します。

http://doc.qt.io/qt-5/signalsandslots.html

それが役立つことを願っています。

于 2016-02-08T19:04:13.190 に答える