6.6.1 – Turtle graphics

6.6.1 – Turtle graphics

Create and control named turtles using the Python subset. Predict positions and headings, draw separate and filled shapes, understand circles and modes, and inspect whether a drawing meets its requirements.

Create the screen and turtle

The turtle library turns movement instructions into a drawing. Position is the point on the canvas; heading is the direction it faces. Its pen state determines whether movement leaves a line. A named turtle, such as pen, has its own state; the screen is the shared drawing surface.

In standard mode a new turtle starts at the centre, position (0, 0), facing east. Positive x is right and positive y is up. Distances are drawing units, normally pixels in a desktop window. A forward move changes position in the current direction. Turning changes direction without moving.

Import the module, create the screen, choose its window size, then create the turtle. Screen.setup controls the desktop window; turtle.screensize controls the drawing canvas, which can be larger and scrollable. They are related sizes with different roles. In NovaLearn the drawing is fitted into the available panel; in a desktop IDE a separate window normally opens.

Draw a right angle

{"exercise_id": "cs-c27-first", "task_version": "1", "runtime_profile": "python-turtle-v1", "original_filename": "main.py", "file_mode": "fresh", "code": "import turtle\n\nscreen = turtle.Screen()\nscreen.setup(600, 400)\nturtle.screensize(600, 400)\nturtle.mode("standard")\npen = turtle.Turtle()\npen.speed(0)\npen.forward(80)\npen.left(90)\npen.forward(50)\nturtle.done()\n", "instructions": "Run the program. It draws east 80 units then north 50. Change only the second distance to 90 and predict the endpoint before running again.", "files": []}

The final endpoint is (80, 50), facing north. The canvas, not the terminal, shows the drawing; no console text is requested. The terminal remains available for errors or input when a program needs them.

turtle.done() is the final call. In a desktop IDE it keeps the graphics event loop alive until the window closes. In the browser it finishes the drawing; the result stays visible in its panel. A final input call can also keep some desktop programs open, but it requires the user to answer and does not provide the same scrollbar behavior. Use the explicit graphics completion call here.

Trace movement and heading

forward moves in the direction the turtle faces; back moves the opposite way without reversing its heading. left turns counterclockwise and right turns clockwise. setheading chooses an absolute direction, unlike a relative turn.

In standard mode the heading values are 0 east, 90 north, 180 west and 270 south. A full 360-degree turn returns to the same heading. Trace the position and heading separately:

Python
import turtle
turtle.mode("standard")
pen = turtle.Turtle()
pen.forward(60)
pen.left(90)
pen.back(20)
pen.right(90)
pen.setheading(180)
pen.forward(30)
turtle.done()
After movementPositionHeading
Forward 60(60, 0)East
Left 90, then back 20(60, -20)North
Right 90(60, -20)East
Set heading 180, then forward 30(30, -20)West

Back moved south while the turtle still faced north. If you change a turn to setheading, reason from the absolute compass direction rather than adding to the old angle.

Control the pen and position

Pen up means movement changes position without drawing. Pen down resumes drawing. setposition(x, y) moves directly to a coordinate and draws the joining line when the pen is down. Lift the pen before relocating between separate shapes.

pencolor chooses the line colour and pensize its width. Use a positive width. Named colours such as navy, red, gold and black are convenient. Python turtle also accepts RGB colour values; with its default colour scale each component lies from 0 to 1, for example the tuple (1.0, 0.0, 0.0) for red. Use named colours in these tasks so no colour-mode change is required.

Relocate without a joining line

{"exercise_id": "cs-c27-pen", "task_version": "1", "runtime_profile": "python-turtle-v1", "original_filename": "main.py", "file_mode": "fresh", "code": "import turtle\npen = turtle.Turtle()\npen.pencolor("navy")\npen.pensize(4)\npen.forward(50)\npen.penup()\npen.setposition(-60, 40)\npen.setheading(90)\npen.pendown()\npen.forward(40)\npen.hideturtle()\nturtle.done()\n", "instructions": "Run and identify the two separate navy lines. Temporarily remove penup to see the unwanted connector, then restore it.", "files": []}

The first line runs from (0, 0) to (50, 0). The second runs from (-60, 40) to (-60, 80). hideturtle hides only the cursor; it leaves the drawing. showturtle makes the cursor visible again. speed controls animation speed in a desktop implementation: 1 is slow, 10 fast, and 0 fastest; named settings include slowest, slow, normal, fast and fastest. Browser rendering may batch the completed drawing, so speed is not a change to its geometry.

home() moves back to (0, 0) and resets the heading to the mode's starting direction. With the pen down it can draw a line home. reset() clears that turtle's drawing and restores its initial state, including position, direction and pen settings. It is not just a move, and in a program with several turtles each named turtle owns its own drawing and state.

To return without drawing a connector and reveal the cursor:

Python
pen.penup()
pen.home()
pen.showturtle()

To erase that turtle's drawing and restore its initial pen settings instead:

Python
pen.reset()

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account