diff --git a/cars.cpp b/cars.cpp index acc545a..eb7893d 100644 --- a/cars.cpp +++ b/cars.cpp @@ -27,32 +27,32 @@ Cars::Cars(QGraphicsItem *parent, int dir, Cars::Color color) : QGraphicsPixmapI setTransformOriginPoint(M_WID/2, M_LEN/2); //设置旋转中心 } -void Cars::Forward(int vel) +void Cars::Forward(qreal vel) { - double ang = qDegreesToRadians(rotation()); + qreal ang = qDegreesToRadians(rotation()); this->moveBy(+vel*qSin(ang), -vel*qCos(ang)); } -void Cars::Backward(int vel) +void Cars::Backward(qreal vel) { - double ang = qDegreesToRadians(rotation()); + qreal ang = qDegreesToRadians(rotation()); this->moveBy(-vel*qSin(ang), +vel*qCos(ang)); } -void Cars::moveLeft(int vel) +void Cars::moveLeft(qreal vel) { - double ang = qDegreesToRadians(rotation()); + qreal ang = qDegreesToRadians(rotation()); moveBy(-vel*qCos(ang), -vel*qSin(ang)); } -void Cars::moveRight(int vel) +void Cars::moveRight(qreal vel) { - double ang = qDegreesToRadians(rotation()); + qreal ang = qDegreesToRadians(rotation()); moveBy(vel*qCos(ang), vel*qSin(ang)); } //ang为旋转角度(弧度),正数为顺时针,负数逆时针 -void Cars::Rotate(int ang) +void Cars::Rotate(qreal ang) { setRotation(rotation() + ang); } diff --git a/cars.h b/cars.h index 2125755..b861d90 100644 --- a/cars.h +++ b/cars.h @@ -16,11 +16,11 @@ class Cars : public QGraphicsPixmapItem public: enum Color { Pink, Red, Yellow, RANDOM }; 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 Forward(qreal vel = 3); + void Backward(qreal vel = 3); + void moveLeft(qreal vel = 3); + void moveRight(qreal vel = 3); + void Rotate(qreal ang = 30); void turnLeft(int r, int ang); void turnRight(int r, int ang);