![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/91f046c8028d994648cc4f5c683a4fd3.gif)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDialog>
#include <QKeyEvent>
#include <QMediaPlayer>
#include <QTimer>
#include "director.h"
namespace Ui {
class MainWindow;
}
/*
* MainWindow - 游戏场景
* SLOT:
* @nextFrame(): 每个朋友会做什么
* @gameOver: 检查游戏是否结束
* @isPlaying: 检查游戏是否暂停
* @counter: 数一数朋友的数目
* @scene: 场景的所有组成部分
* @bgm: 游戏的背景音乐
*/
class MainWindow : public QDialog
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void next_frame();//每个fream更新屏幕
void pause_and_resume();//暂停并继续游戏
void mute_and_play();//静音和取消静音效果
void change_configuration();//应用配置更改
void update_ui();//更新配置设置框的ui
void save_configuration();//用输入的名称保存当前配置
void load_configuration(); //加载配置文件
protected:
void paintEvent(QPaintEvent *event);
void keyPressEvent(QKeyEvent *event);
private:
volatile bool gameOver = false;
volatile bool isPlaying = true;
volatile int counter = 0;
Ui::MainWindow *ui;
Scene* scene;//场景
QMediaPlayer bgm;//音效
QString exepath;//程序路径
QTimer* timer;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileInfo>
#include <QMediaPlaylist>
#include <QApplication>
#include <iostream>
//the path of the configuration file
MainWindow::MainWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::MainWindow)
{
//set up the UI
ui->setupUi(this);
exepath = QApplication::applicationDirPath();
qDebug()<<"exepath=="<<exepath;
this->resize(FRAME_WIDTH,FRAME_HEIGHT);
ui->EnterFileName->hide();
ui->MenuBox->hide();
ui->Configuration->hide();
//set up the soundeffect to play
QMediaPlaylist *playlist = new QMediaPlaylist();
playlist->addMedia(QUrl::fromLocalFile(QFileInfo("exepath/Audio/Lucid_Dreamer1.mp3").absoluteFilePath()));
playlist->setPlaybackMode(QMediaPlaylist::Loop);
bgm.setPlaylist(playlist);
bgm.setVolume(50);
bgm.play();
//创建一个计时器来更新每个fream
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(next_frame()));
timer->start(50);
//load from the configuration file
//从配置文件加载
load_configuration();//创建场景器
this->update();
}
MainWindow::~MainWindow()
{
gameOver = true;
delete ui;
delete scene;
}
/* SLOT */
//update the screen each fream每个fream更新屏幕
void MainWindow::next_frame()
{
update();
}
//pause and resume game暂停并继续游戏
void MainWindow::pause_and_resume()
{
isPlaying = !isPlaying;
mute_and_play();
}
//静音和取消静音效果
void MainWin