Qt金币游戏

firstgame.pro

CONFIG += c++11
QT += multimedia

dataconfig.h

guanqia.h

#ifndef GUANQIA_H
#define GUANQIA_H

#include 
#include "play.h"

class guanqia : public QMainWindow
{
    Q_OBJECT
public:
    explicit guanqia(QWidget *parent = nullptr);
//重写绘图事件
    void paintEvent(QPaintEvent *);
    play *youxi = NULL;

signals:
    void choosefanhui();

};

#endif // GUANQIA_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include "guanqia.h"


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    //重写paintevent
    void paintEvent(QPaintEvent *);
    guanqia *xuanze = NULL;



private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mycoin.h

#ifndef MYCOIN_H
#define MYCOIN_H

#include 
#include 
#include 
#include 

class mycoin: public  QPushButton
{
public:
    mycoin(QString morencoinpath);
    void change();
    QTimer *time1;
    QTimer *time2;
    int min = 1;
    int max = 8;

    bool ing = 0;

    int posx;
    int poxy;
    bool zhengfan;

    int vic = 0  ;

    //监听按下和释放
    void QMousePressEvent(QMouseEvent *e);

};

#endif // MYCOIN_H

mypushbutton.h

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H

#include 

class myPushButton : public QPushButton
{
    Q_OBJECT
public:
    explicit myPushButton(QWidget *parent = nullptr);
    //状态默认为空 ""
    myPushButton(QString zhengchang , QString zhuangtai = "");

    QString zhengchanglujing;
    QString zhuangtailujing;

    //设置按钮弹跳
    void uptiao();
    void downtiao();
    //重写按钮按下mouse::pressevent和释放
    void mousePressEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
signals:

};

#endif // MYPUSHBUTTON_H

play.h

#ifndef PLAY_H
#define PLAY_H

#include 
#include "mycoin.h"

class play : public QMainWindow
{
    Q_OBJECT
public:
    //默认构造函数注释
    //explicit play(QWidget *parent = nullptr);
    play(int level);
    int jilulevel;
    void paintEvent(QPaintEvent *event);
    int array[4][4];
    mycoin *btncoin[4][4] ;
    bool victory;
signals:
    void fanhuixuanze();//两个类发送信号名称可以相同
};

#endif // PLAY_H

**

.cpp

**
dataconfig.cpp

guanqia.cpp

#include "guanqia.h"
#include "mainwindow.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include "mypushbutton.h"
#include "play.h"
#include "QTimer"



guanqia::guanqia(QWidget *parent) : QMainWindow(parent)
{
    setFixedSize(320,588);
    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    //设置主标题
    setWindowTitle("金币翻翻乐");
    //创建菜单栏
    QMenuBar *bar = menuBar();
    setMenuBar(bar);
    QMenu *caidanmenu= bar->addMenu("菜单");

    QAction * startmenu= caidanmenu->addAction("开始");
    QAction * quitmenu= caidanmenu->addAction("退出");

    connect(quitmenu,&QAction::triggered,[=](){
        this->close();
    });
    //返回按钮
    myPushButton *backanniu = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    //设置到当前窗口
    backanniu->setParent(this);
    backanniu->move(this->width()-backanniu->width(),this->height()-backanniu->height());
    connect(backanniu,&myPushButton::clicked,[=](){
//        this->hide();
//        QMainWindow *zhucaidan;
//        zhucaidan = new QMainWindow;
//        zhucaidan->show();
        //主场景监听返回
        emit this->choosefanhui();
    });
    //创建选择关卡的按钮【用一个for写一个矩阵】
    for(int i = 0 ; i < 20 ; i++){
        myPushButton * anniu_20 = new myPushButton(":/res/LevelIcon.png");
        anniu_20->setParent(this);
        anniu_20->move(25+ i%4 * 70,130 + i/4 * 70);
        //自定义的按钮用下方效果不佳
        //anniu_20->setText(QString::number(i+1));
        //要用QLabel
        connect(anniu_20,&myPushButton::clicked,[=](){
            QString str = QString("选择的是第%1关").arg(i+1);
            qDebug()<<str;
            //进入游戏
            this->hide();
            youxi = new play(i+1);
            youxi->show();
            //监听信号
            connect(youxi,&play::fanhuixuanze,this ,[=](){
//                youxi->hide();
                delete youxi;
                youxi = NULL;
                this->show();

            });

        });
        QLabel *label = new QLabel;
        label->setParent(this);
        label->setFixedSize(anniu_20->width(),anniu_20->height());
        label->setText(QString::number(i+1));
        label->move(45+ i%4 * 70,130 + i/4 * 70);
        //设置label文字对齐方式也可以
        //label->setAlignment(Qt::AlignHCenter );

        //鼠标穿透LABEL 55号51号 本节51
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

    }


}
void guanqia::paintEvent(QPaintEvent *){
    QPainter huajia(this);
    QPixmap beijing;
    beijing.load(":/res/OtherSceneBg.png");
    huajia.drawPixmap(0,0,320,588,beijing);
    beijing.load(":/res/Title.png");
    huajia.drawPixmap(0,0,beijing);
}

