1 Star 0 Fork 1

AaTZY/qt_CoinFlip

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
chooselevelscene.cpp 3.26 KB
一键复制 编辑 原始数据 按行查看 历史
#include "chooselevelscene.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <mypushbutton.h>
#include <QDebug>
#include <QTimer>
#include <QLabel>
#include <QFont>
#include <QSound>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
//配置选择关卡场景
this->setFixedSize(320,588);
this->setWindowIcon(QPixmap(":/resource/Coin0001.png"));
this->setWindowTitle("选择关卡");
//创建菜单栏
QMenuBar *bar=menuBar();
setMenuBar(bar);
//创建开始菜单
QMenu *startMenu=bar->addMenu("开始");
//创建退出功能
QAction *quitAction=startMenu->addAction("退出");
//实现退出功能
connect(quitAction,&QAction::triggered,this,[=]()
{
this->close();
});
//返回按钮
MyPushButton *backBtn=new MyPushButton(":/resource/BackButton.png",":/resource/BackButtonSelected.png");
backBtn->setParent(this);
backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
//返回音效
QSound *backSound=new QSound(":/resource/BackButtonSound.wav",this);
//实现点击返回
connect(backBtn,&MyPushButton::clicked,[=]()
{
backSound->play();
QTimer::singleShot(300,this,[=]()
{
emit this->chooseSceneBack();
});
});
//选择音效
QSound *chooseSound=new QSound(":/resource/TapButtonSound.wav",this);
//创建关卡选择按钮
for(int i=0;i<20;i++)
{
MyPushButton *menuBtn=new MyPushButton(":/resource/LevelIcon.png");
menuBtn->setParent(this);
menuBtn->resize(80,80);
menuBtn->move(this->width()*0.5-2*menuBtn->width()+(i%4)*menuBtn->width(),180+i/4*menuBtn->height());
//通过QLabel设置数字
QLabel *menulabel=new QLabel;
QFont labelFont("黑体",18,QFont::Bold);
menulabel->setParent(this);
menulabel->setFont(labelFont);
menulabel->setFixedSize(menuBtn->width(),menuBtn->height());
menulabel->move(this->width()*0.5-2*menuBtn->width()+(i%4)*menuBtn->width(),180+i/4*menuBtn->height());
menulabel->setText(QString::number(i+1));
menulabel->setAlignment(Qt::AlignCenter);
//让鼠标穿透label
menulabel->setAttribute(Qt::WA_TransparentForMouseEvents);
//监听每个按钮的点击事件后进入游戏场景
connect(menuBtn,&MyPushButton::clicked,this,[=]()
{
chooseSound->play();
playScene=new PlayScene(i+1);
playScene->setGeometry(this->geometry());
this->hide();
playScene->show();
connect(playScene,&PlayScene::chooseSceneBack,this,[=]()
{
this->setGeometry(playScene->geometry());
playScene->hide();
this->show();
delete playScene;
playScene=NULL;
});
});
}
}
void ChooseLevelScene::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pix;
//加载标题
pix.load(":/resource/OtherSceneBg.png");
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//加载背景
pix.load(":/resource/Title.png");
painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Aatzy/qt_CoinFlip.git
git@gitee.com:Aatzy/qt_CoinFlip.git
Aatzy
qt_CoinFlip
qt_CoinFlip
master

搜索帮助