QT 翻金币 小游戏
项目介绍
环境介绍:
使用 C++语言,基于 QT5.9.4 框架 + MinGW 32bit(位)编译器 开发的 【翻金币】小游戏
界面演示
规则说明
翻金币是一个简单的游戏,在翻金币场景中点击界面中的币可以进行翻转,翻转规则是币加
其上下左右的币同时进行翻转(动画做了一些小延迟),胜利的条件是界面中全是金币。
实现步骤分解
由界面演示,我们知道这个界面主要就 3 个界面:
翻金币的主界面
选择关卡界面
翻金币场景界面
界面详述
翻金币的主界面
说明
翻金币主界面有一个菜单栏,菜单栏有一个开始菜单,开始菜单里面只有一项退出,点击退
出,游戏结束。
主界面还有一个 START 按钮,点击 START 按钮,可以跳到翻金币的选择关卡界面。
其余则是背景和标题栏还有一个 Label
实现步骤
创建一个 MainScence 类,因为需要一个菜单栏,所以选择继承 QMainWindow
mainScence.h
#ifndef MAINSCENCE_H
#define MAINSCENCE_H
#include <QMainWindow>
#include"mypushbutton.h"
#include"chooeslevelscreen.h"
namespace Ui {
class MainScence;
}
class MainScence : public QMainWindow
{
Q_OBJECT
public:
explicit MainScence(QWidget *parent = 0);
~MainScence();
void paintEvent(QPaintEvent *);
ChooesLevelscreen *chooseScence;
private:
Ui::MainScence *ui;
};
#endif // MAINSCENCE_H
在.h 文件中重写了一个 paintEvent()函数,这个函数会被主动调用,无需手动调用,创建
一个 ChooesLevelscreen 对象,这个对象用来关联跳转。
mainScence.cpp
#include "mainscence.h"
#include "ui_mainscence.h"
#include<QPainter>
#include<QTimer>
#include<QSound>
MainScence::MainScence(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainScence)
{
ui->setupUi(this);
// 点击开始 - 退出游戏
connect(ui->actionQuit,&QAction::triggered,[=](){
this->close();
});
//设置固定大小
this->setFixedSize(320,588);
//设置标题
this->setWindowTitle("翻金币");
//设置图片
this->setWindowIcon(QIcon(":/res/Coin0001.png"));
//开始按钮的创建
MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
startBtn->setParent(this);
startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);
//准备开始按钮音效
QSound *startSound = new QSound(":/res/TapButtonSound.wav",this);
//设置循环次数
startSound->setLoops(-1);//如果是-1 代表无限循环
//创建出第二个界面 - 选择关卡
chooseScence = new ChooesLevelscreen;
//监听第二个选择关卡场景中的返回按钮的信号
connect(chooseScence,&ChooesLevelscreen::chooseScenceBack,[=](){
chooseScence->hide();
//将自身的位置同步为选择关卡场景的位置
this->setGeometry(chooseScence->geometry());
this->show();
});
connect(startBtn,&MyPushButton::clicked,[=](){
//qDebug()<<"点击了开始按钮";
//播放开始的音效
startSound->play();
//设置开始按钮的点击效果
startBtn->Jump1();
startBtn->Jump2();
//延时 进入到第二个界面
QTimer::singleShot(500,this,[=](){
//将自身隐藏调用
this->hide();
//进入到第二个界面 - 选择关卡
chooseScence->setGeometry(this->geometry());
chooseScence->show();
});
});
}
void MainScence::paintEvent(QPaintEvent *){
QPainter painter(this);
//画背景图片
QPixmap pix;
pix.load(":/res/PlayLevelSceneBg.png");
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//画标题图片
pix.load(":/res/Title.png");
//缩放原始图片
pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
painter.drawPixmap(10,30,pix);
}
MainScence::~MainScence()
{
delete ui;
}
选择关卡界面
说明
选择关卡界面也是有一个·菜单栏,其效果和实现和主界面一致,中心是一个选择关卡的功
能,目前是 20 个关卡。
还有一个 BACK 按钮,用来触发返回上一界面的功能
具体实现
chooesLevelscreen.h
#ifndef CHOOESLEVELSCREEN_H
#define CHOOESLEVELSCREEN_H
#include <QMainWindow>
#include"playscene.h"
class ChooesLevelscreen : public QMainWindow
{
Q_OBJECT
public:
explicit ChooesLevelscreen(QWidget *parent = nullptr);
void paintEvent(QPaintEvent *);
//维护游戏场景的指针
PlayScene *playScene;
signals:
//自定义的信号,只需要声明,不需要实现
void chooseScenceBack();
public slots:
};
#endif // CHOOESLEVELSCREEN_H
chooesLevelscreen.cpp
#include "chooeslevelscreen.h"
#include<QMenuBar>
#include<QMenu>
#include<QAction>
#include<QPainter>
#include<QTimer>
#include<QLabel>
#include<QSound>
#include"mypushbutton.h"
ChooesLevelscreen::ChooesLevelscreen(QWidget *parent) : QMainWindow(parent)
{
//设置固定大小
this->setFixedSize(320,588);
//设置标题
this->setWindowTitle("选择关卡");
//设置图片
this->setWindowIcon(QIcon(":/res/Coin0001.png"));
//创建菜单栏
QMenuBar *bar =menuBar();
this->setMenuBar(bar);
//创建菜单
QMenu *startMenu = bar->addMenu("开始");
//创建菜单项
QAction *quitAction = startMenu->addAction("退出");
//创建选择关卡的音效
QSound *chooesSound = new QSound(":/res/TapButtonSound.wav",this);
//返回按钮音效
QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);
//点击退出按钮 实现退出游戏
connect(quitAction,&QAction::triggered,[=](){
this->close();