#ifndef CHOOSELEVELSCENE_H
#define CHOOSELEVELSCENE_H
#include <QMainWindow>
#include<playscene.h>
class ChooseLevelScene : public QMainWindow
{
Q_OBJECT
public:
explicit ChooseLevelScene(QWidget *parent = nullptr);
PlayScene* pScene = nullptr;
protected:
virtual void paintEvent(QPaintEvent *) override;
signals:
};
#endif // CHOOSELEVELSCENE_H
#include "chooselevelscene.h"
#include<QPixmap>
#include<QMenu>
#include<QMenuBar>
#include<QPainter>
#include<QString>
#include<QLabel>
#include"mypushbutton.h"
ChooseLevelScene::ChooseLevelScene(QWidget *parent)
: QMainWindow{parent}
{
//游戏场景基本设置
//设置固定窗口大小
setFixedSize(320,588);
//设置窗口图标
setWindowIcon(QPixmap(":/res/Coin0001.png"));
//设置窗口标题
setWindowIconText("请选择游戏关卡");
//菜单栏基本操作
//创建菜单栏
QMenuBar* bar = this->menuBar(); //指向QMainwindow自带的菜单栏
//把菜单栏设置到窗口
this->setMenuBar(bar);
//在菜单栏上面添加控件
QMenu* startBar = bar->addMenu("开始");
//加入退出菜单页
QAction* quitAction = startBar->addAction("退出");
//连接退出信号
connect(quitAction,&QAction::triggered,[=](){
this->close();
});
//设置返回按钮
MyPushButton *closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
closeBtn->setParent(this); //设置对象树析构
//设置按钮位置
closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
//创建选择关卡按钮
for(int i=0;i<20;++i){
MyPushButton *menuBtn = new MyPushButton(":/res/LevelIcon.png");
menuBtn->setParent(this);
menuBtn->move(25 + (i%4)*70 , 130+ (i/4)*70);
QLabel *label = new QLabel(this);
label->setFixedSize(menuBtn->width(),menuBtn->height());
label->setText(QString::number(i+1));
//设置居中对齐
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //水平-垂直居中
label->move(25 + (i%4)*70 , 130+ (i/4)*70);
//鼠标事件穿透,也就是鼠标事件会被按钮接受
label->setAttribute(Qt::WA_TransparentForMouseEvents,true);
//创建游戏场景
connect(menuBtn,&MyPushButton::clicked,[=](){
if(pScene ==nullptr){ //关卡未被选择
this->hide();
pScene = new PlayScene(i+1); //创建一个游戏界面对象
pScene->show();
//监听返回按钮
connect(pScene, &PlayScene::chooseSceneBack, this, [=]() {
if (pScene) { // 确保 pScene 不为空
this->show(); // 显示选择关卡界面
delete pScene; // 删除指针内存
pScene = nullptr; // 重新置空
}
});
}
});
}
}
//重写绘图事件来绘制背景图片
void ChooseLevelScene:: paintEvent(QPaintEvent *ev){
QPainter painter(this);
QPixmap pixmap;
//加载图片
pixmap.load(":/res/OtherSceneBg.png");
//填充图片
painter.drawPixmap(0,0,this->width(),this->height(),pixmap);
}
#ifndef MAINSCENE_H
#define MAINSCENE_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainScene;
}
QT_END_NAMESPACE
class MainScene : public QMainWindow
{
Q_OBJECT
public:
MainScene(QWidget *parent = nullptr);
~MainScene();
protected:
//重写绘图事件 绘制背景图片
virtual void paintEvent(QPaintEvent*) override;
private:
Ui::MainScene *ui;
};
#endif // MAINSCENE_H
#include "mainscene.h"
#include "ui_mainscene.h"
#include"mypushbutton.h"
#include<QPainter>
#include<QTimer>
#include"chooselevelscene.h"
MainScene::MainScene(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainScene)
{
ui->setupUi(this);
//设置主窗口
setFixedSize(320,588); //设置固定窗口大小
setWindowIcon(QPixmap(":/res/Coin0001.png")); //设置窗口图标
setWindowTitle("翻金币小游戏");
//设置退出信号
connect(ui->actionQuit,&QAction::triggered,this,[=](){
this->close(); //关闭主界面
});
//加入自定义的按钮
MyPushButton * startBtn = new MyPushButton(":/res/MenuSceneStartButton");
startBtn->setParent(this); //加入析构树
startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7); //调整按钮位置
//设置开始游戏按钮点击后变化事件
connect(startBtn,&MyPushButton::clicked,[=](){
//调用类内变化函数
startBtn->zoom1();
startBtn->zoom2();
//延时0.1s后进入游戏界面
ChooseLevelScene* chooseScene = new ChooseLevelScene();
QTimer::singleShot(100,this,[=](){
this->hide();
chooseScene->show();
});
});
}
//重写绘图事件的实现
void MainScene::paintEvent(QPaintEvent*ev){
//创建画家
QPainter painter(this);
//创建Pixmap对象
QPixmap pix;
//加载图片
pix.load(":/res/PlayLevelSceneBg.png");
//绘制背景图片
painter.drawPixmap(0,0,this->width(),this->height(),pix); //填充整个窗口
//加载标题
pix.load(":/res/Title.png");
pix.scaled(pix.width()*0.5,pix.height()*0.5);
painter.drawPixmap(20,50,pix.width(),pix.height(),pix);
}
MainScene::~MainScene()
{
delete ui;
}
#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include <QWidget>
#include<QPushButton>
class MyPushButton : public QPushButton
{
Q_OBJECT
public:
explicit MyPushButton(QWidget *parent = nullptr);
MyPushButton(QString normalImg,QString pressImg = ""); //构造函数,传入按钮图片的路径
QString normalImg;
QString pressImg;
//自定义特效函数
void zoom1();
void zoom2();
signals:
};
#endif // MYPUSHBUTTON_H
#include "mypushbutton.h"
#include<QString>
#include<QPropertyAnimation>
MyPushButton::MyPushButton(QWidget *parent)
: QPushButton{parent}
{ }
MyPushButton::MyPushButton(QString normalImgPath,QString pressImgPath){
//成员变量保存路径
this->normalImg = normalImgPath;
this->pressImg = pressImgPath ;
//创建Pixmap对象加载图片
QPixmap pixmap;
bool ok = pixmap.load(normalImg); //返回一个bool类型,表示加载是否成功
if(!ok){
qDebug()<<"初始图片加载失败!";
}
//把按钮固定设置为图片大小
this->setFixedSize(pixmap.width(),pixmap.height());
//设置不规则图片样式
this->setStyleSheet("QPushButton{border:0px;}");
//设置按钮图表为图片
this->setIcon(pixmap);
//设置按钮图标大小
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
}
//实现点击按钮特效函数
//弹下去
void MyPushButton::zoom1(){
//创建特效对象
QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");
animation->setDuration(200); //设置特效持续时间
//设置起始位置
animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
//设置结束位置
animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height())); //向下滑动
//设置缓和曲线 QEasingCurve::OutBounce为跳动效果
animation->setEasingCurve(QEasingCurve::OutBounce);
//启动动画
animation->start();
//结束动画后回收内存
connect(animation, &QPropertyAnimation::finished, [=](){
animation->deleteLater();
});
}
//弹回去
void MyPushButton::zoom2(){
//创建特效对象
QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");
animation->setDuration(200); //设置特效持续时间
//设置起始位置
animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
//设置结束位置
animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height())); //向下滑动
//设置缓和曲线 QEasingCurve::OutBounce为跳动效果
animation->setEasingCurve(QEasingCurve::OutBounce);
//启动动画
animation->start();
//结束动画后回收内存
connect(animation, &QPropertyAnimation::finished, [=](){
animation->deleteLater();
});
}
#ifndef PLAYSCENE_H
#define PLAYSCENE_H
#include <QWidget>
#include<QMainWindow>
class PlayScene : public QMainWindow
{
Q_OBJECT
public:
int levelIndex;
explicit PlayScene(QWidget *parent = nullptr);
PlayScene(int idx);
protected:
//重写绘图事件
virtual void paintEvent(QPaintEvent*)override;
signals:
void chooseSceneBack();
};
#endif // PLAYSCENE_H
#include "playscene.h"
#include<QMenuBar>
#include<QMenu>
#include<QPainter>
#include<QPixmap>
#include"mypushbutton.h"
#include<QTimer>
#include<QLabel>
#include<QFont>
PlayScene::PlayScene(QWidget *parent)
: QMainWindow{parent}
{
}
PlayScene::PlayScene(int idx){
//创建游戏场景
this->levelIndex = idx;
setFixedSize(320,588);
setWindowIcon(QPixmap(":/res/Coin0001.png"));
setWindowTitle("第"+QString::number(levelIndex)+"关");
//创建菜单栏
QMenuBar *bar = this->menuBar();
setMenuBar(bar);
QMenu *startMenu = bar->addMenu("开始");
QAction *quitAction = startMenu->addAction("退出");
//退出信号
connect(quitAction,&QAction::triggered,this,[=](){
this->close();
});
//返回按钮
MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
closeBtn->setParent(this);
closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
connect(closeBtn,&MyPushButton::clicked,[=](){
QTimer::singleShot(200,this,[=](){ //延时0.2s后执行
this->hide();
emit this->chooseSceneBack();
});
});
//加载当前关卡文字
QLabel *label = new QLabel(this);
//创建字体
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
//设置标签字体
label->setFont(font);
label->setText(QString("Level:%1").arg(QString::number(this->levelIndex)));
//设置标签位置
label->setGeometry(10,this->height()-50,120,50);
//设置翻金币背景图片
for(int i=0;i<4;++i){
for(int j=0;j<4;++j){
QLabel *label = new QLabel(this);
label->setGeometry(0,0,50,50);
label->setPixmap(QPixmap(":/res/BoardNode.png"));
label->move(57+i*50,200+j*50);
}
}
}
//重写绘图事件
void PlayScene::paintEvent(QPaintEvent*event){
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.scaled(pix.width()*0.5,pix.height()*0.5);
painter.drawPixmap(20,50,pix.width(),pix.height(),pix);
}