diff --git a/day12/rock-paper-scissors/main.cpp b/day12/rock-paper-scissors/main.cpp new file mode 100755 index 0000000..288fc74 --- /dev/null +++ b/day12/rock-paper-scissors/main.cpp @@ -0,0 +1,15 @@ +#include "mainwidget.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWidget mainWidget; + + mainWidget.setWindowTitle("猜拳"); + mainWidget.setWindowIcon(QIcon(":/imgs/rock")); + + mainWidget.show(); + return a.exec(); +} diff --git a/day12/rock-paper-scissors/mainwidget.cpp b/day12/rock-paper-scissors/mainwidget.cpp new file mode 100755 index 0000000..2608d71 --- /dev/null +++ b/day12/rock-paper-scissors/mainwidget.cpp @@ -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: "<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: "<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: "<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: "<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: "< +#include +#include +#include +#include +#include + +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 diff --git a/day12/rock-paper-scissors/mainwidget.ui b/day12/rock-paper-scissors/mainwidget.ui new file mode 100755 index 0000000..1bd663e --- /dev/null +++ b/day12/rock-paper-scissors/mainwidget.ui @@ -0,0 +1,431 @@ + + + MainWidget + + + + 0 + 0 + 800 + 600 + + + + MainWidget + + + 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)); + + + + + + + + + + 500 + 500 + + + + background-color: rgb(0, 170, 255); + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 500 + 500 + + + + + 0 + 0 + + + + false + + + + + + false + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Qt::Vertical + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + background-color: rgb(255, 85, 0); + + + + + + + 130 + 130 + + + + + + + + :/imgs/rock:/imgs/rock + + + + 130 + 130 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + 130 + 130 + + + + + + + + :/imgs/paper:/imgs/paper + + + + 130 + 130 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + 130 + 130 + + + + + + + + :/imgs/scissors:/imgs/scissors + + + + 130 + 130 + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + Qt::Horizontal + + + + + + + background-color: rgb(85, 255, 255); + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + font: 18pt "黑体"; + + + 游戏次数: + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + font: 18pt "黑体"; + + + 当前得分: + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + font: 18pt "黑体"; +color: rgb(255, 0, 0); + + + 开始游戏 + + + + + + + font: 18pt "黑体"; +color: rgb(255, 0, 0); + + + 清零 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + + diff --git a/day12/rock-paper-scissors/res.qrc b/day12/rock-paper-scissors/res.qrc new file mode 100755 index 0000000..7f99cd6 --- /dev/null +++ b/day12/rock-paper-scissors/res.qrc @@ -0,0 +1,7 @@ + + + resources/paper.png + resources/rock.png + resources/scissors.png + + diff --git a/day12/rock-paper-scissors/resources/paper.png b/day12/rock-paper-scissors/resources/paper.png new file mode 100755 index 0000000..629434f Binary files /dev/null and b/day12/rock-paper-scissors/resources/paper.png differ diff --git a/day12/rock-paper-scissors/resources/rock.png b/day12/rock-paper-scissors/resources/rock.png new file mode 100755 index 0000000..8473dff Binary files /dev/null and b/day12/rock-paper-scissors/resources/rock.png differ diff --git a/day12/rock-paper-scissors/resources/scissors.png b/day12/rock-paper-scissors/resources/scissors.png new file mode 100755 index 0000000..6906290 Binary files /dev/null and b/day12/rock-paper-scissors/resources/scissors.png differ diff --git a/day12/rock-paper-scissors/rock-paper-scissors.pro b/day12/rock-paper-scissors/rock-paper-scissors.pro new file mode 100755 index 0000000..cd568da --- /dev/null +++ b/day12/rock-paper-scissors/rock-paper-scissors.pro @@ -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