top of page

Python - #6 - Turtle

Import the Turtle

The turtle library stores all of the code to create and move an object called a turtle

​

The turtle library must be imported into your Python program before you can use it to draw lines, shapes and colours.

turtle.PNG

Create a new Python program and save the file as PythonTurtle.

​

Write import turtle as the first line of code.

Basic Shapes

The turtle can be controlled by writing how many pixels it should travel forward and the angle it should point left or right.

Moving Forwards

turtle.forward(100) will move the turtle forward by 100 pixels.

​

turtle.forward(200) will move the turtle forward by 200 pixels.

​

​

​

​

When using the left command or the right command, the turtle won't actually move, but it will rotate by the number of degrees that you state.

​

For example, typing turtle.left(90) will point the turtle upwards.

Rotating Left & Right

mem9.png

Copy the code to the right to make the turtle draw a square.

​

Then try to make:

  1. A Rectangle

  2. A Triangle

  3. A Pentagon

  4. A Hexagon

turtle2.PNG
turtle4.PNG

Square

turtle7.PNG

Rectangle

turtle6.PNG

Triangle

turtle5.PNG

Pentagon

turtle8.PNG

Hexagon

Hint: To work out the angles, divide 360 by the number of sides.

Using Loops

You can use a for loop to repeat code.

​

This is especially helpfully with intricate shapes with many sides.

​

The code below will print a square but in only 3 lines instead of the 8 lines from task 2.

turtle3.PNG

This is the number of times the code underneath will be repeated.

​

Change it to a higher number to repeat it more often.

Each line after the 'for num in range' line must be indented.

​

Press the tab key once on your keyboard to indent your code.

Task 3 - Copy the code above to make the turtle draw a square using a loop.

​

Then try to make:

  1. A Heptagon

  2. An Octagon

  3. A Circle

  4. A Pentagram (5-sided Star)

turtle4.PNG
turtle11.PNG
turtle10.PNG
turtle12.PNG
turtle9.PNG

Square

Heptagon

Octagon

Circle

Pentagram

Hint: To work out the angles, divide 360 by the number of sides.

Advanced Features

Choose a background colour

turtle.bgcolor("red")

   

Choose the line size and colour

turtle.pensize(6)

turtle.color("green")

 

Fill a shape

turtle.color("yellow")

turtle.begin_fill()

 

(put your turtle's directions in here)

 

turtle.end_fill()

​

Lift the pen

turtle.penup()
turtle.pendown()

 

Speed up/Slow down the turtle

turtle.speed(speed=10)

​

Change the turtle's appearance

turtle.shape("turtle")

Other options include "circle" and "arrow".

turtle13.PNG
turtle14.PNG
turtle15.PNG
turtle16.PNG
turt3.png
turt2.png
turt1.png

Task 4 - Use the code above to make:

​

  1. A blue square on a red background.

  2. A yellow triangle on a pink background.

  3. Two different coloured circles - not touching each other.

  4. Three different shapes of three different colours - not touching each other.

Complex Shapes

Use everything that you have learned on this page to help you create more complex shapes.

​

You could try:​

  • A Flower

  • A Word (like your name - you will need to use the penup() and pendown() commands.

  • A Christmas tree

  • A Landscape (green ground, blue sky, yellow sun)​

bottom of page