148 lines
4.1 KiB
C++
Executable File
148 lines
4.1 KiB
C++
Executable File
#include "mainwidget.h"
|
|
#include "ui_mainwidget.h"
|
|
|
|
MainWidget::MainWidget(QWidget *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::MainWidget)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
// 电脑初始化时加载的图片
|
|
QPixmap firstImg;
|
|
firstImg.load(":/imgs/scissors");
|
|
ui->randImgsLable->setPixmap(firstImg);
|
|
|
|
// 随机函数定义
|
|
srand(time(NULL)); // 随时间变化的随机种子
|
|
// randNum = rand() % 3; // 在 0-2 之间随机取值
|
|
timer = new QTimer();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
MainWidget::~MainWidget()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
|
|
void MainWidget::on_startGameBtn_clicked()
|
|
{
|
|
numsOfGame++;
|
|
connect(timer,&QTimer::timeout,[&](){
|
|
randNum = rand() % 3; // 在 0-2 之间随机取值
|
|
QPixmap nowImg = switchImgs(randNum); // 根据当前的随机值加载图片
|
|
ui->randImgsLable->setPixmap(nowImg); // 将当前随机值对应的图片设置到主界面
|
|
|
|
// 调试信息
|
|
qDebug() << "numsOfGame: " << numsOfGame <<", randNum: "<<randNum
|
|
<<", playerInputNum: "<<playerInputNum<<endl;
|
|
});
|
|
timer->start(0.05*1000); // 50ms
|
|
|
|
// 更新游戏次数
|
|
ui->numsOfGameLable->setText("游戏次数: " + QString::number(numsOfGame));
|
|
}
|
|
|
|
QPixmap MainWidget::switchImgs(int randNum)
|
|
{
|
|
switch (randNum) {
|
|
case rock:
|
|
randImg = new QPixmap(":/imgs/rock"); // 随机数为 0 时, 加载拳头
|
|
break;
|
|
case paper:
|
|
randImg = new QPixmap(":/imgs/paper"); // 随机数为 1 时, 加载布
|
|
break;
|
|
case scissors:
|
|
randImg = new QPixmap(":/imgs/scissors"); // 随机数为 2 时, 加载剪刀
|
|
break;
|
|
default:
|
|
randImg = new QPixmap(":/imgs/paper"); // 默认加载布
|
|
break;
|
|
}
|
|
|
|
return *randImg;
|
|
}
|
|
|
|
void MainWidget::on_rockBtn_clicked()
|
|
{
|
|
timer->stop(); // 随机数生成的计数器停止
|
|
playerInputNum = rock; // 玩家出拳
|
|
// 电脑不出“布”, 则玩家加 10 分, 否则减 3 分
|
|
if(randNum != paper){
|
|
currentScore+=10;
|
|
} else {
|
|
currentScore-=3;
|
|
}
|
|
|
|
// 调试信息
|
|
qDebug() << "numsOfGame: " << numsOfGame <<", randNum: "<<randNum
|
|
<<", playerInputNum: "<<playerInputNum<<endl;
|
|
|
|
// 更新计分板
|
|
ui->currentScoreLable->setText("当前得分: " + QString::number(currentScore));
|
|
}
|
|
|
|
void MainWidget::on_paperBtn_clicked()
|
|
{
|
|
timer->stop(); // 随机数生成的计数器停止
|
|
playerInputNum = paper; // 玩家出布
|
|
// 电脑不出“剪刀”, 则玩家加 10 分, 否则减 3 分
|
|
if(randNum != scissors){
|
|
currentScore+=10;
|
|
} else {
|
|
currentScore-=3;
|
|
}
|
|
|
|
// 调试信息
|
|
qDebug() << "numsOfGame: " << numsOfGame <<", randNum: "<<randNum
|
|
<<", playerInputNum: "<<playerInputNum<<endl;
|
|
|
|
// 更新计分板
|
|
ui->currentScoreLable->setText("当前得分: " + QString::number(currentScore));
|
|
}
|
|
|
|
void MainWidget::on_scissorsBtn_clicked()
|
|
{
|
|
timer->stop(); // 随机数生成的计数器停止
|
|
playerInputNum = scissors; // 玩家出剪刀
|
|
// 电脑不出“拳”, 则玩家加 10 分, 否则减 3 分
|
|
if(randNum != rock){
|
|
currentScore+=10;
|
|
} else {
|
|
currentScore-=3;
|
|
}
|
|
|
|
// 调试信息
|
|
qDebug() << "numsOfGame: " << numsOfGame <<", randNum: "<<randNum
|
|
<<", playerInputNum: "<<playerInputNum<<endl;
|
|
|
|
// 更新计分板
|
|
ui->currentScoreLable->setText("当前得分: " + QString::number(currentScore));
|
|
}
|
|
|
|
void MainWidget::on_clearBtn_clicked()
|
|
{
|
|
// 统计数据重置为 0
|
|
currentScore = 0;
|
|
numsOfGame = 0;
|
|
|
|
timer->stop(); // 随机数生成的计数器停止
|
|
|
|
// 更新游戏次数
|
|
ui->numsOfGameLable->setText("游戏次数: " + QString::number(numsOfGame));
|
|
// 更新计分板
|
|
ui->currentScoreLable->setText("当前得分: " + QString::number(currentScore));
|
|
|
|
// 调试信息
|
|
qDebug() << "numsOfGame: " << numsOfGame <<", randNum: "<<randNum
|
|
<<", playerInputNum: "<<playerInputNum<<endl;
|
|
}
|