Here you will learn
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.
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.
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.
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.
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.
-- Output from your program will be here --
Question: What will the program draw?
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.
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.
-- Output from your program will be here --
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.
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.
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().
-- Output from your program will be here --
Login to save your progress and your code.
Draw 100 squares with a random side in the interval [10,50]. The squares should be drawn at random coordinates.
-- Output from your program will be here --
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.
Changes to a drawing area that is 200x200 pixels.
from turtle import *
Screen().setup(200,200)
Login to save your progress and your code.
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.
-- Output from your program will be here --