如何用QT做消消乐游戏(简单版羊了个羊)

        基于Qt的widget界面开发的图形消除单机游戏,在规定区域 内从 6 张基础图片中随机选中生成一组若干张 图片(有序堆叠),从表面选中图片到等待区,3 张相同即可消除,若等待区装满 7 张图片即输掉 游戏,全部消除即可通关并积分累加。 使用技术: 使用了 QT 图形界面设计工具; 重写 QT 事件处理函数等。

        图形和背景音乐自选。 

QT       += core gui
QT       += multimedia

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mylabel.cpp \
    widget.cpp

HEADERS += \
    mylabel.h \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    music.qrc \
    rs.qrc \
    rsback.qrc

 以下是mylabel.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "mylabel.h"
#include <QMediaPlayer>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void mouseReleaseEvent(QMouseEvent *event);   //松开鼠标

public slots:
    void pButSlotFun();
    void pBut_2SlotFun();
    void positionChanged(qint64);

private:
    Ui::Widget *ui;
    myLabel  *pMyLabel[50];        //*pMyLabel, *pMyLabel_2, *pMyLabel_3, *pMyLabel_4, *pMyLabel_5, *pMyLabel_6;
    int win;
    int integralVal;
    QMediaPlayer *player;
    QUrl playerQurl;
};
#endif // WIDGET_H

 以下是widget.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QMouseEvent>
#include <QLabel>

class myLabel :public QLabel
{
public:
    myLabel(QWidget *parent);
    ~myLabel();


    //重写 父类的 事件处理函数
    void mousePressEvent(QMouseEvent *event);  //按压鼠标
    //void mouseReleaseEvent(QMouseEvent *event);   //松开鼠标
    //void mouseDoubleClickEvent(QMouseEvent *event);    //双击
    void mouseMoveEvent(QMouseEvent *event);    //鼠标移动

    //void wheelEvent(QWheelEvent *event);        //滚轮滑动

    //void keyPressEvent(QKeyEvent *event);       //键盘按下
    //void keyReleaseEvent(QKeyEvent *event);   //键盘松开

    //void enterEvent(QEvent *event);         //鼠标进入
   // void leaveEvent(QEvent *event);         //鼠标离开

    int mouseX,mouseY,can,val,blankval;//鼠标在窗口的坐标X、Y;入框锁定;显示出来的图片对应的编号;入框图片的编号
    static QString pictureName[100];//装图片地址--字符串
    static int pictureVal[100];//使用了的图片地址编号
    static int pictureNum[6];//每种图片计数
    static int picture[100];//同一图片入框数量
    static int blank[8];//框内装图标的图片地址
    static int vali,blanki;//显示出来的图片数量,实际装入框的数量
};

#endif // MYLABEL_H

 以下是mylabel.cpp

#include "mylabel.h"
#include <QDebug>
#include <QMouseEvent>
#include <QMessageBox>


myLabel::myLabel(QWidget *parent)
{
    can=0;
    mouseX=0;
    mouseY=0;
    blankval=0;

    pictureName[0]=":/rs/Calculator 512x512.png";
    pictureName[1]=":/rs/Calendar 512x512.png";
    pictureName[2]=":/rs/Camera 512x512.png";
    pictureName[3]=":/rs/Clock 512x512.png";
    pictureName[4]=":/rs/YouTube 512x512.png";
    pictureName[5]=":/rs/iPod 512x512.png";

    if(vali<40){
        pictureVal[vali]=qrand()%6;
        pictureNum[pictureVal[vali]]++;
        val=vali;
        QPixmap pix (pictureName[pictureVal[vali]]);
        //pMyLabel = new myLabel;
        this->setParent(parent);
        this->setGeometry(1900,1900,100,100);
        this->setPixmap(pix);
        this->setScaledContents(true);
        vali++;
    }else{
        for(int i=0;i<6;i++){
            if( (pictureNum[i]%3) != 0){
                pictureVal[vali]=i;
                pictureNum[i]++;
                val=vali;
                QPixmap pix (pictureName[pictureVal[vali]]);
                this->setParent(parent);
                this->setGeometry(1900,1900,100,100);
                this->setPixmap(pix);
                this->setScaledContents(true);
                vali++;
                return;
            }

        }

    }
}

myLabel::~myLabel()
{

}

void myLabel::mousePressEvent(QMouseEvent *event)//点击鼠标
{
    //event->y();
    if(can==0 && blank[blanki]<=765){
        blankval=blanki;
        this->setGeometry(blank[blankval],515,100,100);

        blanki++;
        blank[blanki]=blank[blanki-1]+120;
        can=1;

        picture[pictureVal[val]]++;
    }

}

