当前位置:   article > 正文

结合QT与STM32的串口通讯之跑马游戏设计与实现_利用q和stm

利用q和stm

通讯界面

在这里插入图片描述

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

#include <QTimer>
#include <QString>
#include <QByteArray>

#include <QMessageBox>

#include "childwidget.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:

    //连接信号槽
    void ConnectSignal();

    //界面初始化显示设置
    void WidgetInitial();

    //针对串口的操作
    void SerialPortOnorOFF();
    void SerialPortSetBaud();
    void SerialPortSetDataBit();
    void SerialPortSetFlowContronl();
    void SerialPortSetParity();
    void SerialPortSetStopBit();

    //针对定时器的操作
     void UpdateSerialPortNumber();
     void UpdateSendAutomaic();
     void UpdateReceive();

     //启动发送定时器
     void Send_automatic_Start();

     //子窗口退出到父窗口
     void ChildtoParent();

     //关闭
     void closeApp();

     //十六进制选择
     void checkChange();

     //下位机数据解析开启
     void dataAnalysis();

     //显示胜利界面
     void showVictroy();

private:
    Ui::Widget *ui;

private:
    QSerialPort *my_serial_port = new QSerialPort(this);

    QSerialPortInfo *my_serial_port_info = new QSerialPortInfo();

    QTimer *timer_serial_port_number = new QTimer();
    QTimer *timer_send_automatic = new QTimer();
    QTimer *timer_receive_data = new QTimer();
    QTimer *timer_exitGameKey4 = new QTimer();

    QString serial_port_name = "";
public:
    ChildWidget childW;
    bool is_hex = false;
    bool is_Startgame = false;
    bool is_ExitFromVictroy = false;//胜利界面跳出时候,是否按下key4
};

#endif // WIDGET_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("通讯连接");
    //连接信号槽
    ConnectSignal();

    WidgetInitial();

}

Widget::~Widget()
{
    qDebug()<<"exit UI";
    delete ui;
}

void Widget::ConnectSignal()
{
    //通讯连接
    connect(ui->pushButton_openport,SIGNAL(clicked(bool)),this, SLOT(SerialPortOnorOFF()));

    //通讯参数选择
    connect(ui->comboBox_baudrate, SIGNAL(currentIndexChanged(int)), this, SLOT(SerialPortSetBaud()));
    connect(ui->comboBox_databit,SIGNAL(currentIndexChanged(int)),this,SLOT(SerialPortSetDataBit()));
    connect(ui->comboBox_flowcontrol,SIGNAL(currentIndexChanged(int)),this,SLOT(SerialPortSetFlowContronl()));
    connect(ui->comboBox_checkbit,SIGNAL(currentIndexChanged(int)),this,SLOT(SerialPortSetParity()));
    connect(ui->comboBox_stopbit,SIGNAL(currentIndexChanged(int)),this,SLOT(SerialPortSetStopBit()));


    //串口号
    connect(timer_serial_port_number, SIGNAL(timeout()), this, SLOT(UpdateSerialPortNumber()));
    //发送数据给下位机
    connect(timer_send_automatic,SIGNAL(timeout()),this,SLOT(UpdateSendAutomaic()));
   //接收下位机数据
    connect(timer_receive_data,SIGNAL(timeout()),this,SLOT(UpdateReceive()));

    //进入游戏界面
    connect(ui->pushButton_gamein,SIGNAL(clicked(bool)),this,SLOT(Send_automatic_Start()));

    //子窗口退出信号触发槽函数
    connect(&childW,&ChildWidget::exitsignal,this,&Widget::ChildtoParent);

    //开发板按键4退出
    connect(timer_exitGameKey4,&QTimer::timeout,this,&Widget::ChildtoParent);

    //主窗口按键退出

    connect(ui->pushButton_exit,&QPushButton::clicked,this,&Widget::closeApp);

    //十六进制选择
    connect(ui->checkBoxHex,&QCheckBox::stateChanged,this,&Widget::checkChange);

    //游戏开始信号触发
    connect(&childW,&ChildWidget::startsignal,this,&Widget::dataAnalysis);

    //游戏胜利触发信号,开启发送定时器,给下位机发送胜利信息
    connect(&childW,&ChildWidget::showVicsignal,this,&Widget::showVictroy);


}

