#include "car.h"

Car::Car()
{
    this->v = 0;
    this->t = 0;
}
void Car::step()
{
    this->v += this->a * this->t * this->t / 2;
    double dt = 0.1;
    t += dt;
    if(this->v > maxV && a > 0) {
        this->t = 0;
        a = -a;
    }
    if(this->v <= 0 && a < 0) {
        this->t = 0;
        a = -a;
        v = 0;
    }
}
#ifndef CAR_H
#define CAR_H


class Car
{
public:
    Car();
    void step();

    double getV() {
        return v;
    }

    double getA() {
        return a;
    }

    double getT() {
        return t;
    }

    void setA(double a) {
        this->a = a;
    }

    void setMaxSpeed(double maxV) {
        this->maxV = maxV;
    }
private:
    double a;
    double v;
    double maxV;
    double t;

};
#endif // CAR_H
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    car.cpp \
    main.cpp \
    mainwindowcar.cpp

HEADERS += \
    car.h \
    mainwindowcar.h

FORMS += \
    mainwindowcar.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 += \
    mine.qrc
#include "mainwindowcar.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindowCar w;
    w.show();
    return a.exec();
}
#include "mainwindowcar.h"
#include "ui_mainwindowcar.h"
#include <QPixmap>
#include <QGraphicsScene>
#include <QRect>
#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <QMessageBox>

MainWindowCar::MainWindowCar(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowCar)
{
    ui->setupUi(this);
    this->setupImages();
    car = new Car();
    this->carOneOrTwo = true;
}

MainWindowCar::~MainWindowCar()
{
    delete ui;
    delete car;
}

void MainWindowCar::setupImages() {
    QPixmap p (":/Img/road.png");
        if(!ui->Background->scene()) {
        QGraphicsScene * s = new QGraphicsScene(this);
        ui->Background->setScene(s);
    }
        ui->Background->scene()->addPixmap(p);

    //preload car images
    this->carIcon1 = QIcon(":/Img/mypolice.png");
    this->carIcon2 = QIcon(":/Img/mypolice2.png");

    this->updateCar();
}


void MainWindowCar::updateCar() {

    //code to substitute image of a car

    //each run to change image to another
    if (this->carOneOrTwo)
        ui->PushCar->setIcon(this->carIcon1);
    else
        ui->PushCar->setIcon(this->carIcon2);


    carOneOrTwo = !carOneOrTwo;

    ui->PushCar->setIconSize(QSize(100, 200));

}

void MainWindowCar::stepUi() {
    QRect oldRect = ui->PushCar->geometry();

    if(oldRect.x() > this->geometry().width() - 200) {
        oldRect.setX(50);
    } else {
        oldRect.setX(oldRect.x() + (int)car->getV());

    }
    oldRect.setWidth(120);
    ui->PushCar->setGeometry(oldRect);

}
void MainWindowCar::on_pushButtonStart_clicked()
{

    //this->ui->pushButtonStart->setVisible(false);
    int frame = 0;
    bool ok = true;

    this->car->setA(this->ui->lineEditAccel->text().toDouble(&ok));
    if (!ok) {
        QMessageBox::warning(this, "Error", "Please enter a good number for acceleration!");
        return;
    }
    ok = true;
    this->car->setMaxSpeed(this->ui->lineEditMaxSpeed->text().toDouble(&ok));
    if (!ok) {
        QMessageBox::warning(this, "Error", "Please enter a good number for max speed!");
        return;
    }

    this->isStopped = false;
    this->ui->pushButtonStart->setEnabled(false);
    this->ui->lineEditAccel->setEnabled(false);
    this->ui->lineEditMaxSpeed->setEnabled(false);


    while(!this->isStopped) {
        QCoreApplication::processEvents();
        QThread::currentThread()->msleep(100);
        this->stepUi();
        if (frame % 5 == 0) this->updateCar(); //update each 5th frame
        car->step();
        qDebug() << car->getV();
        frame++;
    }
}


void MainWindowCar::on_pushButtonStop_clicked()
{
    this->isStopped = true;
    this->ui->pushButtonStart->setEnabled(true);
    this->ui->lineEditAccel->setEnabled(true);
    this->ui->lineEditMaxSpeed->setEnabled(true);
}



#ifndef MAINWINDOWCAR_H
#define MAINWINDOWCAR_H

#include <QMainWindow>
#include <QIcon>
#include "car.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindowCar; }
QT_END_NAMESPACE

class MainWindowCar : public QMainWindow
{
    Q_OBJECT

public:
    MainWindowCar(QWidget *parent = nullptr);
    ~MainWindowCar();

private slots:
    void on_pushButtonStart_clicked();

    void on_pushButtonStop_clicked();


private:
    Ui::MainWindowCar *ui;
public:
    Car *car;

    QIcon carIcon1;
    QIcon carIcon2;

    void stepUi();
    void setupImages();
    void updateCar();
    bool isStopped;
    bool carOneOrTwo;
};
#endif // MAINWINDOWCAR_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindowCar</class>
 <widget class="QMainWindow" name="MainWindowCar">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>900</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindowCar</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QGraphicsView" name="Background">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>70</y>
      <width>891</width>
      <height>471</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonStart">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>0</y>
      <width>100</width>
      <height>32</height>
     </rect>
    </property>
    <property name="text">
     <string>Start</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonStop">
    <property name="geometry">
     <rect>
      <x>790</x>
      <y>0</y>
      <width>100</width>
      <height>32</height>
     </rect>
    </property>
    <property name="text">
     <string>Stop</string>
    </property>
   </widget>
   <widget class="QPushButton" name="PushCar">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>380</y>
      <width>100</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="flat">
     <bool>true</bool>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEditAccel">
    <property name="geometry">
     <rect>
      <x>320</x>
      <y>5</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>2</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>230</x>
      <y>7</y>
      <width>81</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Acceleration</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>410</x>
      <y>7</y>
      <width>91</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Max Speed</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEditMaxSpeed">
    <property name="geometry">
     <rect>
      <x>500</x>
      <y>5</y>
      <width>113</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>30</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>900</width>
     <height>24</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
<RCC>
    <qresource prefix="/Img">
        <file>road.png</file>
        <file>mypolice.png</file>
        <file>mypolice2.png</file>
    </qresource>
</RCC>
这里边包含了car.cpp，car.h，carmoving_wu.pro，main.cpp，mainwindowcar.cpp，mainwindowcar.h，mainwindowcar.ui，mine.qrc文件，帮我仿照这些代码按照相同的结构，写出一个人在房间走的画面的代码，在qt中实现