DS

C++大作业代码(4) 打地鼠、坦克大战

C++大作业代码(4) 打地鼠、坦克大战

Posted by xuepro on June 14, 2019
//------------mainwindow.h-------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QApplication>
#include <QWidget>
#include <QPaintEvent>
#include <QTimer>
#include <QDebug>
#include <QPainter>
#include <QMouseEvent>
#include <QPainterPath>
#include <QPainter>
#include <QPen>
#include <QColor>
#include <QBrush>
#include <QMessageBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <Qpalette>
#include <QtDebug>
#include <QPushButton>
#include <QLabel>
#include <QMainWindow>
#include <bits/stdc++.h>
using namespace std;
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void getenemy();
    //void print();
    void tankAI();
    void HideCursor();
    void mainfunction();
    //void paint();
    void keyPressEvent(QKeyEvent  *event);
    void paintEvent(QPaintEvent *event);
    //void Sleep();

private slots:
    void on_pushButton_3_clicked();
    //void on_radioButton_2_clicked();
    //void on_radioButton_clicked();
    //void on_pushButton_2_clicked();

//    void on_radioButton_3_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
    QButtonGroup *SelectModeGroup;
};

#endif // MAINWINDOW_H
//--------------mainwindow.cpp---------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QWidget>
#include <QPaintEvent>
#include <QTime>
#include <QDebug>
#include <QPainter>
#include <QMouseEvent>
#include <QPainterPath>
#include <QPainter>
#include <QPen>
#include <QColor>
#include <QBrush>
#include <QMessageBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <Qpalette>
#include <QtDebug>
#include <QPushButton>
#include <QLabel>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QCheckBox>
#include <QComboBox>
#include <QTextEdit>
#include <QTextBrowser>
#include <QGroupBox>//模拟方式显示数值
#include <QSpinBox>//数字
#include <QDateEdit>
#include <QTimeEdit>
#include <QDateTimeEdit>
#include <QTabWidget>//不介绍不算控件
#include <QCompleter>
#include <QHBoxLayout>
#include <QDebug>
#include <QPixmap>
#include <QString>
#include <QLCDNumber>
#include <QTabWidget>
#include <QPainter>
#include <QBrush>
#include <QPicture>
#include <QTime>
#include <QTimer>
#include <windows.h>
#include <bits/stdc++.h>
using namespace std;



struct tank{
    int x;
    int y;
    int direction;
};
char ch;
int flag=1;
tank player,enemy;
int xx[5]={0,1,0,-1},yy[5]={1,0,-1,0};
int bullets[50][50];
int issleep=0;
int conserve[50][50]{
{4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4},
{4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1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,1,1,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,4},
{4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,4},
{4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}
};
int mp[50][50];
bool vis[50][50];
int pre[50][50];
int score=-2;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("面向对象");
    setMinimumSize(1000,850);
    setMaximumSize(1000,850);

}


/*void Sleep(unsigned int msec)
{
    QTime reachTime=QTime::currentTime().addSecs(msec);
    while(QTime::currentTime()<reachTime)
        QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}*/

