Qt實(shí)現(xiàn)抽獎(jiǎng)小游戲的三種方式
簡介
本文章是基本Qt與C++實(shí)現(xiàn)一個(gè)抽獎(jiǎng)小游戲,用到的知識(shí)點(diǎn)在此前發(fā)布的幾篇文章。
下面是跳轉(zhuǎn)鏈接:
【Qt控件之QLabel】用法及技巧
鏈接: //www.dbjr.com.cn/program/3003053ai.htm
【Qt控件之QPushButton】用法及技巧
鏈接://www.dbjr.com.cn/program/300318qgu.htm
【Qt控件之QDialog】用法及技巧
鏈接://www.dbjr.com.cn/program/300322vaf.htm
【Qt控件之QMainWindow】用法及技巧
鏈接:http://www.dbjr.com.cn/program/300326afw.htm
【Qt控件之QTimer】用法及技巧
鏈接:http://www.dbjr.com.cn/program/300328eal.htm
實(shí)現(xiàn)方式
實(shí)現(xiàn)方式多種多樣,但畢竟是小程序,需求明確(就沒考慮操作及優(yōu)化),功能簡單,條理清晰,主要提供三種實(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(); // 開始 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)"開始"和"結(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)前線程將被阻塞,無法處理其他事件。這會(huì)導(dǎo)致用戶界面無響應(yīng),給用戶一種程序已經(jīng)卡死的感覺。
直接粘貼相關(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(); // 開始 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)擊"開始",啟動(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)擊"開始",將信號(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)文章
關(guān)于雙向鏈表的增刪改查和排序的C++實(shí)現(xiàn)
下面小編就為大家?guī)硪黄P(guān)于雙向鏈表的增刪改查和排序的C++實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12c語言字符串函數(shù)strstr,strtok,strerror的使用和實(shí)現(xiàn)
C語言中的字符串處理函數(shù)如strtok、strstr和strerror對(duì)于字符串的處理有著重要的作用,strtok函數(shù)用于分割字符串,它通過sep參數(shù)指定的分隔符來分割str參數(shù)指定的字符串,并返回分割后的每個(gè)子字符串2024-10-10C語言數(shù)據(jù)結(jié)構(gòu)之圖的遍歷實(shí)例詳解
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之圖的遍歷實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07C++11/14 線程調(diào)用類對(duì)象和線程傳參的方法
這篇文章主要介紹了C++11/14 線程調(diào)用類對(duì)象和線程傳參的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01