moveBackward

Category:Turtle

moveBackward(pixels)

Category: Turtle

Moves the turtle backward a given number of pixels from the current direction.

Sometimes it is more natural to tell the turtle to moveBackward() in a straight line instead of telling the turtle to moveForward() a negative number of pixels.

Examples


// Move backward 25 pixels (default)
moveBackward();

Example: Letter L Draw the letter L with moveBackward() and turnLeft() only.

// Draw the letter L with moveBackward() and turnLeft() only.
moveBackward();
moveBackward();
turnLeft();
moveBackward();

Example: Long Line Move backward 200 pixels.

// Move backward 200 pixels.
moveBackward(200);

Example: Barbell Draw a barbell using moveForward(), moveBackward() and dot().

// Draw a barbell using moveForward(), moveBackward() and dot().
moveForward(50);
dot(10);
moveBackward(100);
dot(10);
moveForward(50);

Example: Parallel Park the Turtle Use canvas drawing and moveBackward() to parallel park.

// Use canvas drawing and moveBackward() to parallel park.
speed(10);
createCanvas("id", 320, 480);
setFillColor("yellow");
rect(180, 100, 60, 100);
setFillColor("red");
rect(180, 300, 60, 100);
show();
moveForward(75);
moveBackward(50);
turnLeft(45);
moveBackward(75);
turnRight(45);
moveForward();

Syntax

moveBackward(pixels);

Parameters

Name Type Required? Description
pixels number No The number of pixels to move the turtle backward from its current direction. If not provided, the turtle will move forward 25 pixels

Returns

No return value. Moves turtle only.

Tips

  • Use penUp() before calling moveBackward() to have the turtle not draw as it moves.
  • The screen default size is 320 pixels wide and 450 pixels high, but you can move the turtle off the screen by exceeding those dimensions.
  • There are three ways to move the turtle in a straight line:
    • Specify the number of pixels to move the turtle in the direction it is facing using moveForward(pixels) or moveBackward(pixels).
    • Specify a number of pixels in the x and y direction to move the turtle using move(x,y), regardless of direction that the turtle is facing.
    • Specify an x and y pixel location on the screen to move the turtle to using moveTo(x,y), regardless of direction that the turtle is facing.

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org