void Widget::WidgetInitial()//界面初始化显示设置
{

    timer_serial_port_number->start(10);//10ms刷新一次串口信息

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){

        qDebug() << "serial_port_name :"<<serial_port_name;
        qDebug()<<"info.portName():"<<info.portName();
        if(serial_port_name.compare(info.portName())){
            ui->comboBox_IO->clear();
            ui->comboBox_IO->addItem(info.portName());
            my_serial_port->setPortName(info.portName());
        }else{

            serial_port_name = info.portName();
        }
    }

    qDebug()<<"界面初始化完成";
}

void Widget::SerialPortOnorOFF()
{
    QString str = ui->pushButton_openport->text();

    if(str.compare("通讯连接")==0)
    {


        // 以 读写模式 打开串口
        bool is_open = my_serial_port->open(QIODevice::ReadWrite);

        if(is_open){
            ui->pushButton_openport->setText("连接成功");
            // 当串口成功打开后 配置基本传输设置
            SerialPortSetBaud();
            SerialPortSetDataBit();
            SerialPortSetFlowContronl();
            SerialPortSetParity();
            SerialPortSetStopBit();
            timer_receive_data->start(50);  // 50 ms 接收一次数据
        }


    }
    else
    {
        ui->pushButton_openport->setText("通讯连接");
        my_serial_port->close();   // 关闭串口
        timer_receive_data->stop();

        qDebug()<<"通讯断开";

    }
}


void Widget::SerialPortSetBaud()//波特率
{
    qDebug()<<"进入波特率设置";

    int index = ui->comboBox_baudrate->currentIndex();

    switch (index) {
    case 0:
        my_serial_port->setBaudRate(QSerialPort::Baud1200);
        break;
    case 1:
        my_serial_port->setBaudRate(QSerialPort::Baud2400);
        break;
    case 2:
        my_serial_port->setBaudRate(QSerialPort::Baud4800);
        break;
    case 3:
        my_serial_port->setBaudRate(QSerialPort::Baud9600);
        break;
    case 4:
        my_serial_port->setBaudRate(QSerialPort::Baud19200);
        break;
    case 5:
        my_serial_port->setBaudRate(QSerialPort::Baud38400);
        break;
    case 6:
        my_serial_port->setBaudRate(QSerialPort::Baud57600);
        break;
    case 7:
        my_serial_port->setBaudRate(QSerialPort::Baud115200);
        break;
    case 8:
        my_serial_port->setBaudRate(QSerialPort::UnknownBaud);
        break;
    }
}

void Widget::SerialPortSetDataBit()//数据位
{
    qDebug()<<"数据位设置";
    int index = ui->comboBox_databit->currentIndex();
    switch (index) {
    case 0:
        my_serial_port->setDataBits(QSerialPort::Data8);
        break;
    case 1:
        my_serial_port->setDataBits(QSerialPort::Data7);
        break;
    case 2:
        my_serial_port->setDataBits(QSerialPort::Data6);
        break;
    case 3:
        my_serial_port->setDataBits(QSerialPort::Data5);
        break;
    case 4:
        my_serial_port->setDataBits(QSerialPort::UnknownDataBits);
        break;
    }
}

void Widget::SerialPortSetFlowContronl()//流控制
{
    int index = ui->comboBox_flowcontrol->currentIndex();
    switch(index)
    {
    case 0:
        my_serial_port->setFlowControl(QSerialPort::NoFlowControl);
        break;
    case 1:
        my_serial_port->setFlowControl(QSerialPort::HardwareControl);
        break;
    case 2:
        my_serial_port->setFlowControl(QSerialPort::SoftwareControl);
        break;
    case 3:
        my_serial_port->setFlowControl(QSerialPort::UnknownFlowControl);
        break;
    }
}