main.cpp

#include "mainwindow.h"
#include "dataconfig.h"
#include 
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
//    dataConfig config;
//    for(int i = 0 ; i < 4 ; i++){
//        for(int j = 0 ; j < 4 ; j++){
//            qDebug()<
//        }
//     qDebug()<
//    }
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
#include "mypushbutton.h"

//发布从debug改成release再点击锤子【编译】,之后运行一下
//之后右键mainwin.cpp 再exp中打开
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    //设置音效
    QSound *sing =new QSound(":/res/TapButtonSound.wav",this);
    sing->setLoops(1);//循环几次,-1无限循环

    ui->setupUi(this);
    //main
    //设置固定大小
    setFixedSize(320,588);
    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    //设置主标题
    setWindowTitle("金币翻翻乐");
    //实现工具栏退出按钮的作用
    connect(ui->actionquit,&QAction::triggered,[=](){
        this->close();
    });
    //绘制背景图片用label也可以但是一般是绘图事件用画家画画
    //开始按钮
    myPushButton * startanniu = new myPushButton(":/res/MenuSceneStartButton.png");
    startanniu->setParent(this);
    startanniu->move(this->width()*0.5-startanniu->width()*0.5,this->height()*0.6);

    //实例化选择关卡场景并且监听
    xuanze = new guanqia;
    //监听返回信号
    connect(xuanze,&guanqia::choosefanhui,this ,[=](){
        this->setGeometry(xuanze->geometry());
        QTimer::singleShot(500,this,[=](){
        xuanze->hide();
        this->show();
        sing->stop();
        });
    });

    connect(startanniu,&myPushButton::clicked,[=](){
        sing->play();
        qDebug()<<"点击开始按钮成功"<<endl;
        //设置弹起特效【注意顺序】
        startanniu->uptiao();
        startanniu->downtiao();
        //延时进入选择关卡界面
        QTimer::singleShot(500,this,[=](){
            //设置场景位置
            xuanze->setGeometry(this->geometry());


            this->hide();//隐藏自身窗口
            xuanze->show();

        });
    });

}

//重写绘图事件设置背景
void MainWindow:: paintEvent(QPaintEvent *){
    QPainter huajia(this);
    QPixmap beijing;
    beijing.load(":/res/PlayLevelSceneBg.png");
    //把背景图画上去【拉伸屏幕】
    huajia.drawPixmap(0,0,320,588,beijing);
    //画背景上图标【缩放图标】
    beijing.load(":/res/Title.png");
    beijing.scaled(beijing.width()*0.7,beijing.height()*0.6);
    huajia.drawPixmap(0,0,beijing);
    //设置圆形按钮/不规则按钮【start】
    //自定义封装按钮
    //创建 CPP Class文件



}