void sleep(unsigned int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

void MainWindow::paintEvent(QPaintEvent*){
    if(score>=0)
        ui->label_2->setText(QString::fromStdString(to_string(score)));
    else
        ui->label_2->setText(QString::fromStdString(to_string(0)));
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing, true);



    QBrush brush;
    brush.setColor(QColor(255, 255, 255));
    brush.setStyle(Qt::SolidPattern);
    p.setBrush(brush);



    QPen pen=p.pen();
    p.setPen(pen);
    p.drawRect(10, 10, 820, 820 );


    //网格线
    pen.setColor(Qt::black);
    pen.setWidth(1);
    p.setPen(pen);

    for(int i=0;i<=41;i++){
        p.drawLine(QPoint(10+i*20,10),QPoint(10+i*20,830));
        p.drawLine(QPoint(10,10+i*20),QPoint(830,10+i*20));
    }

    for(int i=0;i<=40;i++){
        for(int j=0;j<=40;j++){
            if(bullets[i][j]!=0){
                if(mp[i][j]==4){
                    bullets[i][j]=0;
                    QPainter painter(this);
                    painter.fillRect(QRect(11+j*20,11+20*i,18,18), QBrush(Qt::darkGray));
                }
                else if(mp[i][j]==1){
                    QPainter painter(this);
                    bullets[i][j]=0;
                    mp[i][j]=0;
                    painter.fillRect(QRect(11+j*20,11+20*i,18,18), QBrush(Qt::white));
                }
                else if(i==enemy.x&&j==enemy.y){
                    getenemy();
                    score++;
                }
                else if(i==player.x&&j==player.y){
                    flag=0;//被击中了
                }
                else{
                    QPainter painter(this);
                    brush.setColor(Qt::yellow);
                    p.setPen(Qt::NoPen);
                    p.setBrush(brush);
                    p.setPen(pen);
                    p.drawEllipse(QPoint(20+20*j,20+20*i),5,5);
                }
            }
            else if(i==player.x&&j==player.y&&player.direction==1){
                QPainter painter(this);
                QPixmap pix;
                pix.load("D:/EXE/test/mianxiangduixiang/player1.png");
                painter.drawPixmap(j*20+10,i*20+10,20,20,pix);
            }
            else if(i==player.x&&j==player.y&&player.direction==2){
                QPainter painter(this);
                QPixmap pix;
                pix.load("D:/EXE/test/mianxiangduixiang/player2.png");
                painter.drawPixmap(j*20+10,i*20+10,20,20,pix);
            }
            else if(i==player.x&&j==player.y&&player.direction==3){
                QPainter painter(this);
                QPixmap pix;
                pix.load("D:/EXE/test/mianxiangduixiang/player3.png");
                painter.drawPixmap(j*20+10,i*20+10,20,20,pix);
            }
            else if(i==player.x&&j==player.y&&player.direction==4){
                QPainter painter(this);
                QPixmap pix;
                pix.load("D:/EXE/test/mianxiangduixiang/player4.png");
                painter.drawPixmap(j*20+10,i*20+10,20,20,pix);
            }
            else if(i==enemy.x&&j==enemy.y&&enemy.direction==1){
                QPainter painter(this);
                QPixmap pix;
                pix.load("D:/EXE/test/mianxiangduixiang/enemy1.png");
                painter.drawPixmap(j*20+10,i*20+10,20,20,pix);
            }
            else if(i==enemy.x&&j==enemy.y&&enemy.direction==2){
                QPainter painter(this);
                QPixmap pix;
                pix.load("D:/EXE/test/mianxiangduixiang/enemy2.png");
                painter.drawPixmap(j*20+10,i*20+10,20,20,pix);
            }
            else if(i==enemy.x&&j==enemy.y&&enemy.direction==3){
                QPainter painter(this);
                QPixmap pix;
                pix.load("D:/EXE/test/mianxiangduixiang/enemy3.png");
                painter.drawPixmap(j*20+10,i*20+10,20,20,pix);
            }
            else if(i==enemy.x&&j==enemy.y&&enemy.direction==4){
                QPainter painter(this);
                QPixmap pix;
                pix.load("D:/EXE/test/mianxiangduixiang/enemy4.png");
                painter.drawPixmap(j*20+10,i*20+10,20,20,pix);
            }


            else if(mp[i][j]==4){
                QPainter painter(this);
                painter.fillRect(QRect(11+j*20,11+20*i,18,18), QBrush(Qt::darkGray));
            }
            else if(mp[i][j]==0){
                QPainter painter(this);
                painter.fillRect(QRect(11+j*20,11+20*i,18,18), QBrush(Qt::white));
            }
            else if(mp[i][j]==1){
                QPainter painter(this);
                painter.fillRect(QRect(11+j*20,11+20*i,18,18), QBrush(Qt::red));
            }
        }
    }

    for(int i=0;i<=40;i++){
        for(int j=0;j<=40;j++){
                /*if(bullets[i][j]!=0&&mp[i][j]==4)
                    continue;
                else if(bullets[i][j]!=0&&mp[i][j]!=0){
                    bullets[i][j]=0;
                    mp[i][j]=0;
                }*/
            if(bullets[i][j]&(1<<1)){
                if(i-1>=1&&i-1<=39){
                    bullets[i-1][j]|=(1<<1)*64;
                }
            }
            if(bullets[i][j]&(1<<2)){
                if(i+1>=1&&i+1<=39)
                    bullets[i+1][j]|=(1<<2)*64;
            }
            if(bullets[i][j]&(1<<3)){
                if(j-1>=1&&j-1<=39)
                    bullets[i][j-1]|=(1<<3)*64;
            }
            if(bullets[i][j]&(1<<4)){
                if(j+1>=1&&j+1<=39)
                    bullets[i][j+1]|=(1<<4)*64;
            }
        }
    }
    for(int i=0;i<=40;i++){
        for(int j=0;j<=40;j++){
            bullets[i][j]/=64;
        }
    }

}

