day12 coding Qt作业: 猜拳游戏

This commit is contained in:
flykhan 2023-06-28 22:57:03 +08:00
parent f36cc5ce97
commit 554a5f12d0
9 changed files with 686 additions and 0 deletions

View File

@ -0,0 +1,15 @@
#include "mainwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget mainWidget;
mainWidget.setWindowTitle("猜拳");
mainWidget.setWindowIcon(QIcon(":/imgs/rock"));
mainWidget.show();
return a.exec();
}

View File

@ -0,0 +1,147 @@
#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;
}

View File

@ -0,0 +1,52 @@
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include <QIcon>
#include <QPixmap>
#include <QTimer>
#include <QDebug>
#include <cstdlib>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWidget; }
QT_END_NAMESPACE
class MainWidget : public QWidget
{
Q_OBJECT
public:
MainWidget(QWidget *parent = nullptr);
~MainWidget();
private:
Ui::MainWidget *ui;
QPixmap switchImgs(int); // 切换电脑图片函数
private:
int randNum; // 电脑出招映射的随机数 0, 1, 2
int playerInputNum; // 玩家点击的按钮映射的数值 0, 1, 2
QTimer *timer; // 计时器
QPixmap *randImg; // 随机的电脑出招图片
int numsOfGame; // 游戏次数
int currentScore; // 当前得分
public:
// 定义枚举: 0-拳、1-布、2-剪刀
enum{
rock,
paper,
scissors
};
private slots:
void on_startGameBtn_clicked();
void on_rockBtn_clicked();
void on_paperBtn_clicked();
void on_scissorsBtn_clicked();
void on_clearBtn_clicked();
};
#endif // MAINWIDGET_H

View File

@ -0,0 +1,431 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWidget</class>
<widget class="QWidget" name="MainWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWidget</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QWidget" name="widget_3" native="true">
<property name="maximumSize">
<size>
<width>500</width>
<height>500</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 170, 255);</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<spacer name="horizontalSpacer_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<spacer name="horizontalSpacer_11">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<spacer name="horizontalSpacer_13">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="randImgsLable">
<property name="maximumSize">
<size>
<width>500</width>
<height>500</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="text">
<string/>
</property>
<property name="scaledContents">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_14">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<spacer name="horizontalSpacer_12">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<spacer name="horizontalSpacer_10">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="0" column="2">
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_9">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QWidget" name="widget_4" native="true">
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 0);</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="rockBtn">
<property name="maximumSize">
<size>
<width>130</width>
<height>130</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/imgs/rock</normaloff>:/imgs/rock</iconset>
</property>
<property name="iconSize">
<size>
<width>130</width>
<height>130</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="paperBtn">
<property name="maximumSize">
<size>
<width>130</width>
<height>130</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/imgs/paper</normaloff>:/imgs/paper</iconset>
</property>
<property name="iconSize">
<size>
<width>130</width>
<height>130</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="scissorsBtn">
<property name="maximumSize">
<size>
<width>130</width>
<height>130</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/imgs/scissors</normaloff>:/imgs/scissors</iconset>
</property>
<property name="iconSize">
<size>
<width>130</width>
<height>130</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QWidget" name="widget_2" native="true">
<property name="styleSheet">
<string notr="true">background-color: rgb(85, 255, 255);</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="numsOfGameLable">
<property name="styleSheet">
<string notr="true">font: 18pt &quot;黑体&quot;;</string>
</property>
<property name="text">
<string>游戏次数: </string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="currentScoreLable">
<property name="styleSheet">
<string notr="true">font: 18pt &quot;黑体&quot;;</string>
</property>
<property name="text">
<string>当前得分: </string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="startGameBtn">
<property name="styleSheet">
<string notr="true">font: 18pt &quot;黑体&quot;;
color: rgb(255, 0, 0);</string>
</property>
<property name="text">
<string>开始游戏</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="clearBtn">
<property name="styleSheet">
<string notr="true">font: 18pt &quot;黑体&quot;;
color: rgb(255, 0, 0);</string>
</property>
<property name="text">
<string>清零</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_6">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="res.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -0,0 +1,7 @@
<RCC>
<qresource prefix="/imgs">
<file alias="paper">resources/paper.png</file>
<file alias="rock">resources/rock.png</file>
<file alias="scissors">resources/scissors.png</file>
</qresource>
</RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -0,0 +1,34 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwidget.cpp
HEADERS += \
mainwidget.h
FORMS += \
mainwidget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
res.qrc