/*void myLabel::mouseReleaseEvent(QMouseEvent *event)
{
    can=0;
    return;
}*/

void myLabel::mouseMoveEvent(QMouseEvent *event)//移动鼠标
{
    mouseX+=event->x()-50;
    mouseY+=event->y()-50;
   // qDebug()<<mouseX<<" "<<mouseY<<endl;
    //this->setGeometry(mouseX,mouseY,100,100);//点击拖动,对象跟随鼠标移动

   // if(mouseX>30 && mouseX<830 && mouseY>500 && mouseY<620){//对象陷入某个区域
            //this->setGeometry(500,550,100,100)
   // }

}


 以下是widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMessageBox>
#include <QPalette>

#include <QMouseEvent>

int myLabel::blanki=0;
int myLabel::vali=0;
int myLabel::pictureVal[100]={0};
int myLabel::pictureNum[6]={0};
int myLabel::picture[100]={0};
int myLabel::blank[8]={0};
QString myLabel::pictureName[100];

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    myLabel::blank[0]=45;//框位置
    win=0;
    integralVal=0;

    QPalette palette;
    palette.setBrush(QPalette::Background,QBrush(QPixmap(":/rs/background.png")));
    this->setPalette(palette);


    ui->textBrowser->setGeometry(2000,2000,960,470);
    ui->groupBox->setGeometry(2000,2000,900,130);

    ui->label_2->setText("游戏介绍:\n   将3张相同的图形选入框中即可消除,全部消除则获胜。");
    ui->label_2->setWordWrap(1);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(pButSlotFun()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(pBut_2SlotFun()));
    ui->groupBox_2->setGeometry(190,110,580,330);


    for(int i=0;i<48;i++){
        pMyLabel[i]= new myLabel (this);
        qDebug()<<myLabel::vali<<endl;
    }

    player = new QMediaPlayer(this);
    player->setMedia(QUrl("qrc:/rs_music/8bit remi.mp3"));
    player->setVolume(100);
    player->play();
    connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
}

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



void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    //event->y();
    int blankJ=0,blanknum=0;
    for(int i=0;i<myLabel::vali;i++){
        if(myLabel::picture[myLabel::pictureVal[i]]>=3 &&pMyLabel[i]->can==1){   //val  can

            pMyLabel[i]->setGeometry(1000,1000,100,100);
            pMyLabel[i]->can=2;

            myLabel::blanki--;
            blanknum=1;
            qDebug()<<myLabel::picture[myLabel::pictureVal[i]]<<endl;
            myLabel::picture[myLabel::pictureVal[i]]+=10;

            if(myLabel::picture[myLabel::pictureVal[i]]>=33){myLabel::picture[myLabel::pictureVal[i]]-=33;}
            win++;//消除一次加1分

        }
    }

    for(int j=0;j<myLabel::vali;j++){
        if(pMyLabel[j]->can==1 && blanknum==1){
           pMyLabel[j]->setGeometry(myLabel::blank[blankJ],515,100,100);
           blankJ++;
        }
    }
    if(myLabel::blanki==7 && blanknum==0){
        QMessageBox::about(this,"游戏结束"," 很遗憾,你输掉了。。。\t\t \n \n");
        ui->textBrowser->setGeometry(2000,2000,960,470);
        ui->groupBox->setGeometry(2000,2000,900,130);
        ui->groupBox_2->setGeometry(190,110,580,330);
        //player->pause();//暂停音乐
        for(int i=0;i<myLabel::vali;i++){
            pMyLabel[i]->setGeometry(1000,1000,100,100);
        }
        for(int i=0;i<6;i++){
            myLabel::picture[i]=0;
        }
        myLabel::blanki=0;
        if(integralVal>20){
            integralVal-=20;
            return;
        }//消除失败减5分
        integralVal=0;
        win=0;
    }

    if(myLabel::blanki==0 && blanknum==1 &&  myLabel::vali==win){
        QMessageBox::about(this,"游戏结束"," 恭喜你,你赢了。。。      \t\t \n \n ");
        ui->textBrowser->setGeometry(2000,2000,960,470);
        ui->groupBox->setGeometry(2000,2000,900,130);
        ui->groupBox_2->setGeometry(190,110,580,330);
        //player->pause();//暂停音乐
        integralVal+=win;
        win=0;
    }
}