void MainWindow::getenemy(){
    score++;
    while(1){
        enemy.x=rand()%37+2;
        enemy.y=rand()%37+2;
        enemy.direction=rand()%4+1;
        if(mp[enemy.x][enemy.y]!=0)
            continue;
        if(abs(enemy.x-player.x)<=5)
            continue;
        if(abs(enemy.y-player.y)<=5)
            continue;

        break;
    }
}


void MainWindow::tankAI(){
    memset(vis,0,sizeof(vis));
    memset(pre,0,sizeof(pre));
    queue<pair<int,int> >q;
    //int ans=0;
    while(!q.empty())
        q.pop();
    q.push(pair<int,int>(player.x,player.y));
    vis[player.x][player.y]=1;
    while(!q.empty()){
        pair<int,int> temp=q.front();
        q.pop();

        pair<int,int> nex;
        for(int i=0;i<4;i++){
            nex.first=temp.first+xx[i];
            nex.second=temp.second+yy[i];
            if(nex.first<=0||nex.first>=40||nex.second<=0||nex.second>=40)
                continue;
            if(vis[nex.first][nex.second])
                continue;
            if(mp[nex.first][nex.second]!=0)
                continue;

            vis[nex.first][nex.second]=1;
            pre[nex.first][nex.second]=temp.first*100+temp.second;
            q.push(pair<int,int>(nex.first,nex.second));
        }
    }
    pair<int,int> temp;
    int ran=rand()%3;//随机数可通过改变随机数范围来控制难度,以控制敌方坦克是否直接向玩家走来
        if(ran==0){
            temp.first=pre[enemy.x][enemy.y]/100;
            temp.second=pre[enemy.x][enemy.y]%100;



            if(enemy.x<temp.first)
                enemy.direction=2;
            else if(enemy.x>temp.first)
                enemy.direction=1;
            else if(enemy.y>temp.second)
                enemy.direction=3;
            else
                enemy.direction=4;
            enemy.x=temp.first;
            enemy.y=temp.second;
        }
        else{
            ran=rand()%4+1;
            if(ran==1&&(enemy.x-1)>1&&mp[enemy.x-1][enemy.y]==00){
                if(enemy.direction==1)
                    enemy.x--;
                else
                    enemy.direction==1;
            }
            else if(ran==2&&(enemy.x+1)<39&&mp[enemy.x+1][enemy.y]==0){
                if(enemy.direction==2)
                    enemy.x++;
                else
                    enemy.direction==2;
            }
            else if(ran==3&&(enemy.y-1)>1&&mp[enemy.x][enemy.y-1]==0){
                if(enemy.direction==3)
                    enemy.y--;
                else
                    enemy.direction==3;
            }
            else if(ran==4&&(enemy.y+1)<39&&mp[enemy.x][enemy.y+1]==0){
                if(enemy.direction==4)
                    enemy.y++;
                else
                    enemy.direction==4;
            }
        }
        //vv.push_back(pair<int,int>(enemy.x,enemy.y));
        if(abs(enemy.x-player.x)<=1&&enemy.y>player.y&&ran==0){
            if(enemy.direction!=3)
                enemy.direction=3;
            else{
                bullets[enemy.x][enemy.y-1]|=(1<<3);
            }
        }
        else if(abs(enemy.x-player.x)<=1&&enemy.y<player.y&&ran==0){
            if(enemy.direction!=4)
                enemy.direction=4;
            else{
                bullets[enemy.x][enemy.y+1]|=(1<<4);
            }
        }
        else if(abs(enemy.y-player.y)<=1&&enemy.x>player.x&&ran==0){
            if(enemy.direction!=1)
                enemy.direction=1;
            else{
                bullets[enemy.x-1][enemy.y]|=(1<<1);
            }
        }
    else if(abs(enemy.y-player.y)<=1&&enemy.x<player.x&&ran==0){
        if(enemy.direction!=2)
            enemy.direction=2;
        else{
            bullets[enemy.x+1][enemy.y]|=(1<<2);
        }
    }
    /*temp.first=pre[enemy.x][enemy.y]/100;
    temp.second=pre[enemy.x][enemy.y]%100;
    if(enemy.x<temp.first)
        enemy.direction=2;
    else if(enemy.x>temp.first)
        enemy.direction=1;
    else if(enemy.y>temp.second)
        enemy.direction=3;
    else
        enemy.direction=4;
    enemy.x=temp.first;
    enemy.y=temp.second;
    if(abs(enemy.x-player.x)<=1&&enemy.y>player.y){
        if(enemy.direction!=3)
            enemy.direction=3;
        else{
            bullets[enemy.x][enemy.y-2]=3;
        }
    }
    else if(abs(enemy.x-player.x)<=1&&enemy.y<player.y){
        if(enemy.direction!=4)
            enemy.direction=4;
        else{
            bullets[enemy.x][enemy.y+2]=4;
        }
    }
    else if(abs(enemy.y-player.y)<=1&&enemy.x>player.x){
        if(enemy.direction!=1)
            enemy.direction=1;
        else{
            bullets[enemy.x-2][enemy.y]=1;
        }
    }
    else if(abs(enemy.y-player.y)<=1&&enemy.x<player.x){
        if(enemy.direction!=2)
            enemy.direction=2;
        else{
            bullets[enemy.x+2][enemy.y]=2;
        }
    }*/
}








MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent  *event) {
    if(event->key() ==Qt::Key_A) {
        ch='a';
    }
    else if(event->key() ==Qt::Key_D){
        ch='d';
    }
    else if(event->key() ==Qt::Key_S) {
        ch='s';
    }
    else if(event->key() == Qt::Key_W){
        ch='w';
    }
    else if(event->key() ==Qt::Key_X){
        ch='x';
    }
}
void MainWindow::mainfunction(){
    if(issleep==0){
        memset(bullets,0,sizeof(bullets));
        for(int i=0;i<=41;i++)
            for(int j=0;j<=41;j++)
                mp[i][j]=conserve[i][j];
        score=0;
        player.x=38;
        player.y=15;
        player.direction=1;
        getenemy();
    }
    if(issleep==1){
        issleep=0;
    }
    update();



    flag=1;
    for(int i=0;i<100;i++){
        if(enemy.x==player.x&&enemy.y==player.y){
            QMessageBox::about(NULL, "结束", "你输了");
            break;
        }
        if(flag==0||mp[enemy.x][enemy.y]){
            QMessageBox::about(NULL, "结束", "你输了");
            break;
        }


        sleep(500);

        if(ch=='A'||ch=='a'){
            if(player.y-1>=1&&mp[player.x][player.y-1]==0)
                player.y--;
            player.direction=3;
            ch='1';
        }
        else if(ch=='S'||ch=='s'){
            if(player.x+1<=39&&mp[player.x+1][player.y]==0)
                player.x++;
            player.direction=2;
            ch='1';
        }
        else if(ch=='D'||ch=='d'){
            if(player.y+1<=39&&mp[player.x][player.y+1]==0)
                player.y++;
            player.direction=4;
            ch='1';
        }
        else if(ch=='W'||ch=='w'){
            if(player.x-1>=1&&mp[player.x-1][player.y]==0)
                player.x--;
            player.direction=1;
            ch='1';
        }
        else if(ch=='X'||ch=='x'){
            if(player.direction==1)
                bullets[player.x-1][player.y]|=(1<<1);
            else if(player.direction==2)
                bullets[player.x+1][player.y]|=(1<<2);
            else if(player.direction==3)
                bullets[player.x][player.y-1]|=(1<<3);
            else if(player.direction==4)
                bullets[player.x][player.y+1]|=(1<<4);
            ch='1';
        }
        tankAI();
        this->update();
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    mainfunction();
}

void MainWindow::on_pushButton_2_clicked()
{
    sleep(500000);
    issleep=1;
}
//---------------main.cpp-------------------
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

打地鼠

//----------------hamstergame.h-----------------
#ifndef HAMSTERGAME_H
#define HAMSTERGAME_H

#include <QMainWindow>
#include<QTimer>

namespace Ui {
class HamsterGame;
}

class HamsterGame : public QMainWindow
{
    Q_OBJECT

public:
    explicit HamsterGame(QWidget *parent = nullptr);
    ~HamsterGame();

private:
    Ui::HamsterGame *ui;
    int score, time;
    QTimer * ptimer;
    void showScore();
    QPoint distance;
    bool start; //解决游戏bug
protected:
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
private slots:
    void on_mouse1_clicked();
    void on_mouse3_clicked();
    void on_mouse2_clicked();
    void on_start_clicked();
    void showMouse();
    void on_stop_clicked();
    bool isStart();
    void setStart(bool start);
    void on_line_clicked();
    void on_exit_clicked();
};

#endif // HAMSTERGAME_H
//-----------------hamstergame.cpp---------------
#include "hamstergame.h"
#include "ui_hamstergame.h"
#include<QLabel>
#include<QTimer>
#include<QMessageBox>
#include<QMouseEvent>
#include<QTime>
#include<QTimerEvent>
#include<QCursor>
#include<QPixmap>
#include<QMouseEvent>
#include<QMessageBox>
#include<QAbstractButton>
#include "hamstergame.h"
#include <QApplication>

HamsterGame::HamsterGame(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::HamsterGame)
{
    ui->setupUi(this);
    score = 0;
    time = 0;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));//产生随机数
    this->setWindowOpacity(1);//设置透明度
    this->setWindowFlags(Qt::FramelessWindowHint);//去除边框
    this->setAttribute(Qt::WA_TranslucentBackground);//透明背景
    //this->ui->menuBar->hide();
    this->setStart(false);
    this->ui->mainToolBar->hide();
    this->ptimer = new QTimer;
    //this->p = new QTimer;
    connect(this->ptimer, SIGNAL(timeout()), this, SLOT(showMouse()));
    this->ui->stop->hide();
    this->setCursor(QCursor(QPixmap(":/icons/pictureUp.png")));
    ui->mouse1->setStyleSheet("QPushButton:pressed{border-image: url(:/icons/boom.png);}"
                              "QPushButton{border-image: url(:/icons/hello.png);}");
    ui->mouse2->setStyleSheet("QPushButton:pressed{border-image: url(:/icons/boom.png);}"
                              "QPushButton{border-image: url(:/icons/teeth.png);}");
    ui->mouse3->setStyleSheet("QPushButton:pressed{border-image: url(:/icons/boom.png);}"
                              "QPushButton{border-image: url(:/icons/family.png);}");
}

