Qt斗地主界面的发牌,抢地主,出牌界面的实现。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ZQL2288/article/details/61926982

      无聊的时候挺喜欢玩斗地主,在学完Qt之后我想做一个斗地主,在完成了界面和逻辑之后最终碰到了AI出牌算法这个问题。对于这种比较难得问题,小生束手难测,仅仅完成了界面。界面逻辑已经完成的差不多了。现将其中的核心代码开源,望各位道友能指点一二。代码写的有点混乱,逻辑可能不清楚,还希望各位道友能多多海涵。

在以后用空的时候会将这个抢地主,自动发牌,出牌的逻辑模块独立剥离出来成为一个完整独立的类。

里面的牌的界面是我用PS做的,我会在文章下方写下文件的下载地址。望各位道友能有所收获。

最后说一句,写的不是代码,是孤独啊。

 

ifndef CARDWIDGET_H
#define CARDWIDGET_H

#include <QWidget>
#include <QLabel>
#include "verdesktop.h"

class cardWidget : public QWidget
{
    Q_OBJECT
public:
    QLabel *cardLabel;//牌面
    void setGeometryAndLabel();//设置坐标与大小
    void setAnimation();//创建动画
    void setNoAnimation();//创建无补间动画
    void writeCardWidget_X(int x)//写入坐标X
    {
        this->cardWidget_X=x;
    }
    void writeCardNum(int Num);//写入卡号
    int readCardNum();//读取卡号
    void writeCardID(int id)
    {
        this->cardID=id;
    }//写入卡的id
    int readCardID()
    {
        return this->cardID;
    }//读取卡的id
    explicit cardWidget(QWidget *parent = 0);
    bool isup=false;
    bool isdown=true;
private:
    void mousePressEvent(QMouseEvent *event);//单击事件
    void mouseDoubleClickEvent(QMouseEvent *event);
    int cardWidget_X;//x坐标
    int cardID;//卡牌的id
    int cardNum=0;//定义从A-----大王分别为1------------15
    float ver;
    VerDesktop *verDesktop;
signals:
    void mousePressSend(int ID);//发送信号单击信号
    void animationFinished(int ID);//发送动画结束信号
public slots:
    void getAnimationFinished();//接收动画结束信号
};

#endif // CARDWIDGET_H

 

 
#include "cardwidget.h" #include <QDebug> #include <QMouseEvent> #include <QPropertyAnimation> cardWidget::cardWidget(QWidget *parent) : QWidget(parent) { verDesktop=new VerDesktop(this); ver=verDesktop->getVerDesktop(); } void cardWidget::setGeometryAndLabel() { this->hide(); int x=(cardNum/4)*100;//通过寻图算法找到相应的扑克面 int y=(cardNum%4)*150; cardLabel=new QLabel(this); cardLabel->clear(); QPixmap MMM(":/res/image/cards.png");//提供大图的扑克Pixmap QPixmap cardpix=MMM.copy(x,y,100*ver,150*ver); cardLabel->setGeometry(0,0,100*ver,150*ver); cardLabel->setPixmap(cardpix.scaled(100*ver,150*ver)); cardLabel->setScaledContents(true); } void cardWidget::getAnimationFinished() { emit animationFinished(cardID); } void cardWidget::writeCardNum(int Num) { this->cardNum=Num; } int cardWidget::readCardNum() { return cardNum; } void cardWidget::mousePressEvent(QMouseEvent *event) { Q_UNUSED(event); isup=!isup; emit mousePressSend(cardID); qDebug()<<cardID; qDebug()<<cardNum; } void cardWidget::mouseDoubleClickEvent(QMouseEvent *event) { Q_UNUSED(event); } void cardWidget::setAnimation()//提供了一个动画实现了发牌的动态展现 { this->show(); QPropertyAnimation *animation0=new QPropertyAnimation(this,"geometry"); QPropertyAnimation *animation1=new QPropertyAnimation(this->cardLabel,"geometry"); animation0->setDuration(300); animation1->setDuration(300); animation0->setStartValue(QRect(350*ver,0,50*ver,75*ver)); animation0->setEndValue(QRect(cardID*40*ver,120*ver,100*ver,150*ver)); animation1->setStartValue(QRect(0,0,50*ver,75*ver)); animation1->setEndValue(QRect(0,0,100*ver,150*ver)); animation1->start(QAbstractAnimation::DeleteWhenStopped); animation0->start(QAbstractAnimation::DeleteWhenStopped); connect(animation0,SIGNAL(finished()),this,SLOT(getAnimationFinished())); connect(animation1,SIGNAL(finished()),this,SLOT(getAnimationFinished())); } void cardWidget::setNoAnimation() { this->show(); this->setGeometry(cardID*40*ver,120*ver,100*ver,150*ver); }

 

 


 

 

 

 


 
#ifndef CARDSBASEWIDGET_H
#define CARDSBASEWIDGET_H

