• 打地鼠游戏(5)


    绑定开始、暂停、停止按钮,

    先用之前的绑定方式来完成,对比后面介绍的一种方式。

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 
     6 #include "myscene.h"
     7 
     8 namespace Ui {
     9 class MainWindow;
    10 }
    11 
    12 class MainWindow : public QMainWindow
    13 {
    14     Q_OBJECT
    15 
    16 public:
    17     explicit MainWindow(QWidget *parent = nullptr);
    18     ~MainWindow();
    19 
    20     virtual void closeEvent(QCloseEvent *event);
    21 private slots:
    22     void start_game();  //开始游戏
    23     void hold_game();   //暂停游戏
    24     void end_game();    //停止游戏
    25 
    26 private:
    27     Ui::MainWindow *ui;
    28     MyScene * sc;
    29 
    30 };
    31 
    32 #endif // MAINWINDOW_H
     1 MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
     2 {
     3     ui->setupUi(this);
     4     //将场景在容器中进行显式
     5     this->sc = new MyScene();
     6     ui->graphicsView->setScene(this->sc);
     7 
     8     //控件绑定
     9     connect(this->ui->btn_begin,SIGNAL(clicked()),this,SLOT(start_game()));
    10     connect(this->ui->btn_hold,SIGNAL(clicked()),this,SLOT(hold_game()));
    11     connect(this->ui->btn_end,SIGNAL(clicked()),this,SLOT(end_game()));
    12 
    13 }
     1 void MainWindow::start_game(){
     2     this->sc->get_timer_handler()->start(1000); //开启定时器
     3 }
     4 void MainWindow::hold_game(){
     5     this->sc->get_timer_handler()->stop();
     6 }
     7 void MainWindow::end_game(){
     8     //关闭定时器
     9     this->sc->get_timer_handler()->stop();
    10     //图元恢复默认
    11     for(int i = 0;i<16;i++){
    12         this->sc->get_item(i)->setPic(":/mouse/pic/bg1.png");
    13         this->sc->get_item(i)->setmouse(false);
    14     }
    15     //记分清零
    16 }

    这里在mainwindow类中去引用了this->sc,还要去写get_item(int i)方法来获取item[i],它是一个myscene的类,之所以会这样,是因为在connect的时候,指定的接收信号的对象是this,即mainwindow对象,其实可以一步到位,直接connect的时候,指定myscene对象接收信号:

     1 MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
     2 {
     3     ui->setupUi(this);
     4     //将场景在容器中进行显式
     5     this->sc = new MyScene();
     6     ui->graphicsView->setScene(this->sc);
     7 
     8     //控件绑定
     9     connect(this->ui->btn_begin,SIGNAL(clicked()),this->sc,SLOT(start_game()));
    10     connect(this->ui->btn_hold,SIGNAL(clicked()),this->sc,SLOT(hold_game()));
    11     connect(this->ui->btn_end,SIGNAL(clicked()),this->sc,SLOT(end_game()));
    12 
    13 }
     1 #ifndef MYSCENE_H
     2 #define MYSCENE_H
     3 
     4 #include "myitem.h"
     5 
     6 #include <QObject>
     7 #include <QGraphicsScene>
     8 #include <QTimer>
     9 
    10 class MyScene : public QGraphicsScene
    11 {
    12     Q_OBJECT  /*具备信号与槽能力的关键*/
    13 public:
    14     explicit MyScene(QObject *parent = nullptr);
    15     ~MyScene();
    16 
    17 signals:
    18 
    19 public slots:
    20     void update_my_item();
    21     void start_game();  //开始游戏
    22     void hold_game();   //暂停游戏
    23     void end_game();    //停止游戏
    24 
    25 private:
    26     myitem* item[16]; //当前一个场景由4x4个图元构成,每一个图元都是一张图片构成
    27     QTimer* p_timer;  //定时器
    28 };
    29 
    30 #endif // MYSCENE_H
     1 #include "myscene.h"
     2 
     3 #include <stdlib.h>
     4 #include <time.h>
     5 
     6 MyScene::MyScene(QObject *parent) : QGraphicsScene(parent)
     7 {
     8 
     9     //添加图元到场景
    10     for(int i = 0;i<16;i++){
    11         this->item[i] = new myitem();
    12         //设置坐标
    13         /*一维数组转换为二维数组,下面宽、高的倍数组合
    14         0 0,0 1,0 2,0 3
    15         1 0,1 1,1 2,1 3
    16         2 0,2 1,2 2,2 3
    17         3 0,3 1,3 2,3 3
    18         */
    19         double x = i/4 * this->item[i]->boundingRect().width();
    20         double y = i%4 * this->item[i]->boundingRect().height();
    21         this->item[i]->setPos(x,y);
    22         this->addItem(this->item[i]);
    23     }
    24 
    25     //绑定一个定时器,等定时周期到触发一个随机数[0-15],然后让场景中的一个图元的图片更换
    26     this->p_timer = new QTimer();
    27     connect(this->p_timer, SIGNAL(timeout()), this, SLOT(update_my_item())); //绑定定时器和槽函数
    28     //this->p_timer->start(1000); //由于绑定了开始按钮,在点击开始按钮之后开启定时器
    29 
    30 }
    31 
    32 MyScene::~MyScene(){
    33     for(int i = 0;i<16;i++){
    34         delete this->item[i];
    35     }
    36     delete this->p_timer;
    37 }
    38 
    39 
    40 void MyScene::update_my_item(){
    41     //每次出现老鼠之前,先恢复到所有图元为无老鼠的样式图片
    42     for(int i = 0;i<16;i++){
    43         this->item[i]->setPic(":/mouse/pic/bg1.png");
    44 
    45         //同时需要恢复图元老鼠标识为默认
    46         this->item[i]->setmouse(false);
    47     }
    48 
    49     srand(time(NULL)); //设置随机数种子
    50 
    51     //一次性允许显式最多三个老鼠
    52     int count = rand()%3 +1;
    53     for(int k=1;k<=3;k++){
    54         int i = rand()%16;
    55         this->item[i]->setPic(":/mouse/pic/mouse.png");
    56 
    57         //同时需要设置图元老鼠标识
    58         this->item[i]->setmouse(true);
    59     }
    60 }
    61 
    62 void MyScene::start_game(){
    63     this->p_timer->start(1000); //开启定时器
    64 }
    65 void MyScene::hold_game(){
    66     this->p_timer->stop();
    67 }
    68 void MyScene::end_game(){
    69     //关闭定时器
    70     this->p_timer->stop();
    71     //图元恢复默认
    72     for(int i = 0;i<16;i++){
    73         this->item[i]->setPic(":/mouse/pic/bg1.png");
    74         this->item[i]->setmouse(false);
    75     }
    76     //记分清零
    77 }
    内在的趣味,表面的繁琐
  • 相关阅读:
    变长参数表
    以二进制方式输出数字在内存中的存储形式
    asp.net mvc controller 获取数据
    asp.net mvc 3.0 新特性之 Model
    一个实体对象不能由多个 IEntityChangeTracker 实例引用
    iis6.0运行asp请求资源正在使用中
    view的使用
    asp.net mvc RenderAction 和RenderPartial用法
    今天
    当下的力量
  • 原文地址:https://www.cnblogs.com/data1213/p/10851977.html
Copyright © 2020-2023  润新知