void Widget::SerialPortSetParity()//校验位
{
    int index = ui->comboBox_checkbit->currentIndex();

    switch (index) {
    case 0:
        my_serial_port->setParity(QSerialPort::NoParity);
        break;
    case 1:
        my_serial_port->setParity(QSerialPort::EvenParity);
        break;
    case 2:
        my_serial_port->setParity(QSerialPort::OddParity);
        break;
    case 3:
        my_serial_port->setParity(QSerialPort::SpaceParity);
        break;
    case 4:
        my_serial_port->setParity(QSerialPort::MarkParity);
        break;
    case 5:
        my_serial_port->setParity(QSerialPort::UnknownParity);
        break;
    }
}

void Widget::SerialPortSetStopBit()//停止位
{
    int index = ui->comboBox_stopbit->currentIndex();

    switch (index) {
    case 0:
        my_serial_port->setStopBits(QSerialPort::OneStop);
        break;
    case 1:
        my_serial_port->setStopBits(QSerialPort::OneAndHalfStop);
        break;
    case 2:
        my_serial_port->setStopBits(QSerialPort::TwoStop);
        break;
    case 3:
        my_serial_port->setStopBits(QSerialPort::UnknownStopBits);
        break;
    }
}

//更新串口号
void Widget::UpdateSerialPortNumber(){

   // qDebug() <<"进入更新串口号函数";

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){

        if(serial_port_name.compare(info.portName())){
            ui->comboBox_IO->clear();
            ui->comboBox_IO->addItem(info.portName());
            my_serial_port->setPortName(info.portName());
            return;
        }else{

            serial_port_name = info.portName();
            return;
        }
    }

    ui->comboBox_IO->clear();
    ui->comboBox_IO->addItem("无");



}

//发送数据给下位机
void Widget::UpdateSendAutomaic()
{
    //qDebug()<<"发送数据给下位机";
    QByteArray data;
    data.resize(11);
    data[0] = 0xaa;
    data[1] = 0xf9;

    for(int i = 0; i < 8; i++)
    {
        data[i+2] = 0x00;
    }
    data[10] = 0xdd;
    if(childW.VictroyShow.isOpenVictroy && !is_ExitFromVictroy)
    {
        //victory界面开启,发送数据给下位机
        if(1 == childW.horse_win)
        {
            //horse1赢
           data[2] = 0x01;
        }
        else if(2 == childW.horse_win)
        {
            //horse2赢
            data[3] = 0x02;

        }
        else if(3 == childW.horse_win)
        {
            //horse3赢
            data[4] = 0x03;

        }
    }


    my_serial_port->write(data);//16进制发送给下位机
    qDebug()<<data.toHex();
    timer_send_automatic->stop();
}

void Widget::Send_automatic_Start()
{
    qDebug()<<"进入游戏界面";
    childW.isExit = false;
    childW.show();
    this->hide();
    is_hex = true;
}

//更新接收数据
void Widget::UpdateReceive()
{
    //数据解析
    QByteArray data = my_serial_port->readAll();
    QString str(data);

    if(!data.isNull())
    {
        if(is_hex)
        {
            //HEX文件接收
            QByteArray temp;
            data = data.toHex();

            for(int i = 0; i < data.length()/2; ++i)
            {
                temp += data.mid(i*2,2) + "  ";//每两个字节加一个空格
            }
           ui->plainTextEdit__receive->appendPlainText(temp);

           if(is_Startgame && !childW.isExit)
           {
               if(data.mid(2*2,2) == "01")
               {
                 //按键1按下
                 qDebug()<<"key1的值:"<<data.mid(2*2,2);
                 childW.key1Move();

               }
               else if(data.mid(2*3,2) == "02")
               {
                   //按键2按下
                   qDebug()<<"key2的值:"<<data.mid(2*3,2);
                   childW.key2Move();
               }
               else if(data.mid(2*4,2) == "03")
               {
                   //按键3按下
                   qDebug()<<"key3的值:"<<data.mid(2*4,2);
                   childW.key3Move();
               }
           }
           if(data.mid(2*5,2) == "04")
           {
               //按键4按下,退出游戏
               qDebug()<<"key4的值:"<<data.mid(2*5,2);
               if(!timer_exitGameKey4->isActive())
               {
                    timer_exitGameKey4->start(50);
               }

           }
        }
        else
        {
            //文本文件接收
            ui->plainTextEdit__receive->appendPlainText(str);
        }
    }


}

