Я совершенно новичок в QT и C ++. Я последовал онлайн-уроку и создал свою первую игру, используя QT. Игра в основном такая. Я танк, и я могу переместить танк влево и вправо в нижнем углу сцены. Бомбы падают случайным образом с верхней части сцены на землю. Я могу стрелять в них, нажав пробел. когда пуля попадает в бомбу, счет засчитывается ...
В моей игре только одна сцена. Когда я запускаю программу, игра запускается в то же время. Я хочу, чтобы моя программа сначала открывала Главное меню при запуске игры. Главное меню должно содержать две кнопки QPushButton. Они Начало игры & Выход . Я не знаю, как реализовать эту часть.
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
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();
}
основной. 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();
}