Qt實(shí)現(xiàn)抽獎(jiǎng)小游戲的三種方式
簡(jiǎn)介
本文章是基本Qt與C++實(shí)現(xiàn)一個(gè)抽獎(jiǎng)小游戲,用到的知識(shí)點(diǎn)在此前發(fā)布的幾篇文章。
下面是跳轉(zhuǎn)鏈接:
【Qt控件之QLabel】用法及技巧鏈接: //www.fzitv.net/program/3003053ai.htm
【Qt控件之QPushButton】用法及技巧 鏈接://www.fzitv.net/program/300318qgu.htm
【Qt控件之QDialog】用法及技巧 鏈接://www.fzitv.net/program/300322vaf.htm
【Qt控件之QMainWindow】用法及技巧 鏈接:http://www.fzitv.net/program/300326afw.htm
【Qt控件之QTimer】用法及技巧 鏈接:http://www.fzitv.net/program/300328eal.htm
實(shí)現(xiàn)方式
實(shí)現(xiàn)方式多種多樣,但畢竟是小程序,需求明確(就沒(méi)考慮操作及優(yōu)化),功能簡(jiǎn)單,條理清晰,主要提供三種實(shí)現(xiàn)方式(此階段未實(shí)現(xiàn)概率設(shè)置,之后再發(fā)布概率設(shè)置版本吧):
1. 基于while循環(huán)
示例:
先粘貼UI

.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_FORWARD_DECLARE_CLASS(C_DlgSetting)
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
// 退出
void slot_actQuit_triggered();
// 設(shè)置概率
void slot_actSetting_triggered();
// 開(kāi)始
void slot_btnStart_clicked();
// 停止
void slot_btnStop__clicked();
private:
Ui::MainWindow *ui;
C_DlgSetting* m_pDlgSetting; // 概率設(shè)置類
bool m_bFlag = false;// 標(biāo)志
};
#endif // MAINWINDOW_H.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "DlgSetting.h"
#include <QTime>
#include <QThread>
#include <QCoreApplication>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// m_pDlgSetting = new C_DlgSetting(this);
// 信號(hào)和槽
{
connect(ui->action_quit, &QAction::triggered, this, &MainWindow::slot_actQuit_triggered);
connect(ui->action_setting, &QAction::triggered, this, &MainWindow::slot_actSetting_triggered);
connect(ui->btn_start, &QPushButton::clicked, this, &MainWindow::slot_btnStart_clicked);
connect(ui->btn_stop, &QPushButton::clicked, this, &MainWindow::slot_btnStop__clicked);
}
// 聲明隨機(jī)數(shù)種子,不然就是偽隨機(jī)(每次產(chǎn)生的隨機(jī)數(shù)都一樣)
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slot_actQuit_triggered()
{
close();
}
void MainWindow::slot_actSetting_triggered()
{
// m_pDlgSetting->exec();
}
void MainWindow::slot_btnStart_clicked()
{
if(m_bFlag)
{
return;
}
QStringList sl;
sl << "一等獎(jiǎng)" << "二等獎(jiǎng)" << "三等獎(jiǎng)" << "四等獎(jiǎng)" << "五等獎(jiǎng)";
m_bFlag = true;
while (m_bFlag) {
int nRange = qrand() % 5;
ui->label_turn->setText(sl.at(nRange));
// 100ms轉(zhuǎn)一次
QThread::msleep(100);
// 防止界面卡死
QCoreApplication::processEvents();
}
}
void MainWindow::slot_btnStop__clicked()
{
m_bFlag = false;
// 顯示最終獲獎(jiǎng)結(jié)果
QString strRes = QString("最終結(jié)果: %1").arg(ui->label_turn->text());
ui->label_res->setText(strRes);
}.main
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}結(jié)果