#include <QWidget>
#include "verdesktop.h"
#include "cardwidget.h"

class CardsBaseWidget : public QWidget
{
    Q_OBJECT
public:
    explicit CardsBaseWidget(QWidget *parent = 0);
    void writeCardsSize(int Size){
        cardSize=Size;
    }
    int readCardsSize(){
        return cardSize;
    }
    cardWidget *cardwidget;
    QVector<cardWidget*>vector_card;
    void setGeometryAndLabels();//设定坐标
private:
    float ver;
    VerDesktop *verDesktop;
    int cardSize=20;

signals:
    void hideCardssssLabel();//关闭信号
public slots:
    void getCardWidgetID(int id);//获取单击widgetID
    void getCardAnimationFinshedID(int id);//获取动画结束widgetID
    void openGame();//开始游戏
#ifndef BASEWIDGET_H
#define BASEWIDGET_H

#include <QWidget>
#include <verdesktop.h>
#include <QPainter>
#include <QLabel>
#include <QTime>
#include<QPushButton>
#include"cardsbasewidget.h"
#include "mythreadrandomnum.h"

class BaseWidget : public QWidget
{
    Q_OBJECT
public:
    explicit BaseWidget(QWidget *parent = 0);
    void writeLeftCardSize(int size);//写入
    void writeRightCardSize(int size);//写入
    void showChuOrBuyaoButton();//显示出牌或者不出

private:
    float ver;
    VerDesktop *verDesktop;
    CardsBaseWidget *cardBaseWidget;
    void setCardBaseWidgetOnBaseWidget();//将cardbasewidget定义在BaseWidget
    void setBaseWidgetSizeAndBackground();//设定BaseWidget的界面大小
    void changeBaseBackgroundPicture();//改变默认背景图片
    void setNormalBaseBackgroundPicture();//设定默认的背景图片
    void paintEvent(QPaintEvent *event);//绘制背景图片
    QLabel *dizhuLabel;//地主
    QLabel *nongmin0Label;//农民1
    QLabel *nongmin1Label;//农民2
    QLabel *cardsAllLabel;//总牌数
    QLabel *rightCardAllLabel;//左方牌
    QLabel *leftCardAllLabel;//右方牌
    QWidget *rightCardSizeWidget;//左方牌数
    QWidget *leftCardSizeWidget;//右方牌数
    QLabel *rightCardSizeLabel;
    QLabel *leftCardSizeLabel;
    QPushButton *openGameButton;//开始游戏
    QPushButton *getDizhuButton;//抢地主
    QPushButton *abandonDizhuButton;//不抢地主
    QPushButton *chupaiButton;//出牌
    QPushButton *buyaoButton;//不要
    void setPersonPixmapLabel();//设定角色图片
    QList<int> numbersList;
    void getRandomCardNumList();//获取随机值
    QList<int> userNumCardList;//用户的牌数与号
    QList<int> nongmin0NumCardList;//农民0的牌数与号
    QList<int> nongmin1NumCardList;//农民的牌数与号
    void setUserRandomCardNumList();//为用户分配随机值
    void sortUserRandomCardNumList();//排序用户的牌
signals:

public slots:
    void getHideCardsss();//关闭label--cardsss
    void openGameButtonSlot();//开始游戏的slot
    void getDizhuButtonSlot();//抢地主的slot
    void abandonDizhuButtonSlot();//不抢的slot
    void chupaiButtonSlot();//出牌的slot
    void buyaoButtonSlot();//不要的slot
};

 

#include "basewidget.h"
#include <QPropertyAnimation>

BaseWidget::BaseWidget(QWidget *parent) : QWidget(parent)
{
    verDesktop=new VerDesktop(this);
    ver=verDesktop->getVerDesktop();
    setBaseWidgetSizeAndBackground();
    setCardBaseWidgetOnBaseWidget();
    setPersonPixmapLabel();
    connect(cardBaseWidget,SIGNAL(hideCardssssLabel()),this,SLOT(getHideCardsss()));
    connect(openGameButton,SIGNAL(clicked(bool)),this,SLOT(openGameButtonSlot()));
    connect(getDizhuButton,SIGNAL(clicked(bool)),this,SLOT(getDizhuButtonSlot()));
    connect(abandonDizhuButton,SIGNAL(clicked(bool)),SLOT(abandonDizhuButtonSlot()));
    connect(chupaiButton,SIGNAL(clicked(bool)),this,SLOT(chupaiButtonSlot()));
    connect(buyaoButton,SIGNAL(clicked(bool)),this,SLOT(buyaoButtonSlot()));
}
void BaseWidget::setBaseWidgetSizeAndBackground()
{
    this->setGeometry(5*ver,5*ver,1300*ver,900*ver);
    this->setMinimumSize(1300*ver,900*ver);
    this->setMaximumSize(1300*ver,900*ver);
}
void BaseWidget::changeBaseBackgroundPicture()
{
}
void BaseWidget::setNormalBaseBackgroundPicture()
{
}
void BaseWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPixmap background0;
    background0.load(":/res/image/basebackground0.png");
    background0.scaled(1300*ver,900*ver);
    QPainter painter(this);
    painter.save();
    painter.drawPixmap(0,0,1300*ver,900*ver,background0);
    painter.restore();
}
void BaseWidget::setPersonPixmapLabel()
{
    dizhuLabel=new QLabel(this);
    nongmin0Label=new QLabel(this);
    nongmin1Label=new QLabel(this);
    cardsAllLabel=new QLabel(this);
    openGameButton=new QPushButton(this);
    rightCardAllLabel=new QLabel(this);
    leftCardAllLabel=new QLabel(this);
    leftCardSizeWidget=new QWidget(this);
    leftCardSizeLabel=new QLabel(leftCardSizeWidget);
    rightCardSizeWidget=new QWidget(this);
    rightCardSizeLabel=new QLabel(rightCardSizeWidget);
    getDizhuButton=new QPushButton(this);
    abandonDizhuButton=new QPushButton(this);
    chupaiButton=new QPushButton(this);
    buyaoButton=new QPushButton(this);
    QPixmap nongmin0Pix(":/res/image/nongmin0.png");
    nongmin0Label->setGeometry(0,280*ver,155*ver,210*ver);
    nongmin0Label->setPixmap(nongmin0Pix.scaled(155*ver,210*ver));
    QPixmap nongmin1Pix(":/res/image/nongmin1.png");
    nongmin1Label->setGeometry(1148*ver,280*ver,155*ver,210*ver);
    nongmin1Label->setPixmap(nongmin1Pix.scaled(155*ver,210*ver));
    QPixmap dizhuPix(":/res/image/dizhu.png");
    dizhuLabel->setGeometry(10*ver,603*ver,170*ver,240*ver);
    dizhuLabel->setPixmap(dizhuPix);
    QPixmap cardsssPix(":/res/image/cardsss.png");
    cardsAllLabel->setGeometry(550*ver,370*ver,150*ver,150*ver);
    cardsAllLabel->setPixmap(cardsssPix.scaled(150*ver,150*ver));
    cardsAllLabel->setScaledContents(true);
    QPixmap rightCardsssPix(":/res/image/rightcardsss.png");
    rightCardAllLabel->setPixmap(rightCardsssPix.scaled(150*ver,110*ver));
    rightCardAllLabel->setGeometry(155*ver,370*ver,150*ver,110*ver);
    rightCardAllLabel->hide();
    QPixmap leftCardssPix(":/res/image/leftcardsss.png");
    leftCardAllLabel->setPixmap(leftCardssPix.scaled(150*ver,110*ver));
    leftCardAllLabel->setGeometry(1005*ver,370*ver,150*ver,110*ver);
    leftCardAllLabel->hide();
    cardsAllLabel->hide();
    openGameButton->setGeometry(550*ver,370*ver,100*ver,50*ver);
    openGameButton->setText(tr("开始游戏"));
    rightCardSizeWidget->setGeometry(240*ver,275*ver,50*ver,50*ver);
    rightCardSizeLabel->setGeometry(0,0,50*ver,50*ver);
    rightCardSizeLabel->setText(tr("左部牌数"));
    leftCardSizeWidget->setGeometry(1012*ver,275*ver,50*ver,50*ver);
    leftCardSizeLabel->setGeometry(0,0,50*ver,50*ver);
    leftCardSizeLabel->setText(tr("右部牌数"));
    getDizhuButton->setGeometry(450*ver,475*ver,100*ver,50*ver);
    getDizhuButton->setText(tr("抢地主"));
    getDizhuButton->hide();
    abandonDizhuButton->setGeometry(650*ver,475*ver,100*ver,50*ver);
    abandonDizhuButton->setText("不抢");
    abandonDizhuButton->hide();
    chupaiButton->setGeometry(450*ver,475*ver,100*ver,50*ver);
    chupaiButton->setText("出牌");
    chupaiButton->hide();
    buyaoButton->setGeometry(650*ver,475*ver,100*ver,50*ver);
    buyaoButton->setText("不要");
    buyaoButton->hide();
}
void BaseWidget::showChuOrBuyaoButton()
{
    chupaiButton->show();
    buyaoButton->show();
}
void BaseWidget::setCardBaseWidgetOnBaseWidget()
{
    cardBaseWidget=new CardsBaseWidget(this);
    cardBaseWidget->show();
}
void BaseWidget::getHideCardsss()
{
    rightCardAllLabel->show();
    leftCardAllLabel->show();
    cardsAllLabel->hide();
    getDizhuButton->show();
    abandonDizhuButton->show();
}
void BaseWidget::openGameButtonSlot()
{
    getRandomCardNumList();
    setUserRandomCardNumList();
    sortUserRandomCardNumList();
    cardsAllLabel->show();
    QPropertyAnimation *animation0=new QPropertyAnimation(cardsAllLabel,"geometry");
    animation0->setDuration(6000);
    animation0->setStartValue(QRect(550*ver,370*ver,150*ver,150*ver));
    animation0->setEndValue(QRect(550*ver,420*ver,150*ver,100*ver));
    animation0->start(QAbstractAnimation::DeleteWhenStopped);
    cardBaseWidget->openGame();
    openGameButton->hide();
}
void BaseWidget::writeLeftCardSize(int size)
{
    leftCardAllLabel->setText(QString::number(size));
}
void BaseWidget::writeRightCardSize(int size)
{
    rightCardAllLabel->setText(QString::number(size));
}
void BaseWidget::getRandomCardNumList()
{
    int i,j;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(i=0;i<54;i++)//生成54个随机数
    {
        numbersList.append(qrand()%54);
        bool flag=true;
        while(flag)
        {
            for(j=0;j<i;j++)
            {
                if(numbersList[i]==numbersList[j])
                {
                    break;
                }
            }
            if(j<i)
            {
                numbersList[i]=rand()%54;
            }
            if(j==i)
            {
                flag=!flag;
            }
        }
    }
}
void BaseWidget::setUserRandomCardNumList()
{
    for(int i=0;i<17;i++)
    {
        userNumCardList.append(numbersList[i]);
    }
    for(int i=17;i<34;i++)
    {
        nongmin0NumCardList.append(numbersList[i]);
    }
    for(int i=34;i<51;i++)
    {
        nongmin1NumCardList.append(numbersList[i]);
    }
}
void BaseWidget::sortUserRandomCardNumList()
{
    qDebug()<<"11111111111111111111111";
    qDebug()<<userNumCardList.size();
    qDebug()<<"11111111111111111111111";
    cardBaseWidget->writeCardsSize(userNumCardList.size());
    qSort(userNumCardList.begin(),userNumCardList.end());
    for(int i=0;i<userNumCardList.size();i++)
    {
        qDebug()<<userNumCardList[i];
        cardBaseWidget->vector_card.at(i)->writeCardNum(userNumCardList[i]);
        cardBaseWidget->vector_card.at(i)->cardLabel->clear();
        cardBaseWidget->vector_card.at(i)->setGeometryAndLabel();
    }
}
void BaseWidget::getDizhuButtonSlot()//用户抢完地主后重新刷新牌面显示并且加入三张牌
{
    userNumCardList.append(numbersList[51]);
    userNumCardList.append(numbersList[52]);
    userNumCardList.append(numbersList[53]);
    qDeleteAll(cardBaseWidget->vector_card);
    cardBaseWidget->vector_card.clear();
    cardBaseWidget->writeCardsSize(userNumCardList.size());
    qSort(userNumCardList.begin(),userNumCardList.end());
    cardBaseWidget->setGeometryAndLabels();
    qDebug()<<"22222222222222222222222";
    qDebug()<<userNumCardList.size();
    qDebug()<<"22222222222222222222222";
    for(int i=0;i<userNumCardList.size();i++)
    {
        cardBaseWidget->vector_card.at(i)->writeCardNum(userNumCardList[i]);
        cardBaseWidget->vector_card.at(i)->cardLabel->clear();
        cardBaseWidget->vector_card.at(i)->setGeometryAndLabel();
        cardBaseWidget->vector_card.at(i)->setNoAnimation();
    }
    getDizhuButton->hide();
    abandonDizhuButton->hide();
    showChuOrBuyaoButton();
}
void BaseWidget::abandonDizhuButtonSlot()
{
    nongmin0NumCardList.append(numbersList[51]);
    nongmin0NumCardList.append(numbersList[52]);
    nongmin0NumCardList.append(numbersList[53]);
    getDizhuButton->hide();
    abandonDizhuButton->hide();
    showChuOrBuyaoButton();
}
void BaseWidget::chupaiButtonSlot()//出牌动作的实现,以及后台数据分析进行。
{
    int size=cardBaseWidget->readCardsSize();
    for(int i=0;i<size;i++)
    {
        if(cardBaseWidget->vector_card.at(i)->isup==true)
        {
            int value=cardBaseWidget->vector_card.at(i)->readCardNum();
            userNumCardList.removeOne(value);
            continue;
        }
    }
    qDeleteAll(cardBaseWidget->vector_card);
    cardBaseWidget->vector_card.clear();
    cardBaseWidget->writeCardsSize(userNumCardList.size());
    qSort(userNumCardList.begin(),userNumCardList.end());
    cardBaseWidget->setGeometryAndLabels();
    qDebug()<<"33333333333333333333333";
    qDebug()<<userNumCardList.size();
    qDebug()<<"33333333333333333333333";
    for(int i=0;i<userNumCardList.size();i++)
    {
        cardBaseWidget->vector_card.at(i)->writeCardNum(userNumCardList[i]);
        cardBaseWidget->vector_card.at(i)->cardLabel->clear();
        cardBaseWidget->vector_card.at(i)->setGeometryAndLabel();
        cardBaseWidget->vector_card.at(i)->setNoAnimation();
    }
    chupaiButton->hide();
    buyaoButton->hide();
}
void BaseWidget::buyaoButtonSlot()
{
    chupaiButton->hide();
    buyaoButton->hide();
}

 