//子窗口退出到父窗口
void Widget::ChildtoParent()
{
    childW.LableShowChange();
    childW.close();
    this->show();
    is_Startgame = false;

    if(!ui->checkBoxHex->isChecked())
    {
        is_hex  = false;
        qDebug()<<"文本形式解析";
    }
    else
    {
        is_hex = true;
        qDebug()<<"十六进制解析";
    }
    if(timer_exitGameKey4->isActive())
    {
        timer_exitGameKey4->stop();
    }
    if(childW.VictroyShow.isOpenVictroy)
    {
        /***/
        //还原下位机各引脚信号
        is_ExitFromVictroy = true;
        this->UpdateSendAutomaic();
        is_ExitFromVictroy = false;
        /***/
        qDebug()<<"胜利界面关闭";
        childW.VictroyShow.close();
    }
}

void Widget::closeApp()
{
    this->close();
}

void Widget::checkChange()
{

    if(!ui->checkBoxHex->isChecked())
    {
        ui->checkBoxHex->setChecked(false);
        is_hex  = false;
        qDebug()<<"文本形式解析";
    }
    else
    {
        ui->checkBoxHex->setChecked(true);
        is_hex = true;
        qDebug()<<"十六进制解析";
    }


}

void Widget::dataAnalysis()
{

    //开始游戏
    qDebug()<<"开始游戏";
    is_Startgame = true;

}

void Widget::showVictroy()
{
     timer_send_automatic->start(50);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466

游戏界面

在这里插入图片描述

#ifndef CHILDWIDGET_H
#define CHILDWIDGET_H

#include <QWidget>
#include <QTimer>
#include "victroy.h"

namespace Ui {
class ChildWidget;
}

class ChildWidget : public QWidget
{
    Q_OBJECT

public:
    explicit ChildWidget(QWidget *parent = 0);
    ~ChildWidget();
signals:
    void exitsignal();
    void startsignal();
    void showVicsignal();
public slots:
    void exitslot();
    void StartorStopGame();
    void key1Move();
    void key2Move();
    void key3Move();
    void victroyHorse();
    void GameReturn();

public:
    int horse1_x;
    int horse2_x;
    int horse3_x;
    int offsetx;
    int offsetx_start;
    int horse1_startx;
    int horse2_startx;
    int horse3_startx;
    int horse1_starty;
    int horse2_starty;
    int horse3_starty;
    bool isStartKey1;
    bool isStartKey2;
    bool isStartKey3;
    bool isExit;
    int horse_win;

    Victroy VictroyShow;

    void LableShowChange();
    void VictroyShowUI();

private:
    Ui::ChildWidget *ui;
    QTimer *timerVictroy = new QTimer();
};

#endif // CHILDWIDGET_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
#include "childwidget.h"
#include "ui_childwidget.h"
#include <QDebug>

ChildWidget::ChildWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChildWidget)
{
    ui->setupUi(this);
    horse1_x = ui->label_horse1->x();
    horse2_x = ui->label_horse2->x();
    horse3_x = ui->label_horse3->x();
    horse1_startx = horse1_x;
    horse2_startx = horse2_x;
    horse3_startx = horse3_x;
    horse1_starty = ui->label_horse1->y();
    horse2_starty = ui->label_horse2->y();
    horse3_starty = ui->label_horse3->y();
    offsetx = 5;
    offsetx_start = 80;
    isStartKey1 = true;
    isStartKey2 = true;
    isStartKey3 = true;
    isExit = false;
    horse_win = 0;

    ui->pushButton_key1->setEnabled(false);
    ui->pushButton_key2->setEnabled(false);
    ui->pushButton_key3->setEnabled(false);

    connect(ui->pushButton_exit,&QPushButton::clicked,this,&ChildWidget::exitslot);
    connect(ui->pushButton_start,&QPushButton::clicked,this,&ChildWidget::StartorStopGame);

    //模拟按键
     connect(ui->pushButton_key1,&QPushButton::clicked,this,&ChildWidget::key1Move);
     connect(ui->pushButton_key2,&QPushButton::clicked,this,&ChildWidget::key2Move);
     connect(ui->pushButton_key3,&QPushButton::clicked,this,&ChildWidget::key3Move);

    //胜利弹框
     connect(timerVictroy,&QTimer::timeout,this,&ChildWidget::victroyHorse);

     //鼠标事件销毁胜利弹框,返回游戏界面
     connect(&VictroyShow,&Victroy::exitsig,this,&ChildWidget::GameReturn);


    //设置背景图片
    this->setAutoFillBackground(true);
    QPalette pal;
    QPixmap pix(":/new/prefix1/field.png");
    pal.setBrush(QPalette::Window,QBrush(pix));
    this->setPalette(pal);


}

