我对 Qt 很陌生,但我开始尝试制作 2D 游戏。我有一个非常粗糙和简单的游戏开始,但我有一个问题。每当生命值变为 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;
}
健康.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;
}
主文件
#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();
}