如何设置游戏的主菜单场景?QT C++; 我对QT和C++完全陌生。我遵循一个在线教程,用QT创建了我的第一个游戏。游戏基本上是这样的。我是一辆坦克,我可以在场景的下角左右移动坦克。炸弹从现场上部随机掉落到地面。我可以按空格键向他们射击。当子弹击中炸弹时,分数正在计算

如何设置游戏的主菜单场景?QT C++; 我对QT和C++完全陌生。我遵循一个在线教程,用QT创建了我的第一个游戏。游戏基本上是这样的。我是一辆坦克,我可以在场景的下角左右移动坦克。炸弹从现场上部随机掉落到地面。我可以按空格键向他们射击。当子弹击中炸弹时,分数正在计算,c++,qt,C++,Qt,我的游戏只有一个场景。当我运行这个程序时,游戏就开始了。我希望我的程序在运行游戏时首先打开一个主菜单。主菜单应包含两个按钮。他们是开始比赛和退出。我不知道如何实现这一部分 Game.h #ifndef GAME_H #define GAME_H #include <QGraphicsView> #include <QWidget> #include <QGraphicsScene> #include "Score.h" #include "Health.h"

我的游戏只有一个场景。当我运行这个程序时,游戏就开始了。我希望我的程序在运行游戏时首先打开一个主菜单。主菜单应包含两个按钮。他们是开始比赛退出。我不知道如何实现这一部分

Game.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

\ifndef游戏
#定义游戏
#包括
#包括
#包括
#包括“Score.h”
#包括“Health.h”
#包括“Player.h”
课堂游戏:公共QGraphicsView{
公众:
游戏(QWidget*parent=0);
qgraphicscene*场景;
玩家*玩家;
分数*分数;
健康*健康;
};
#endif//GAME_H
Game.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();
}

#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();
}

#包括“Game.h”
#包括“Health.h”
#包括
#包括
#包括
#包括“敌人h”
#包括
游戏::游戏(QWidget*父){
//创建场景
场景=新的qgraphicscene();
场景->设置场景竖立(0,0800600);//将场景设置为800x600而不是无穷大(默认设置)
//背景
setBackgroundBrush(QBrush(QImage(:/images/background.jpg));
//使新创建的场景成为可视化场景(因为游戏是QGraphicsView小部件,
//它可用于可视化场景)
场景(场景);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
设置固定大小(800600);
//创建播放器
player=新玩家();
//将矩形从0x0(默认)更改为100x100像素
播放器-> SETPOS(400500);//ToDo泛化以始终位于屏幕的中间底部。
//使播放器可聚焦,并将其设置为当前焦点
player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus();
//将播放器添加到场景中
场景->附加项(播放器);
//创建分数/健康状况
分数=新分数();
场景->附加项(分数);
健康=新健康();
运行状况->设置位置(运行状况->x(),运行状况->y()+25);
场景->附加项(健康);
//滋生敌人
QTimer*定时器=新的QTimer();
连接(计时器、信号(超时())、播放器、插槽(繁殖());
定时器->启动(2000);
//播放背景音乐
QMediaPlayer*音乐=新的QMediaPlayer();
音乐->设置媒体(qrl(“qrc:/sounds/bgmusic.mp3”);
音乐->播放();
show();
}
main.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();
}

#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();
}

#包括
#包括“Game.h”
#包括
游戏*游戏;
int main(int argc,char*argv[]){
质量保证申请a(argc、argv);
游戏=新游戏();
游戏->表演();
返回a.exec();
}

一种简单的方法是创建一个
游戏菜单
类,该类是子类,使用当前用于创建其他游戏内对象(如
玩家
得分
、健康)的相同方法,但
游戏菜单
对象将被绘制为矩形(或任何形状)并在其中绘制适当的菜单文本选项(作为子类方法的一部分)。然后,您只需将
GameMenu
对象放在QGraphicscene的中心。您还需要在代码中添加条件逻辑,以便在
GameMenu
对象可见时,游戏表现不同(即,当GameMenu可见时,不要生成敌人坦克,并按住手柄箭头键更新GameMenu对象的选定选项,而不是移动玩家的坦克).

非常感谢您的回答,先生!你能告诉我怎么做吗?我完全不知道怎么做。。。如果您能……我不会为您编写代码——我上面写的描述应该足以让您自己编写。我的建议是从复制程序中已经运行的一部分开始,例如,复制
健康
类,将其重命名为
游戏菜单
,然后开始逐步修改其功能,从显示健康表改为显示游戏菜单,继续进行重新编译和测试,以查看在修改代码时程序的行为如何变化。最初的初步步骤将给你信心,使你进一步修改,因为你去。