From e56597c1b501704daf28f1152381f7d8e7073ac1 Mon Sep 17 00:00:00 2001 From: TurnMeOn Date: Tue, 19 Dec 2017 18:38:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=A6=E7=9A=84=E5=89=8D=E5=90=8E=E5=B7=A6?= =?UTF-8?q?=E5=8F=B3=E7=A7=BB=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cars.cpp | 81 +++++++++++++++++++++++++++++++++++++++----------------- cars.h | 34 +++++++++++++----------- 2 files changed, 74 insertions(+), 41 deletions(-) diff --git a/cars.cpp b/cars.cpp index 7fceb60..bf93cdb 100644 --- a/cars.cpp +++ b/cars.cpp @@ -1,50 +1,81 @@ #include "cars.h" -#include -Cars::Cars(QWidget *parent, int dir, Cars::Color color) : QWidget(parent) + +Cars::Cars(QGraphicsItem *parent, int dir, Cars::Color color) : QGraphicsPixmapItem(parent) { + m_pos = this->pos(); + this->setRotation(dir); if(color==RANDOM) color = Cars::Color(rand()%3); //车身颜色一定是确定的 m_color = color; - m_dir = dir; - m_pos = this->pos(); - resize(M_WID, M_LEN); switch (m_color) { case Pink: - m_pixmap = QPixmap(":/cars/pink"); + m_pic = QPixmap(":/cars/pink"); break; case Red: - m_pixmap = QPixmap(":/cars/red"); + m_pic = QPixmap(":/cars/red"); break; case Yellow: - m_pixmap = QPixmap(":/cars/yellow"); + m_pic = QPixmap(":/cars/yellow"); break; default: - m_pixmap = QPixmap(":/cars/red"); + m_pic = QPixmap(":/cars/red"); break; } -} - -void Cars::paintEvent(QPaintEvent *event) { - QPainter painter(this); - m_pos = this->pos(); - painter.translate(M_WID/2, M_LEN/2); //转移远点,设置旋转中心 - painter.rotate(m_dir); //旋转 - painter.translate(-M_WID/2, -M_LEN/2); //移回远点 - painter.drawPixmap(0, 0, M_WID, M_LEN, m_pixmap); //绘制小车 - painter.setPen(QPen(Qt::red, 5)); - painter.drawRect(0, 0, M_WID, M_LEN); - event->ignore(); + m_pic = m_pic.scaled(M_WID, M_LEN); + this->setPixmap(m_pic); } void Cars::Forward(int vel) { - float ang = m_dir*PI/180; - this->move(m_pos.x()+vel*sin(ang), m_pos.y()-vel*cos(ang)); + double ang = qDegreesToRadians(rotation()); + this->moveBy(+vel*qSin(ang), -vel*qCos(ang)); } void Cars::Backward(int vel) { - float ang = m_dir*PI/180; - this->move(m_pos.x()-vel*sin(ang), m_pos.y()+vel*cos(ang)); + double ang = qDegreesToRadians(rotation()); + this->moveBy(-vel*qSin(ang), +vel*qCos(ang)); +} + +void Cars::moveLeft(int vel) +{ + double ang = qDegreesToRadians(rotation()); + moveBy(-vel*qCos(ang), -vel*qSin(ang)); +} + +void Cars::moveRight(int vel) +{ + double ang = qDegreesToRadians(rotation()); + moveBy(vel*qCos(ang), vel*qSin(ang)); +} + +//ang为旋转角度(弧度),正数为顺时针,负数逆时针 +void Cars::Rotate(int ang) +{ + setRotation(rotation() + ang); +} + +void Cars::turnLeft(int r, int ang) +{ + Forward(r*qSin(ang)); + Rotate(-ang); + moveLeft(r*(1-qCos(ang))); +} + +void Cars::turnRight(int r, int ang) +{ + Forward(r*qSin(ang)); + Rotate(ang); + moveRight(r*(1-qCos(ang))); +} + +Cars::Color Cars::getColor() +{ + return this->m_color; +} + +qreal Cars::getDir() +{ + return this->rotation(); } diff --git a/cars.h b/cars.h index 68f7fc9..2125755 100644 --- a/cars.h +++ b/cars.h @@ -1,36 +1,38 @@ #ifndef CARS_H #define CARS_H -#include +#include #include #include -#include #include +#include //车身长宽 #define M_LEN (200) #define M_WID (130) -#define PI (3.1415926f) -class Cars : public QWidget +class Cars : public QGraphicsPixmapItem { - Q_OBJECT public: enum Color { Pink, Red, Yellow, RANDOM }; - explicit Cars(QWidget *parent = nullptr, int dir = 0, Cars::Color color = RANDOM); - void paintEvent(QPaintEvent *event); - void Forward(int vel); - void Backward(int vel); + explicit Cars(QGraphicsItem *parent = Q_NULLPTR, int dir = 0, Cars::Color color = RANDOM); + void Forward(int vel = 3); + void Backward(int vel = 3); + void moveLeft(int vel = 3); + void moveRight(int vel = 3); + void Rotate(int ang = 30); + void turnLeft(int r, int ang); + void turnRight(int r, int ang); + + Color getColor(); + qreal getDir(); + private: - Color m_color; //车身颜色 - int m_dir; //车头朝向 方向角 N为0, 顺时针递增 - QPixmap m_pixmap; + Color m_color; //车身颜色 N为0, 顺时针递增 + QPixmap m_pic; QString m_number; - QPoint m_pos; + QPointF m_pos; -signals: - -public slots: }; #endif // CARS_H