需求:在主页面选择游戏时间,开始游戏,系统生成随机数,用户开始猜数,超时或猜数成功都会出现对话框以及相应动画。
现在ui设计器对页面进行设计
动画的实现都是在栈容器的对应页面上放置一个标签
mywidget.h
#include "mywidget.h" #include "ui_mywidget.h" #include<QComboBox>//数字下拉框 #include<QDebug> #include<QMessageBox> #include<QTime> MyWidget::MyWidget(QWidget *parent) : QWidget(parent) , ui(new Ui::MyWidget) { ui->setupUi(this); //程序开始页 //ui->stackedWidget->setCurrentIndex(0); ui->stackedWidget->setCurrentWidget(ui->page_set); ui->label->setText("<center><h1>猜数字游戏</center></h1>"); ui->label->setFont(QFont("楷体",20,QFont::Bold)); ui->label_2->setText("<center>请设置猜数字时间(秒)</center>"); ui->pushButtonStart->setText("进入游戏"); ui->pushButtonEnd->setText("退出游戏"); //编辑下拉框,直接ui界面添加 //动画设置 //成功动画 winMovie.setFileName("://image/win.gif"); ui->label_win->setMovie(&winMovie); ui->label_win->setScaledContents(true); //失败动画 overMovie.setFileName("://image/over.gif"); ui->label_over->setMovie(&overMovie); ui->label_over->setScaledContents(true); //处理按键 connect(ui->pushButton0,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton1,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton2,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton3,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton4,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton5,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton6,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton7,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton8,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton9,&QPushButton::clicked,this,&MyWidget::DealNum); } MyWidget::~MyWidget() { delete ui; } void MyWidget::on_pushButtonStart_clicked() { ui->textEdit->clear(); resultStr.clear(); //获取游戏时间 gameTime=ui->comboBox->currentText().toInt(); qDebug()<<gameTime<<'s'; //切换游戏界面 //ui->stackedWidget->setCurrentIndex(1); ui->stackedWidget->setCurrentWidget(ui->page_game); ui->textEdit->setFont(QFont("楷体",45)); //生成随机数 int num; qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); while((num=qrand()%10000)<999); randStr=QString::number(num);//获得随机数 qDebug()<<num; //设置进度条 ui->progressBar->setMinimum(0); ui->progressBar->setMaximum(gameTime); ui->progressBar->setValue(gameTime); gameTimerId=this->startTimer(1000);//每隔1s触发定时器 } void MyWidget::DealNum() { //获得信号接收者 QObject *mySender=sender(); QPushButton *p=(QPushButton *)mySender; if(p) { //获取按钮的内容 resultStr+=p->text(); if(resultStr.size()==1&&resultStr=='0') { resultStr.clear(); } if(resultStr.size()<=4) { //显示数字 ui->textEdit->setText(resultStr); if(resultStr.size()==4) { if(resultStr<randStr) { ui->textEdit->append("数字小啦!!!"); } else if(resultStr>randStr) { ui->textEdit->append("数字大啦!!!"); } else { ui->textEdit->append("恭喜你猜对啦!!!"); //关掉定时器 killTimer(gameTimerId); //显示对话框 QMessageBox::information(this,"胜利","恭喜你猜对啦!"); //切换到成功动画界面 ui->stackedWidget->setCurrentWidget(ui->page_win); winMovie.start(); winTimerId=this->startTimer(5000); } resultStr.clear(); } } } } void MyWidget::timerEvent(QTimerEvent *e) { if(e->timerId()==this->gameTimerId) { //设置进度条时间 gameTime--; ui->progressBar->setValue(gameTime); //计时结束,弹出对话框 if(0==gameTime) { killTimer(gameTimerId); QMessageBox::warning(this,"错误","时间到啦!"); //启动失败动画 ui->stackedWidget->setCurrentWidget(ui->page_over); overMovie.start(); //设置动画时间 overTimerId=this->startTimer(5000); } } if(e->timerId()==this->winTimerId) { //停止定时器 ,停止动画,返回到开始页面 killTimer(winTimerId); winMovie.stop(); ui->stackedWidget->setCurrentWidget(ui->page_set); } if(e->timerId()==overTimerId) { //停止定时器 ,停止动画,返回到开始页面 killTimer(overTimerId); overMovie.stop(); ui->stackedWidget->setCurrentWidget(ui->page_set); } } void MyWidget::on_pushButtonEnd_clicked()//ui设计器的相应按键,右击,转到槽 { this->close(); } void MyWidget::on_pushButtonDel_clicked() { if(resultStr==1) { resultStr.clear(); ui->textEdit->clear(); } else { resultStr.chop(1); ui->textEdit->setText(resultStr); } } void MyWidget::on_pushButtonTip_clicked() { resultStr.clear(); QString str="随机数:"+randStr; ui->textEdit->setText(str); }
mywidget.cpp
#include "mywidget.h" #include "ui_mywidget.h" #include<QComboBox>//数字下拉框 #include<QDebug> #include<QMessageBox> #include<QTime> MyWidget::MyWidget(QWidget *parent) : QWidget(parent) , ui(new Ui::MyWidget) { ui->setupUi(this); //程序开始页 //ui->stackedWidget->setCurrentIndex(0); ui->stackedWidget->setCurrentWidget(ui->page_set); ui->label->setText("<center><h1>猜数字游戏</center></h1>"); ui->label->setFont(QFont("楷体",20,QFont::Bold)); ui->label_2->setText("<center>请设置猜数字时间(秒)</center>"); ui->pushButtonStart->setText("进入游戏"); ui->pushButtonEnd->setText("退出游戏"); //编辑下拉框,直接ui界面添加 //动画设置 //成功动画 winMovie.setFileName("://image/win.gif"); ui->label_win->setMovie(&winMovie); ui->label_win->setScaledContents(true); //失败动画 overMovie.setFileName("://image/over.gif"); ui->label_over->setMovie(&overMovie); ui->label_over->setScaledContents(true); //处理按键 connect(ui->pushButton0,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton1,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton2,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton3,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton4,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton5,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton6,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton7,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton8,&QPushButton::clicked,this,&MyWidget::DealNum); connect(ui->pushButton9,&QPushButton::clicked,this,&MyWidget::DealNum); } MyWidget::~MyWidget() { delete ui; } void MyWidget::on_pushButtonStart_clicked() { ui->textEdit->clear(); resultStr.clear(); //获取游戏时间 gameTime=ui->comboBox->currentText().toInt(); qDebug()<<gameTime<<'s'; //切换游戏界面 //ui->stackedWidget->setCurrentIndex(1); ui->stackedWidget->setCurrentWidget(ui->page_game); ui->textEdit->setFont(QFont("楷体",45)); //生成随机数 int num; qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); while((num=qrand()%10000)<999); randStr=QString::number(num);//获得随机数 qDebug()<<num; //设置进度条 ui->progressBar->setMinimum(0); ui->progressBar->setMaximum(gameTime); ui->progressBar->setValue(gameTime); gameTimerId=this->startTimer(1000);//每隔1s触发定时器 } void MyWidget::DealNum() { //获得信号接收者 QObject *mySender=sender(); QPushButton *p=(QPushButton *)mySender; if(p) { //获取按钮的内容 resultStr+=p->text(); if(resultStr.size()==1&&resultStr=='0') { resultStr.clear(); } if(resultStr.size()<=4) { //显示数字 ui->textEdit->setText(resultStr); if(resultStr.size()==4) { if(resultStr<randStr) { ui->textEdit->append("数字小啦!!!"); } else if(resultStr>randStr) { ui->textEdit->append("数字大啦!!!"); } else { ui->textEdit->append("恭喜你猜对啦!!!"); //关掉定时器 killTimer(gameTimerId); //显示对话框 QMessageBox::information(this,"胜利","恭喜你猜对啦!"); //切换到成功动画界面 ui->stackedWidget->setCurrentWidget(ui->page_win); winMovie.start(); winTimerId=this->startTimer(5000); } resultStr.clear(); } } } } void MyWidget::timerEvent(QTimerEvent *e) { if(e->timerId()==this->gameTimerId) { //设置进度条时间 gameTime--; ui->progressBar->setValue(gameTime); //计时结束,弹出对话框 if(0==gameTime) { killTimer(gameTimerId); QMessageBox::warning(this,"错误","时间到啦!"); //启动失败动画 ui->stackedWidget->setCurrentWidget(ui->page_over); overMovie.start(); //设置动画时间 overTimerId=this->startTimer(5000); } } if(e->timerId()==this->winTimerId) { //停止定时器 ,停止动画,返回到开始页面 killTimer(winTimerId); winMovie.stop(); ui->stackedWidget->setCurrentWidget(ui->page_set); } if(e->timerId()==overTimerId) { //停止定时器 ,停止动画,返回到开始页面 killTimer(overTimerId); overMovie.stop(); ui->stackedWidget->setCurrentWidget(ui->page_set); } } void MyWidget::on_pushButtonEnd_clicked() { this->close(); } void MyWidget::on_pushButtonDel_clicked() { if(resultStr==1) { resultStr.clear(); ui->textEdit->clear(); } else { //resultStr[resultStr.size()-1]='\0'; resultStr.chop(1); ui->textEdit->setText(resultStr); } } void MyWidget::on_pushButtonTip_clicked() { resultStr.clear(); QString str="随机数:"+randStr; ui->textEdit->setText(str); }
Qt设置应用程序图标和可执行程序图标🔗:
https://www.cnblogs.com/nanqiang/p/11023535.html
来源:https://www.cnblogs.com/lalala-lueluelue/p/12617643.html