嵌入式学习笔记4——小游戏

小游戏实现按下箭头移动小球,小球撞击兔子得到加分,兔子随机出现在窗口任意位置。

1.通过判断两张图片(小球、兔子)的中心点位置,当小于某个值时表示两者已经碰撞,自动加分显示。
void MainWindow::timerEvent(QTimerEvent e){
if(e->timerId()==tid){
posx=qrand()%300;
posy=qrand()%400;
ui->pushButton_5->move(posx,posy);
if(x
posx+y*posy<50000){
score+=10;
ui->label->setText(" score:"+QString::number(score));
}
}
this->update();
}


***头文件***
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPainter>
#include <QTimerEvent>
#include <QTime>
#include <QBitmap>
#include <math.h>
#include <QDebug>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void paintEvent(QPaintEvent *e);
    void timerEvent(QTimerEvent *e);
    int tid;
    int score;
    int posx,posy,x,y;
    QPixmap ball;
    QPixmap rabbit;
    QPixmap bg;
    QPixmap showscore;
    QPixmap up;
    QPixmap down;
    QPixmap left;
    QPixmap right;

private:
    Ui::MainWindow *ui;

private slots:
    void on_pushButton_3_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_4_clicked();
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H

构造函数

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    tid=this->startTimer(5000);
    QTime time;
    time= QTime::currentTime();
    qsrand(time.msec()+time.second()*1000);
    
    score=0;
    x=y=200;
    bg.load(":/new/prefix1/cao.jpg");
    ball.load(":/new/prefix1/ball.png");
    rabbit.load(":/new/prefix1/rabbit.png");
    up.load(":/new/prefix1/up.png");
    down.load(":/new/prefix1/down.png");
    left.load(":/new/prefix1/left.png");
    right.load(":/new/prefix1/right.png");
    showscore.load(":/new/prefix1/show.png");
    
    ui->pushButton_5->resize(rabbit.size());
    ui->pushButton_5->setMask(rabbit.mask());
    ui->pushButton_5->setStyleSheet("background-image:url(:/new/prefix1/rabbit.png)");

    ui->label->resize(showscore.size());
    ui->label->setMask(showscore.mask());
    ui->label->setStyleSheet("background-image:url(:/new/prefix1/show.png)");

    ui->pushButton->resize(up.size());
    ui->pushButton->setMask(up.mask());
    ui->pushButton->setStyleSheet("background-image:url(:/new/prefix1/up.png)");

    ui->pushButton_4->resize(down.size());
    ui->pushButton_4->setMask(down.mask());
    ui->pushButton_4->setStyleSheet("background-image:url(:/new/prefix1/down.png)");

    ui->pushButton_2->resize(left.size());
    ui->pushButton_2->setMask(left.mask());
    ui->pushButton_2->setStyleSheet("background-image:url(:/new/prefix1/left.png)");

    ui->pushButton_3->resize(right.size());
    ui->pushButton_3->setMask(right.mask());
    ui->pushButton_3->setStyleSheet("background-image:url(:/new/prefix1/right.png)");
}


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


void MainWindow::on_pushButton_clicked()//up
{
    y-=20;
    this->update();
}

void MainWindow::on_pushButton_4_clicked()//down
{
    y+=20;
    this->update();
}

void MainWindow::on_pushButton_2_clicked()//left
{
    x-=20;
    this->update();
}

void MainWindow::on_pushButton_3_clicked()//right
{
    x+=20;
    this->update();
}

void MainWindow::paintEvent(QPaintEvent *e){
    QPainter p(this);
    p.drawPixmap(0,0,1250,700,bg);
    p.drawPixmap(x,y,170,170,ball);
}

void MainWindow::timerEvent(QTimerEvent *e){
    if(e->timerId()==tid){
        posx=qrand()%300;
        posy=qrand()%400;
        ui->pushButton_5->move(posx,posy);
        if(x*posx+y*posy<50000){
            score+=10;
            ui->label->setText("        score:"+QString::number(score));
        }
    }
    this->update();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值