[python/turtle] Fix Turtle::setSpeed

If speed > 10 or < 0.5, it should be set to 0. When speed is 10 or 0,
the drawing should be instantaneous. Default speed is 3.
This commit is contained in:
Léa Saviot
2018-12-06 11:51:58 +01:00
parent 1e78f20ac1
commit 4b3ce7a7f1
2 changed files with 6 additions and 6 deletions

View File

@@ -73,11 +73,8 @@ void Turtle::setHeading(mp_float_t angle) {
}
void Turtle::setSpeed(mp_int_t speed) {
// Speed is clamped between 0 and 10
if (speed < 0) {
if (speed < k_minSpeed || speed > k_maxSpeed) {
m_speed = 0;
} else if (speed > 10) {
m_speed = 10;
} else {
m_speed = speed;
}
@@ -203,7 +200,7 @@ bool Turtle::draw() {
if (m_mileage > 1000) {
if (m_speed > 0) {
if (micropython_port_interruptible_msleep(8 * (8 - m_speed))) {
if (micropython_port_interruptible_msleep(k_maxSpeed * (k_maxSpeed - m_speed))) {
return true;
}
m_mileage -= 1000;

View File

@@ -27,7 +27,7 @@ public:
m_color(KDColorBlack),
m_penDown(true),
m_visible(true),
m_speed(6),
m_speed(k_defaultSpeed),
m_penSize(5),
m_mileage(0),
m_drawn(false),
@@ -70,6 +70,9 @@ private:
static constexpr KDCoordinate k_xOffset = Ion::Display::Width / 2;
static constexpr KDCoordinate k_yOffset = (Ion::Display::Height - 18) / 2;
static constexpr int k_numberOfIcons = 8;
static constexpr float k_minSpeed = 0.5f;
static constexpr float k_defaultSpeed = 3.0f;
static constexpr float k_maxSpeed = 10.0f;
KDPoint position(mp_float_t x, mp_float_t y) const;
KDPoint position() const { return position(m_x, m_y); }