- 實(shí)現(xiàn)思路
– 設(shè)置UI,注意命名
– 進(jìn)行信號(hào)和槽連接
– 實(shí)現(xiàn)"開(kāi)始"和"結(jié)束"功能
– 顯示結(jié)果
2. 基于定時(shí)器
- 示例
UI顯示與1.是一樣的,需借助QTimer實(shí)現(xiàn)
QTimer 是 Qt 框架中的一個(gè)類,用于在特定的時(shí)間間隔后發(fā)出一個(gè)信號(hào)。它是 Qt
的事件循環(huán)系統(tǒng)的一部分,該系統(tǒng)允許程序在等待某些事件(如用戶輸入或定時(shí)器超時(shí))時(shí)保持響應(yīng)。QTimer 的工作原理是將定時(shí)器的超時(shí)作為一個(gè)事件添加到 Qt
的事件隊(duì)列中。當(dāng)事件循環(huán)檢測(cè)到定時(shí)器超時(shí)時(shí),它就會(huì)發(fā)出預(yù)定的信號(hào)。這種機(jī)制允許 QTimer
在等待定時(shí)器超時(shí)時(shí)不會(huì)阻塞用戶界面,因?yàn)槭录h(huán)可以繼續(xù)處理其他事件,如用戶輸入或繪制事件。相比之下,如果使用標(biāo)準(zhǔn)的 C++ 定時(shí)器,如
std::this_thread::sleep_for,在等待定時(shí)器超時(shí)時(shí),當(dāng)前線程將被阻塞,無(wú)法處理其他事件。這會(huì)導(dǎo)致用戶界面無(wú)響應(yīng),給用戶一種程序已經(jīng)卡死的感覺(jué)。
直接粘貼相關(guān)代碼:.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
// 退出
void slot_actQuit_triggered();
// 開(kāi)始
void slot_btnStart_clicked();
// 停止
void slot_btnStop__clicked();
// 定時(shí)器處理
void slot_timeout();
private:
Ui::MainWindow *ui;
bool m_bFlag = false;// 標(biāo)志
QTimer* m_pTimer;// 定時(shí)器
};
#endif // MAINWINDOW_H.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
#include <QThread>
#include <QCoreApplication>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_pTimer = new QTimer(this);
// 處理
connect(m_pTimer, &QTimer::timeout, this, &MainWindow::slot_timeout);
// 信號(hào)和槽
{
connect(ui->action_quit, &QAction::triggered, this, &MainWindow::slot_actQuit_triggered);
connect(ui->btn_start, &QPushButton::clicked, this, &MainWindow::slot_btnStart_clicked);
connect(ui->btn_stop, &QPushButton::clicked, this, &MainWindow::slot_btnStop__clicked);
}
// 聲明隨機(jī)數(shù)種子,不然就是偽隨機(jī)(每次產(chǎn)生的隨機(jī)數(shù)都一樣)
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slot_actQuit_triggered()
{
close();
}
void MainWindow::slot_btnStart_clicked()
{
// 此處可先判斷定時(shí)器是否處于活動(dòng)狀態(tài),如果是,則返回;否則,再啟動(dòng)
// ToDoSomething
{
}
m_pTimer->start(100);
}
void MainWindow::slot_btnStop__clicked()
{
m_pTimer->stop();
// 顯示最終獲獎(jiǎng)結(jié)果
QString strRes = QString("最終結(jié)果: %1").arg(ui->label_turn->text());
ui->label_res->setText(strRes);
}
void MainWindow::slot_timeout()
{
QStringList sl;
sl << "一等獎(jiǎng)" << "二等獎(jiǎng)" << "三等獎(jiǎng)" << "四等獎(jiǎng)" << "五等獎(jiǎng)";
int nRange = qrand() % 5;
ui->label_turn->setText(sl.at(nRange));
}- 實(shí)現(xiàn)思路
– 點(diǎn)擊"開(kāi)始",啟動(dòng)定時(shí)器
– “定時(shí)器"實(shí)現(xiàn)界面刷新
– 點(diǎn)擊"結(jié)束”,停止定時(shí)器,并將結(jié)果顯示
3. 基于線程
- 實(shí)現(xiàn)思路(等之后發(fā)布線程文章后,實(shí)現(xiàn))
– 在主窗口創(chuàng)建一個(gè)線程對(duì)象
– 點(diǎn)擊"開(kāi)始",將信號(hào)發(fā)送到線程中,用于更新幾等獎(jiǎng)
– 線程將更新后的信息發(fā)送到主窗口
– 主窗口動(dòng)態(tài)顯示
– 點(diǎn)擊"結(jié)束",停止線程,顯示結(jié)果
到此這篇關(guān)于Qt實(shí)現(xiàn)抽獎(jiǎng)小游戲的三種方式的文章就介紹到這了,更多相關(guān)Qt 抽獎(jiǎng)游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++數(shù)據(jù)結(jié)構(gòu)之鏈表的創(chuàng)建
這篇文章主要介紹了C++數(shù)據(jù)結(jié)構(gòu)之鏈表的創(chuàng)建的相關(guān)資料,希望通過(guò)本文幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
C/C++ 中堆和棧及靜態(tài)數(shù)據(jù)區(qū)詳解
這篇文章主要介紹了C/C++ 中堆和棧及靜態(tài)數(shù)據(jù)區(qū)詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
C語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng)的源碼分享
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
C語(yǔ)言順序表實(shí)現(xiàn)代碼排錯(cuò)
這篇文章主要介紹了C語(yǔ)言順序表實(shí)現(xiàn)方法,大家參考使用吧2013-12-12
C++實(shí)現(xiàn)十進(jìn)制數(shù)轉(zhuǎn)為其它進(jìn)制數(shù)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)十進(jìn)制數(shù)轉(zhuǎn)為其它進(jìn)制數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C++基于EasyX框架實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
EasyX是針對(duì)C/C++的圖形庫(kù),可以幫助使用C/C++語(yǔ)言的程序員快速上手圖形和游戲編程。本文將利用EasyX框架實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲,需要的可以參考一下2023-01-01
C語(yǔ)言實(shí)現(xiàn)BMP轉(zhuǎn)換JPG的方法
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)BMP轉(zhuǎn)換JPG的方法,涉及C#圖片格式轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之中綴樹轉(zhuǎn)后綴樹的實(shí)例
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之中綴樹轉(zhuǎn)后綴樹的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-08-08

