在编写代码时,无意弄出来了一个这个东西,觉得挺有意思的记录一下,类似乒乓球在矩形内一直运动碰撞反弹demo
头文件
#ifndef MYPROJECT_H
#define MYPROJECT_H
#include <QMainWindow>
#include <QPainter>
#include "form.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MyProject; }
QT_END_NAMESPACE
class MyProject : public QMainWindow
{
Q_OBJECT
public:
MyProject(QWidget *parent = nullptr);
~MyProject();
Form *m_form;
public:
QTimer *timer;
int m_x;
int m_y;
int m_type_x;
int m_type_y;
private slots:
void updateProgressBar();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MyProject *ui;
};
#endif // MYPROJECT_H
#include "myproject.h"
#include "ui_myproject.h"
#include <QDebug>
MyProject::MyProject(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MyProject)
,m_y(1)
,m_x(1)
,m_type_x(0)
,m_type_y(0)
{
ui->setupUi(this);
// 初始化定时器
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MyProject::updateProgressBar);
timer->setInterval(100);
timer->start();
ui->label->setGeometry(th