如何为我的游戏设置主菜单场景?QT C++
How do I set a Main Menu Scene to my game? QT C++
我对QT和C++完全陌生。我遵循了在线教程,并使用QT创建了我的第一个游戏。游戏基本上是这样的。我是一辆坦克,我可以将坦克移动到场景底角的左右。炸弹从现场的顶部随机落到地面上。我可以通过按空格键向他们射击。当子弹击中炸弹时,分数正在计算......
我的游戏只有一个场景。当我运行程序时,游戏在那个时候开始。我希望我的程序在运行游戏时首先打开主菜单。主菜单应包含两个QPush按钮。它们是开始游戏和退出。我不知道如何实现该部分。
游戏.h
#ifndef GAME_H
#define GAME_H
#include <QGraphicsView>
#include <QWidget>
#include <QGraphicsScene>
#include "Score.h"
#include "Health.h"
#include "Player.h"
class Game: public QGraphicsView{
public:
Game(QWidget * parent=0);
QGraphicsScene * scene;
Player * player;
Score * score;
Health * health;
};
#endif // GAME_H
游戏.cpp
#include "Game.h"
#include "Health.h"
#include <QTimer>
#include <QGraphicsTextItem>
#include <QFont>
#include "Enemy.h"
#include <QMediaPlayer>
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)
//Set the Background
setBackgroundBrush(QBrush(QImage(":/images/background.jpg")));
// 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();
// change the rect from 0x0 (default) to 100x100 pixels
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);
// spawn enemies
QTimer * timer = new QTimer();
QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
timer->start(2000);
//Playing the Background Music
QMediaPlayer * music = new QMediaPlayer();
music->setMedia(QUrl("qrc:/sounds/bgmusic.mp3"));
music->play();
show();
}
主.cpp
#include <QApplication>
#include "Game.h"
#include <QGraphicsScene>
Game * game;
int main(int argc, char *argv[]){
QApplication a(argc, argv);
game = new Game();
game->show();
return a.exec();
}
一种直接的方法是创建一个子类 QGraphicsRectObject 的GameMenu
类,使用您当前用于创建其他游戏内对象(如Player
、Score
和Health
(的相同方法,除了GameMenu
对象将被绘制为矩形(或其他任何内容(并在其中绘制适当的菜单文本选项(作为子类 paint 方法的一部分(。 然后,您只需将GameMenu
对象放入QGraphicsScene的中心即可。 您还需要在代码中添加条件逻辑,以便在GameMenu
对象可见时游戏玩法的行为有所不同(即,在 GameMenu 可见时不要生成敌方坦克,并处理箭头键按下以更新 GameMenu 对象的选择选项,而不是移动玩家的坦克(。
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- Qt focusObjectChanged example?
- 如何在cpp文件之间切换窗口?在Qt中
- Qt和C++:将QLineEdit添加到QTabWidget中
- 从数据库实时显示QT c++中的数据
- Qt Quick-如何仅从c++代码与qml属性交互
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 如何在Qt中取消捕获字符串
- Qt SQLite没有查询或参数计数不匹配
- Qt SQlite无法创建表