Log in to save your progress.
<< Previous
Home
Next >>
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

Reference

Click on a function to read more.
Turtle
Function
Short description
back()
Moves the turtle backward
begin_fill()
Starts filling with color
circle()
Draws a circle with optional radius
color()
Determines the color the turtle draws with
end_fill()
Ends filling with color
forward()
Moves the turtle forward
goto()
Moves the turtle to a given coordinate
hideturtle()
Hides the turtle
left()
Turns the turtle left a number of degrees
pendown()
Puts the pen down on the drawing area.
penup()
Lifts the pen from the drawing area
right()
Turns the turtle right a number of degrees
shape()
Changes the drawing shape
Screen().setup()
Creates a drawing area with a given size
speed()
Sets the turtle's drawing speed

The Coordinate System

Here you will learn

How is the coordinate system structured?

The turtle starts at the coordinate (0,0). If the turtle moves to the right, the x-coordinate increases, and if it moves upwards, the y-coordinate increases. For example, if the turtle moves 100 steps to the right and then 200 steps upwards, it is at (100,200).

This may sound obvious if you are used to coordinate systems from mathematics. But it is common in computer contexts that (0,0) is at the top left and that the positive y-direction is downwards. With the turtle module, however, it works just like in mathematics.

Example

Draws a coordinate system and some points with coordinates written out. You do not need to understand the code in this case. Try changing which points are drawn at the bottom of the code!

# Click the Test button to see and run the program.
        

Now let's see how we can use the coordinate system to make the turtle go directly to a coordinate.

Goto

With the function goto(x,y), the turtle goes to the coordinate (x,y). If we want the turtle to go to (0,100), we write goto(0,100). If the pen is down, the turtle draws as it moves there.

Example

Draws a rectangle using goto. Note how the turtle is always facing to the right.

goto(100,0)
goto(100,50)
goto(0,50)
goto(0,0)
  

As we saw in the example above, the turtle does not change its direction when goto is used. It does not matter in which direction the turtle is pointing with goto.

Login or create an account to save your progress and your code.

What does the code do?

Read the code below and try to figure out what the program draws. Run the program after you answer and see if you were correct.

Question: What will the program draw?

A plus sign inside a circle
A cross inside a circle
A plus sign
A cross

Before the create task below, we introduce a new function, pensize(thickness) where the argument thickness is the thickness of the pen. With it, we can vary how thick the lines the turtle draws are. By default, the thickness is 1. To draw with a thickness of 5, write pensize(5).

Login to save your progress and your code.

Create

Draw a cross with any thickness using goto. Choose for yourself whether it will be a cross from corner to corner or a smaller cross.

Random coordinates

Now we will make the turtle draw at a random location on the drawing area. To do this, we need to randomize both the x-coordinate and the y-coordinate using randint.

Example

Draws 20 circles with a radius of 20 at random coordinates.

for i in range(20):
    x = randint(-200,200)
    y = randint(-200,200)
    penup()
    goto(x,y)
    pendown()
    circle(20)
  

Login to save your progress and your code.

Modify the code

Use color(r,g,b) and change so that the colors of the circles are completely random. Fill the circles with begin_fill() and end_fill().

Login to save your progress and your code.

Create

Draw 100 squares with a random side in the interval [10,50]. The squares should be drawn at random coordinates.

Change the size of the drawing area

The drawing area is by default 400x400 pixels. This can be changed with Screen().setup(width,height) where width and height are the number of pixels. In the drawing area, it says Maximum width, which is the maximum width so that the drawing area still fits next to the code. As height, you can choose any value.

Example

Changes to a drawing area that is 200x200 pixels.

from turtle import *
Screen().setup(200,200)
  

Login to save your progress and your code.

Create

Change the size of the drawing area to a square with maximum width. Then draw a circle that touches all the outer sides of the drawing area.

Status
You have finished all tasks in this section!
🎉