MainWindow::~MainWindow()
{
    delete ui;
}


mycoin.cpp

#include "mycoin.h"
#include 
#include 

mycoin::mycoin(QString morencoinpath)
{
    QPixmap pix;
    bool ret = pix.load(morencoinpath);//加载图片路径
    if(!ret){
        qDebug()<<"加载失败"<<endl;
        return;
    }
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    //初始化定时器
    time1 = new QTimer(this);
    time2 = new QTimer(this);
    //监听要写再构造函数中
    connect(time1,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str = QString(":/res/Coin000%1.png").arg(this->min++);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

        //panduan 8/1
        if(this->min > 8){
            this->min = 1;
            ing = 0;
            time1->stop();
        }
    });

    connect(time2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str = QString(":/res/Coin000%1.png").arg(this->max--);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

        //panduan 8/1
        if(this->max < 1){
            this->max = 8;
            ing = 0;
            time2->stop();
        }
    });
}

void mycoin::QMousePressEvent(QMouseEvent *e){
    if(this->ing || this->vic == 16){
        qDebug()<<"11111";
        qDebug()<<QString::number(vic);
        this->vic = 0;
        return;//不能点击
    }

    else
        QPushButton::mousePressEvent(e);
}

void mycoin::change(){
    //如果是正面就反转反面
    if(this->zhengfan){
        time1->start(30);
        ing = 1;
        this->zhengfan = 0;
    }

    else{
        time2->start(30);
        ing = 1;
        this->zhengfan = 1;

    }
}

mypushbutton.cpp

#include "mypushbutton.h"
#include 
//动画
#include 

//myPushButton::myPushButton(QWidget *parent) : QWidget(parent)
//{

//}
//构造函数 默认值不能两个同时写
myPushButton:: myPushButton(QString zhengchang , QString zhuangtai){
    this->zhengchanglujing = zhengchang;
    this->zhuangtailujing = zhuangtai;
    QPixmap anniu;
    bool orr = anniu.load(zhengchang);
    if(!orr){
        qDebug()<<"图片加载失败"<<endl;
        return ;
    }
    //加载成功
    //设置图片固定大小
    this->setFixedSize(anniu.width(),anniu.height());
    //设置不规则图片样式
    //设置边框0像素
    this->setStyleSheet("QPushButton{border:0px;}");
    //设置图标
    this->setIcon(anniu);
    //设置大小
    this->setIconSize(QSize(anniu.width(),anniu.height()));
}
void myPushButton:: uptiao(){
    //创建动态对象
    QPropertyAnimation *donghua = new QPropertyAnimation(this,"geometry");
    //设置时间间隔
    donghua->setDuration(200);
    //起始位置 x,y,宽,高
    donghua->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //结束位置
    donghua->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置弹跳曲线
    donghua->setEasingCurve(QEasingCurve::OutBounce);
    //执行动画
    donghua->start();
}
void myPushButton:: downtiao(){
    //创建动态对象
    QPropertyAnimation *donghua = new QPropertyAnimation(this,"geometry");
    //设置时间间隔
    donghua->setDuration(200);
    //起始位置 x,y,宽,高
    donghua->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //结束位置
    donghua->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置弹跳曲线
    donghua->setEasingCurve(QEasingCurve::OutBounce);
    //执行动画
    donghua->start();
}
void myPushButton:: mousePressEvent(QMouseEvent *e){
    if(this->zhuangtailujing != "")//按下不为空,正在按下
    {
        QPixmap anniu;
        bool orr = anniu.load(this->zhuangtailujing);
        if(!orr){
            qDebug()<<"图片加载失败"<<endl;
            return ;
        }
        //加载成功
        //设置图片固定大小
        this->setFixedSize(anniu.width(),anniu.height());
        //设置不规则图片样式
        //设置边框0像素
        this->setStyleSheet("QPushButton{border:0px;}");
        //设置图标
        this->setIcon(anniu);
        //设置大小
        this->setIconSize(QSize(anniu.width(),anniu.height()));
    }
    //让父类执行其他代码
    return QPushButton::mousePressEvent(e);
}
void myPushButton:: mouseReleaseEvent(QMouseEvent *e){
    if(this->zhengchanglujing != "")
    {
        QPixmap anniu;
        bool orr = anniu.load(this->zhengchanglujing);
        if(!orr){
            qDebug()<<"图片加载失败"<<endl;
            return ;
        }
        //加载成功
        //设置图片固定大小
        this->setFixedSize(anniu.width(),anniu.height());
        //设置不规则图片样式
        //设置边框0像素
        this->setStyleSheet("QPushButton{border:0px;}");
        //设置图标
        this->setIcon(anniu);
        //设置大小
        this->setIconSize(QSize(anniu.width(),anniu.height()));
    }
    //让父类执行其他代码
    return QPushButton::mouseReleaseEvent(e);
}