void Widget::pButSlotFun()
{
    ui->groupBox_2->setGeometry(2000,2000,580,330);
    ui->textBrowser->setGeometry(20,20,960,470);
    ui->groupBox->setGeometry(30,499,900,130);

    int seat=20;
    if(integralVal<48){
        for(int i=0;i<myLabel::vali;i++){   //第一关
            pMyLabel[i]->can=0;
            if(i<10){
                pMyLabel[i]->setGeometry(60+seat*i,50,100,100);
            }else if(i>=10 && i<20){
                pMyLabel[i]->setGeometry(870-seat*(i-9),50,100,100);
            }else if(i>=20 && i<30){
                pMyLabel[i]->setGeometry(250,140+seat*(i-19),100,100);
            }else if(i>=30 && i<40){
                pMyLabel[i]->setGeometry(650,140+seat*(i-29),100,100);
            }else if(i>=40 && i<80){
                pMyLabel[i]->setGeometry(450,320-seat*(i-39),100,100);
            }
        }
        return;
    }

    if(integralVal<96){
        seat=90;
        for(int i=0;i<myLabel::vali;i++){  //第二关
            pMyLabel[i]->can=0;
            if(i<10){
                pMyLabel[i]->setGeometry(160+20*i,350,100,100);
            }else if(i>=10 && i<20){
                pMyLabel[i]->setGeometry(770-20*(i-9),350,100,100);
            }else if(i>=20 && i<26){
                pMyLabel[i]->setGeometry(145+seat*(i-19),50,100,100);
            }else if(i>=26 && i<33){
                pMyLabel[i]->setGeometry(100+seat*(i-25),140,100,100);
            }else if(i>=33 && i<39){
                pMyLabel[i]->setGeometry(145+seat*(i-32),230,100,100);
            }else if(i>=39 && i<43){
                pMyLabel[i]->setGeometry(235+seat*(i-38),95,100,100);
            }else if(i>=43 && i<47){
                pMyLabel[i]->setGeometry(235+seat*(i-42),185,100,100);
            }else if(i>=47 && i<50){
                pMyLabel[i]->setGeometry(370+seat*(i-46),140,100,100);
            }
        }
        return;

    }

    QMessageBox::about(this,"##胜利##","恭喜!你已经消灭全部图形...\t\t\n");
}

void Widget::pBut_2SlotFun()
{
    QString str = QString("恭喜你,你的积分为 %1 \t\t\n").arg(integralVal);
    QMessageBox::about(this,"查看积分",str);
}

void Widget::positionChanged(qint64)
{
    player->play();
}


 以下是widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1000</width>
    <height>642</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>我爱消消乐</string>
  </property>
  <widget class="QGroupBox" name="groupBox">
   <property name="geometry">
    <rect>
     <x>-800</x>
     <y>560</y>
     <width>911</width>
     <height>131</height>
    </rect>
   </property>
   <property name="title">
    <string>放置框</string>
   </property>
   <widget class="QTextBrowser" name="textBrowser_3">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>110</width>
      <height>110</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextBrowser" name="textBrowser_4">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>10</y>
      <width>110</width>
      <height>110</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextBrowser" name="textBrowser_5">
    <property name="geometry">
     <rect>
      <x>250</x>
      <y>10</y>
      <width>110</width>
      <height>110</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextBrowser" name="textBrowser_6">
    <property name="geometry">
     <rect>
      <x>370</x>
      <y>10</y>
      <width>110</width>
      <height>110</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextBrowser" name="textBrowser_7">
    <property name="geometry">
     <rect>
      <x>490</x>
      <y>10</y>
      <width>110</width>
      <height>110</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextBrowser" name="textBrowser_8">
    <property name="geometry">
     <rect>
      <x>610</x>
      <y>10</y>
      <width>110</width>
      <height>110</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextBrowser" name="textBrowser_9">
    <property name="geometry">
     <rect>
      <x>730</x>
      <y>10</y>
      <width>110</width>
      <height>110</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QTextBrowser" name="textBrowser">
   <property name="geometry">
    <rect>
     <x>930</x>
     <y>290</y>
     <width>960</width>
     <height>470</height>
    </rect>
   </property>
  </widget>
  <widget class="QGroupBox" name="groupBox_2">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>120</y>
     <width>581</width>
     <height>331</height>
    </rect>
   </property>
   <property name="title">
    <string/>
   </property>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>140</y>
      <width>171</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>开始游戏</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>330</x>
      <y>140</y>
      <width>171</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>查看积分</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>230</y>
      <width>351</width>
      <height>91</height>
     </rect>
    </property>
    <property name="text">
     <string>游戏介绍:将3张相同的图形选入框中即可消除,全部消除则获胜。</string>
    </property>
    <property name="textFormat">
     <enum>Qt::AutoText</enum>
    </property>
    <property name="scaledContents">
     <bool>false</bool>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值