ChildWidget::~ChildWidget()
{

    delete ui;
}


void ChildWidget::exitslot()
{
    emit exitsignal();
}

void ChildWidget::StartorStopGame()
{
    ui->pushButton_key1->setEnabled(true);
    ui->pushButton_key2->setEnabled(true);
    ui->pushButton_key3->setEnabled(true);
    ui->label_horse1->setPixmap(QPixmap(":/new/prefix1/horse.png"));
    ui->label_horse2->setPixmap(QPixmap(":/new/prefix1/horse.png"));
    ui->label_horse3->setPixmap(QPixmap(":/new/prefix1/horse.png"));
    timerVictroy->start(20);
    emit startsignal();
}

void ChildWidget::LableShowChange()
{
    isStartKey1 = true;
    isStartKey2 = true;
    isStartKey3 = true;
    isExit = true;
    if(!timerVictroy->isActive())
    {
        timerVictroy->start(20);
    }
    if(0 == offsetx)
    {
        offsetx = 5;
    }
    ui->pushButton_key1->setEnabled(false);
    ui->pushButton_key2->setEnabled(false);
    ui->pushButton_key3->setEnabled(false);
    ui->label_horse1->move(horse1_startx,horse1_starty);
    ui->label_horse2->move(horse2_startx,horse2_starty);
    ui->label_horse3->move(horse3_startx,horse3_starty);
    ui->label_horse1->setPixmap(QPixmap(":/new/prefix1/Num1.png"));
    ui->label_horse2->setPixmap(QPixmap(":/new/prefix1/Num2.png"));
    ui->label_horse3->setPixmap(QPixmap(":/new/prefix1/Num3.png"));
}

void ChildWidget::key1Move()
{
    horse1_x = ui->label_horse1->x();
    if(isStartKey1)
    {
        ui->label_horse1->move(horse1_x - offsetx_start,horse1_starty);
        isStartKey1 = false;
    }
    else
    {
        ui->label_horse1->move(horse1_x - offsetx,ui->label_horse1->y());
    }
}

void ChildWidget::key2Move()
{
    horse2_x = ui->label_horse2->x();
    if(isStartKey2)
    {
        ui->label_horse2->move(horse2_x - offsetx_start,horse2_starty);
        isStartKey2 = false;
    }
    else
    {
        ui->label_horse2->move(horse2_x - offsetx,ui->label_horse2->y());
    }
}

void ChildWidget::key3Move()
{
    horse3_x = ui->label_horse3->x();
    if(isStartKey3)
    {
        ui->label_horse3->move(horse3_x - offsetx_start,horse3_starty);
        isStartKey3 = false;
    }
    else
    {
        ui->label_horse3->move(horse3_x - offsetx,horse3_starty);
    }
}

