QT_仿王者荣耀抽奖

QT_仿王者荣耀抽奖

关键代码:
void Widget::funtion_1()
{
    //单抽模式
    int after_pos = 0;
    QLabel* label;
    QLabel*after;
        if(pos > 13)
        {
            pos = 0;
        }
        if(0 == pos){
             after_pos = 13;
        }else
        {
            after_pos = pos - 1;
        }
        after = qobject_cast<QLabel*>(this->children().at(after_pos));
        QString color = QString("background:rgb(%1,%2,%3);")
                .arg(qrand()%256).arg(qrand()%256).arg(qrand()%256);
        after->setStyleSheet("background:none;");
        label = qobject_cast<QLabel*>(this->children().at(pos));
        label->setStyleSheet(color);
        ++pos;
        ++cur_num;
}

void Widget::funtion_2()
{
    //5连抽模式

    //消息循环机制会使设置样式代码不会立即生效,但线程睡眠会立即触发。
    //所以要在设置完样式后再停止线程,要考虑到一个延时问题。
    if(step == randoms[random])
    {
        //timer->stop();
        QThread::msleep(500);
        //timer->start();
        ++random;
        step = 1;
    }else
    {
        ++step;
    }
    int after_pos = 0;
    QLabel* label;
    QLabel*after;
        if(pos > 13)
        {
            pos = 0;
        }
        if(0 == pos){
             after_pos = 13;
        }else
        {
            after_pos = pos - 1;
        }
        after = qobject_cast<QLabel*>(this->children().at(after_pos));
        QString color = QString("background:rgb(%1,%2,%3);")
                .arg(qrand()%256).arg(qrand()%256).arg(qrand()%256);
        after->setStyleSheet("background:none;");
        label = qobject_cast<QLabel*>(this->children().at(pos));
        label->setStyleSheet(color);
        ++pos;
        ++cur_num;


}

void Widget::getAward()
{
    //弹出奖励
    if(1 == fun)
    {
        connect(&ts_1,&award::click,this,&Widget::on_one_clicked);
        ts_1.setImage(random);
        ts_1.show();
    }
    else if(2 == fun)
    {
        connect(&ts_2,&awards::click,this,&Widget::on_five_clicked);
        ts_2.setImage(randoms,5);
        ts_2.show();
    }
}
void Widget::timeOut()
{   //定时器触发格子移动
    if(cur_num == num)
    {
        timer->stop();
        on_off = false;
        if(1 == fun)
        {
            lucky_num += (qrand()%3)+1;
        }else
        {
            lucky_num += (qrand()%8)+3;
        }
        getAward();
        ui->lucky->setText(QString("当前幸运值:%1").arg(lucky_num));
        return;
    }
    if(1 == fun)
    {
        funtion_1();
    }else if(2 == fun)
    {      
        funtion_2();
    }
}
void Widget::on_one_clicked()
{
    //单抽
    if(!on_off)
    {
        on_off = true;
        QLabel* last = qobject_cast<QLabel*>(this->children().at(last_pos));
        last->setStyleSheet("background:none;");
        random = qrand()%14;
        last_pos = random;
        num = random + 28;
        qDebug()<<random;
        pos = 0;
        fun = 1;
        cur_num = -1;
        timer->start();
    }
}
void Widget::on_five_clicked()
{
    //5连抽
    if(!on_off)
    {
        on_off = true;
        QLabel* last = qobject_cast<QLabel*>(this->children().at(last_pos));
        last->setStyleSheet("background:none;");
        num = 0;
        for(int i = 0;i < 5;++i)
        {
            randoms[i] = (qrand()%14)+1;
            num += randoms[i];
            qDebug()<<randoms[i];
        }
        last_pos = num%14;
        random = 0;
        step = -1;
        pos = 0;
        fun = 2;
        cur_num = -1;
        timer->start();
    }
}

效果图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值