Turtle Graphics
Last updated
Last updated
We can create graphics in Python by using a turtle. This is an imaginary pointer that we can instruct to move around the screen.
Our first turtle program is going to create a spiral by drawing lines of every greater length before turning through 90 degrees.
Open up a new python file and type in the following code:
Play around with the numbers in the code to see their effect on the output. Try turning by 91 degrees instead of 90. What happens?
We can also draw basic shapes with turtle. Let's try the circle command.
We can add colour to our turtle by using the pencolor command. Modify your code to set the pen to red
One colour is a bit boring though. We can store several colours in a list and get python to cycle through them.
In almost all programming languages, list entries are numbered starting from 0. So colors[0] = red, colors[1] = yellow etc.
x % 4 (said as x mod 4) calculates the remainder when x is divided by 4. So in our loop this will cycle through 0,1,2,3
Task: Who can make the most appealing graphic. Try to add more than 4 colours to your drawing. Go here to see a list of other colours.
Of course, your images will look much better if the background was darker. We can set the background colour using the bgcolor
command:
We're now going to add some complexity to our programmes to make our images look even better. Take a look at the following code:
Lets look at the changes one at a time:
sides = 6
: In the examples above we had squares, i.e 4 sides. Now we want to store the number of sides in a variable so that we can change it easily.
for x in range (360)
: Not much of a change here but we're going to create a bigger picture by going to 360
colors = ["red", "yellow", "blue", "orange", "green", "purple"]
: More colours. Well why not!
t.forward(x * 3 / sides + x)
: This adds some complexity to our image, moving the turtle forward more with each loop. Can you work out what it's doing?
t.left(360/sides + 1)
: Before we were turning left 90 + 1 degrees because we had a square. 360/4 = 90. Now we want to create a shape with however many sides we have stored in our variable.
t.width(x*sides/100)
: Again, this is adding more complexity. The width command sets how wide our pen mark will be, gradually increasing the width as the spiral moves out.
Make these changes to your code and run the program
Make sure that you copy the changes exactly!!
Play around with your code and see what kind of interesting images you can make. Screenshot your best effort and submit your work.
IDEA: Change the number of side, add more colors. Try adding an extra turn to the end of your loop, like t.left(90)
On a Mac, you can take a screenshot by pressing Command-shift-4
and selecting the area that you want to screenshot. A file of the screenshot will be saved on your desktop.
We can even ask the user how many sides they would like. Replace the line side=6 with the following:
This takes an input from the user and uses the eval()
function to convert it in to a number that the computer can use.