#include <QPropertyAnimation>

CardsBaseWidget::CardsBaseWidget(QWidget *parent) : QWidget(parent)
{
    verDesktop=new VerDesktop(this);
    ver=verDesktop->getVerDesktop();
    setGeometryAndLabels();
}
void CardsBaseWidget::setGeometryAndLabels()
{
    int x;
    if(cardSize<10)
    {
        x=250*ver+(10-cardSize)*40*ver;
    }else
    {
        x=250*ver+(20-cardSize)*20*ver;
    }
    int w=(cardSize-1)*40*ver+100*ver;
    this->setGeometry(x,520*ver,w,320*ver);
    for(int i=0;i<cardSize;i++)
    {
        cardwidget=new cardWidget(this);
        cardwidget->writeCardID(i);
        vector_card.append(cardwidget);
        vector_card.at(i)->setGeometryAndLabel();
    }
    for(int i=0;i<cardSize;i++)
    {
        connect(vector_card.at(i),SIGNAL(mousePressSend(int)),this,SLOT(getCardWidgetID(int)));
        connect(vector_card.at(i),SIGNAL(animationFinished(int)),this,SLOT(getCardAnimationFinshedID(int)));
    }
}
void CardsBaseWidget::getCardAnimationFinshedID(int id)
{
    if(id<cardSize-1)
    {
         int ID=id+1;
         vector_card.at(ID)->setAnimation();
         vector_card.at(ID)->show();
    }
    else
    {
        emit hideCardssssLabel();
    }
}
void CardsBaseWidget::getCardWidgetID(int id)
{
    if(vector_card.at(id)->isup==true)
    {
         vector_card.at(id)->setGeometry(id*40*ver,70*ver,100*ver,150*ver);
    }
    if(vector_card.at(id)->isup==false)
    {
          vector_card.at(id)->setGeometry(id*40*ver,120*ver,100*ver,150*ver);
    }
}
void CardsBaseWidget::openGame()
{
       vector_card.at(0)->setAnimation();
       vector_card.at(0)->setGeometryAndLabel();
       vector_card.at(0)->show();
}


核心代码就是上述这些,完整的代码就在下面的链接里。

项目已经上传到Github,如果大家喜欢欢迎给个Start,如有问题,欢迎issue。

https://github.com/zqljintu/Gameinterface-Doudizhu

或者去我的资源页里去寻找。
 

展开阅读全文

没有更多推荐了,返回首页