HamsterGame::~HamsterGame()
{
    delete ui;
}

void HamsterGame::mousePressEvent(QMouseEvent* event)
{
    this->setCursor(QCursor(QPixmap(":/icons/picturedown.png")));
    if (event->button() == Qt::LeftButton)
    {
        distance = event->globalPos()-frameGeometry().topLeft();
        event->accept();
    }
}

void HamsterGame::mouseReleaseEvent(QMouseEvent* event)
{
    this->setCursor(QCursor(QPixmap(":/icons/pictureUp.png")));
    if(event->buttons() & Qt::LeftButton)
    {
        move(event->globalPos()-distance);
        event->accept();
    }
}

void HamsterGame::on_mouse1_clicked()
{
    this->ui->mouse1->hide();
    if(this->isStart()){//修改
        score++;
        showScore();
    }
}

void HamsterGame::on_mouse2_clicked()
{
    this->ui->mouse2->hide();
    if(this->isStart()){
        score++;
        showScore();
    }
}

void HamsterGame::on_mouse3_clicked()
{
    this->ui->mouse3->hide();
    if(this->isStart()){
        score++;
        showScore();
    }
}

void HamsterGame::on_start_clicked()
{
    this->ui->start->hide();
    this->setStart(true);//设置flag
    this->ptimer->start(1000);
    this->ui->stop->show();

}

void HamsterGame::showMouse()
{
    time++;
    ui->label->setText("用时"+QString::number(time)+"s");
    ui->mouse1->move(qrand()%640+20, (qrand()%140)+160);
    ui->mouse2->move(qrand()%640+20, (qrand()%140)+160);
    ui->mouse3->move(qrand()%640+20, (qrand()%140)+160);//y坐标要在160~300,为了适应背景
    ui->mouse1->show();
    ui->mouse2->show();
    ui->mouse3->show();
    //ui->label->move(qrand()%640+20, (qrand()%140)+160);
}

void HamsterGame::showScore()
{
    ui->label_2->setText("命中"+QString::number(score)+"只");
    //this->setCursor(QCursor(QPixmap(":/icons/pictureUp")));
}

void HamsterGame::on_stop_clicked()
{
    this->setStart(false);//设置flag
    this->ptimer->stop();
    ui->label_2->setText("命中0只");
    ui->label->setText("用时0s");
    QMessageBox::information(NULL, "游戏结束", "太厉害了!击中了"+QString::number(this->score)+"只地鼠!", QMessageBox::Yes);
    this->ui->start->show();
    this->ui->stop->hide();
    this->score = 0;
    this->time = 0;
}

void HamsterGame::setStart(bool start){
    this->start = start;
}

bool HamsterGame::isStart(){
    return this->start;
}

void HamsterGame::on_line_clicked()
{
    this->showMinimized();
}

void HamsterGame::on_exit_clicked()
{
    this->close();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    HamsterGame w;
    w.show();

    return a.exec();
}

代码下载


支付宝打赏 微信打赏

您的打赏是对我最大的鼓励!