Draw more detailed pictures using Python Turtle. Learn how to use functions to repeat code and how to generate random numbers. This is a great way to continue getting to know Python and to understand more basic coding ideas.
Digital pictures out of code like this one:
By making pictures with code you will learn how to:
Use functions to repeat code and draw multiple shapes
Generate random numbers to alter shapes and colours
Use coordinates to move around the screen and learn about events
Use while loops and boolean logic
A computer or tablet capable of accessing the online interactive Python environment called Trinket. You can create a trinket.io account using your google school account; or
If you don’t want to create an account you can use the embedded interactive trinkets below to run code and see what happens; or
You can use a computer that has the Python programme installed.
Sign in to your trinket.io account, click on your name and then select ‘New Trinket’ and ‘Python’ from the drop-down menus.
Import the Turtle library by typing import turtle as t. The 'as t' command means you don't need to create a variable t = Turtle() and therefore saves a line of text.
Let's set the turtle characteristics, add the the following lines of code:
t.speed(0)
t.pensize(2)
t.pencolor("white")
t.Screen().bgcolor("cyan")
t.hideturtle()
Now you can start to tell your turtle what to draw. A snowflake has many branches and arms. Using loops is a really good way to repeat lines of code but you can also store code inside a function (a chunk of pre-programmed code that tells Python to do something) and then call the function to repeat the code again and again. To draw a branch of a snowflake, turtle needs to move forward and then draw a V shape. Code for defining functions is normally first in a programme so insert a line under 'import turtle as t' and type:
def branch():
t.forward(30)
t.left(45)
for i in range(3):
t.forward(30)
t.backward(30)
t.right(45)
t.left(90)
Be careful that all your indents are right! Nothing will happen until you call the function (run the code in the function). To do this type the name of the function branch() at the end of your code and then click run.
Now you can use your branch function to draw the arm of the snowflake. Define a function called 'arm' by typing:
def arm():
for i in range(4):
branch()
t.backward(30*4)
To call your new function replace branch() with arm() at the bottom of your code and click run.
Finally, you can define a function that draws a whole snowflake:
def snowflake():
for i in range(6):
arm()
t.left(360/6)
Call this snowflake function by replacing arm() with snowflake() at the bottom of your code. Try changing the size of the snowflake, the number of branches, the number of arms and the colour.
How about creating a snowstorm with snowflakes of different shapes and sizes all over the screeen? To do this you need to import the random library from Turtle by typing import random as r under 'import turtle as t'.
Next, set the parameter for your branch function to def branch(size) and your arm function to def arm(size). Also change any forward and backward parameters to 'size' . Now define the new variable 'size' in your snowflake function by typing size = r.randint(3,10). The randint function will produce a random integer within the range specified, so this code will draw a snowflake where the branches are a random number between 3 and 10 pixels long. Click run a few times and see how the snowflakes vary in size.
You can also choose the shape of the snowflake randomly by setting the number of arms to be random. Create another new variable under 'size' and call it 'arms'. Type arms = r.randint(6,10). Change the parameter in the for loop below to 'arms' and the left command to t.left(360/arms). Click run a few times again to see how the shapes vary.
To draw snowflakes at random points on the screen you need to set the starting position of the turtle. You can use x,y coordinates and the goto() function. In your snowflake function before the 'size' and 'arms' variables, type:
t.penup()
t.goto(r.randint(-200, 200), r.randint(-200, 200))
t.pendown()
Run the code a few times to check the snowflakes are appearing at different positions.
Finally, place the code where you call the snowflake function into a while loop (repeats blocks of code indefinitely until told to stop by a condition). If a while loop is set to True (known as a Boolean condition, can only be True or False) it is an infinite loop and will run your code until you click the stop button. Type while True: above the snowflake function call and then indent the following line of code. Run the program and see your snowstorm unfold!
Is it a bit annoying how some snowflakes appear on top of others? Would you prefer to choose where turtle draws the snowflakes? No problem, you can use the onscreenclick() function. This function records the x,y coordinates when the mouse is clicked on the screen. When a user provides keyboard, mouse or other input this is known as an event. Replace the while loop with the following code t.Screen().onscreenclick(snowflake).
Now add the x,y coordinates that will be generated by that command as parameters in your snowflake function def snowflake(x,y): and change the goto function parameters aswell t.goto(x,y). Click run and trying clicking within the screen. Make a beautiful snowy scene!
Using the random library and the onscreenclick() function you can make many different colourful pictures. You can also colour in the shapes you draw by using turtle's begin_fill(), end_fill() and fillcolor() functions. Start a new trinket and type the code to import the turtle and random libraries as before. Define a new function def drawstar(): and give it the following code:
points = 5
colour = (r.randint(0,255), r.randint(0,255), r.randint(0,255))
angle = 180-(180/points)
t.color(colour)
for i in range(points):
t.forward(100)
t.right(angle)
Now set turtle's characteristics t.speed(5), t.Screen().bgcolor("dark blue"), t.hideturtle() and call the drawstar() function. Click run a few times to see the different colours of star you get. Try changing the value of the variable 'points' but make sure you only use odd numbers or you'll get an unfinished star!
You can vary the size of the star drawn by adding the variable size = r.randint(10,50) and changing t.forward(size). You can also make the number of points random using the code points = r.randint(2,5)*2+1 this will generate only odd numbers between 5 and 11. You can colour in the stars by adding t.begin_fill() after t.color() and t.end_fill() after the for loop. Run the code and see all the different stars.
Finally, instead of calling the drawstar function, use t.Screen().onscreenclick(drawstar) as you did for the snowflakes. Change def drawstar(x,y) to include the x,y coordinates from the mouse click and add t.penup(), t.goto(x,y) and t.pendown() to the function code. Also change t.speed(0). Now make yourself a beautiful starry picture!
You can make a colourful patchwork by:
defining a function to choose a colour at random using RGB values (0-255) def choosecolour()
defining a function to draw a square def drawsquare()
defining a function to draw a row of squares by calling the drawsquare function def drawrow()
defining a function to draw the whole pattern by calling the drawrow function def drawpattern()
finally, setting the turtle starting position (x,y), calling the drawpattern function and setting it's parameters for size and number of squares.