void ChildWidget::victroyHorse()
{
    int tempPosition1 = ui->label_horse1->x();
    int tempPosition2 = ui->label_horse2->x();
    int tempPosition3 = ui->label_horse3->x();

    if(!(isStartKey1 && isStartKey2 && isStartKey3))
    {
        if(tempPosition1 <= 80 || tempPosition2 <= 80 || tempPosition3 <= 80)
        {
            timerVictroy->stop();
            if(tempPosition1 <= 80 && tempPosition1 < tempPosition2 && tempPosition1 < tempPosition3)
            {
                //horse1胜利
                qDebug() <<"horse1胜利";
                VictroyShow.victoryHorse1Show();
                horse_win = 1;
            }
            else if(tempPosition2 <= 80 && tempPosition2 < tempPosition1 && tempPosition2 < tempPosition3)
            {
                qDebug() <<"horse2胜利";
                VictroyShow.victoryHorse2Show();
                horse_win = 2;
            }
            else if(tempPosition3 <= 80 && tempPosition3 < tempPosition1 && tempPosition3 < tempPosition1)
            {
                qDebug() <<"horse3胜利";
                VictroyShow.victoryHorse3Show();
                horse_win = 3;
            }
            else
            {
                qDebug()<<"horse有并列第一";
            }
            offsetx = 0;
            VictroyShowUI();
        }

    }

}

void ChildWidget::VictroyShowUI()
{
    VictroyShow.show();
    this->hide();
    emit showVicsignal();
}

void ChildWidget::GameReturn()
{
    VictroyShow.close();
    LableShowChange();
    this->show();
    horse_win = 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203

游戏中
在这里插入图片描述

胜利界面

#ifndef VICTROY_H
#define VICTROY_H

#include <QWidget>
#include <QMouseEvent>
namespace Ui {
class Victroy;
}

class Victroy : public QWidget
{
    Q_OBJECT

public:
    explicit Victroy(QWidget *parent = 0);
    ~Victroy();

private:
    Ui::Victroy *ui;
public:
    void victoryHorse1Show();
    void victoryHorse2Show();
    void victoryHorse3Show();
    bool isOpenVictroy;
signals:
    void exitsig();
protected:
    void mousePressEvent(QMouseEvent *event);
};

#endif // VICTROY_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
#include "victroy.h"
#include "ui_victroy.h"
#include "QDebug"

Victroy::Victroy(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Victroy)
{
    ui->setupUi(this);
    isOpenVictroy = false;
    setAttribute(Qt::WA_DeleteOnClose);
}

Victroy::~Victroy()
{
    isOpenVictroy = false;
    qDebug()<<"胜利界面析构";
    delete ui;
}

void Victroy::victoryHorse1Show()
{
    //设置背景图片
    isOpenVictroy = true;
    this->setAutoFillBackground(true);
    QPalette pal;
    QPixmap pix(":/new/prefix1/victory1.jpg");
    pal.setBrush(QPalette::Window,QBrush(pix));
    this->setPalette(pal);

}

void Victroy::victoryHorse2Show()
{
    //设置背景图片
    isOpenVictroy = true;
    this->setAutoFillBackground(true);
    QPalette pal;
    QPixmap pix(":/new/prefix1/victory 2.jpg");
    pal.setBrush(QPalette::Window,QBrush(pix));
    this->setPalette(pal);

}

void Victroy::victoryHorse3Show()
{
    //设置背景图片
    isOpenVictroy = true;
    this->setAutoFillBackground(true);
    QPalette pal;
    QPixmap pix(":/new/prefix1/victory3.jpg");
    pal.setBrush(QPalette::Window,QBrush(pix));
    this->setPalette(pal);

}

void Victroy::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        emit exitsig();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/954163
推荐阅读
相关标签
  

闽ICP备14008679号