play.cpp

#include "play.h"
#include "QDebug"
#include "guanqia.h"
#include "mainwindow.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include "mypushbutton.h"
#include "mycoin.h"
#include "dataconfig.h"
#include //动画
#include 
#include 
//play::play(QWidget *parent) : QMainWindow(parent)
//{
//20220812
//}
play::play(int level){
    this->jilulevel = level;
    qDebug()<<"第"<<level<<"关";
    setFixedSize(320,588);
    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    //设置主标题
    setWindowTitle("金币翻翻乐");
    //创建菜单栏
    QMenuBar *bar = menuBar();
    setMenuBar(bar);
    QMenu *caidanmenu= bar->addMenu("菜单");

    QAction * startmenu= caidanmenu->addAction("开始");
    QAction * quitmenu= caidanmenu->addAction("退出");

    connect(quitmenu,&QAction::triggered,[=](){
        this->close();
    });
    //返回按钮
    myPushButton *backanniu = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    //设置到当前窗口
    backanniu->setParent(this);
    backanniu->move(this->width()-backanniu->width(),this->height()-backanniu->height());
    connect(backanniu,&myPushButton::clicked,[=](){
//        this->hide();
//        QMainWindow *zhucaidan;
//        zhucaidan = new QMainWindow;
//        zhucaidan->show();
        //主场景监听返回
        qDebug()<<"游戏中点击返回";
        emit this->fanhuixuanze();
    });
    //显示了level
    QLabel *label =new QLabel;
    label->setParent(this);
    label->setText(QString::number(level));
    //设置字体
    QFont font;
    font.setFamily("华文楷体");
    font.setPointSize(20);
    QString str = QString("level:%1").arg(this->jilulevel);
    //将字体设置到标签控件中
    label->setFont(font);
    label->setText(str);
//    label->move(100,500);
//    label->setFixedSize(100,100);
    //一步完成
    label->setGeometry(30,this->height()-80,120,60);

    //初始化每个二维数组关卡
    dataConfig config;
    for(int i = 0 ; i < 4 ; i++)
        for(int j = 0 ; j < 4 ; j++){
            this->array[i][j]=config.mData[this->jilulevel][i][j];
        }
    //预先写入胜利图片
    QLabel *label_2 = new QLabel;
    QPixmap pix_1;
    pix_1.load(":/res/LevelCompletedDialogBg.png");
    label_2->setGeometry(0,0,pix_1.width(),pix_1.height());
    label_2->setPixmap(pix_1);
    label_2->setParent(this);
    label_2->move((this->width() - pix_1.width())*0.5,-pix_1.height());


    //显示金币背景图案
    for(int i = 0 ; i < 4 ; i++)
        for(int j = 0 ; j < 4 ; j++){
            QPixmap pix=QPixmap(":/res/BoardNode(1).png");
            QLabel *label_1 = new QLabel;
            label_1->setGeometry(0,0,pix.width(),pix.height());
            label_1->setPixmap(pix);
            label_1->setParent(this);
            label_1->move(57+i*50,200+j*50);
       //创建金币
            QString str1;
            if(array[i][j]==1)
                str1=":/res/Coin0001.png";
            else
                str1= ":/res/Coin0008.png";
            mycoin *coin=new mycoin(str1);
            coin->setParent(this);
            coin->move(59+i*50,204+j*50);
            //给金币的xy坐标以及状态赋值
            coin->posx=i;
            coin->poxy=j;
            coin->zhengfan=this->array[i][j];

            //将金币放入维护的二维数组中
            btncoin[i][j]=coin;

            //点击金币就翻转监听
            if(this->victory==0)
            connect(coin,&mycoin::clicked,[=](){
                QSound *singing =new QSound(":/res/ConFlipSound.wav",this);
                singing->play();

                coin->change();
                this->array[i][j] = this->array[i][j]==0?1:0;

                QTimer::singleShot(300,this,[=](){
                    //翻转周围的
                    //不用else if
                    if(coin->posx + 1 <= 3)
                    {
                        //翻转右侧
                        btncoin[coin->posx+1][coin->poxy]->change();
                        this->array[coin->posx+1][coin->poxy] = this->array[coin->posx+1][coin->poxy]== 0 ? 1 : 0 ;

                    }
                    if(coin->posx - 1 >= 0)
                    {
                        //翻转左侧
                        btncoin[coin->posx-1][coin->poxy]->change();
                        this->array[coin->posx-1][coin->poxy] =  this->array[coin->posx-1][coin->poxy]== 0 ? 1 : 0 ;

                    }
                    if(coin->poxy + 1 <= 3)
                    {
                        //翻转下侧
                        btncoin[coin->posx][coin->poxy+1]->change();
                        this->array[coin->posx][coin->poxy+1] = this->array[coin->posx][coin->poxy+1]== 0 ? 1 : 0 ;

                    }
                    if(coin->poxy - 1 >= 0)
                    {
                        //翻转上侧
                        btncoin[coin->posx][coin->poxy-1]->change();
                        this->array[coin->posx][coin->poxy-1] = this->array[coin->posx][coin->poxy-1]== 0 ? 1 : 0 ;

                    }

                    this->victory = 1;
                    for(int i = 0 ; i < 4 ; i++){
                        for(int j = 0 ; j < 4 ;j++)
                        {
                         if(btncoin[i][j]->zhengfan == 0){
                             this->victory = 0;
                            break;
                            }
                        }
                    }

                    if(this->victory == 1){
                        qDebug()<<"游戏胜利";
                        //将所有胜利改为1
                        for(int i = 0 ; i < 4 ; i++){
                            for(int j = 0 ; j < 4 ;j++)
                            {
                                coin->vic++;
                                //btncoin[i][j]->victory = 1;
                                btncoin[i][j]->disconnect();
                            }
                        }
                    //胜利图片显示
                        QSound *sing =new QSound(":/res/LevelWinSound.wav",this);
                        sing->play();

                        QPropertyAnimation *donghua = new QPropertyAnimation(label_2,"geometry");//矩形框
                        donghua->setDuration(1000);
                        //开始位置
                        donghua->setStartValue(QRect(label_2->x(),label_2->y(),label_2->width(),label_2->height()));
                        //结束位置
                        donghua->setEndValue(QRect(label_2->x(),label_2->y()+114,label_2->width(),label_2->height()));
                        //曲线
                        donghua->setEasingCurve(QEasingCurve::OutBounce);
                        donghua->start();



                        }
                });


            });

        }
}
void play::paintEvent(QPaintEvent *){
    QPainter huajia(this);
    QPixmap beijing;
    beijing.load(":/res/OtherSceneBg.png");
    huajia.drawPixmap(0,0,320,588,beijing);
    beijing.load(":/res/Title.png");
    huajia.drawPixmap(0,0,beijing);
}

你可能感兴趣的:(QT,qt,游戏,ui)