Turtles draw modern art.
Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
Sets the orientation of the turtle to x degrees.
Starts the process to fill a shape with colour. The shape is only filled once it is fully enclosed and the command turtle.end_fill() has been executed.
Ends the process of filling a shape once it is complete. Can only be used with turtle.begin_fill().
Draws a circle of radius x. The parameter y is optional but determines the extent of the circle in degrees. turtle.circle(50, 180) would result in half a circle being drawn of radius 50.
Sets the speed of the turtle to x. The variable can be an integer between 0 and 10 with zero being the fastest. The variable can also be a string: "fastest", "fast", "normal", "slow", "slowest".
Hides the turtle so it is not shown. Vectors will still be drawn if the pen is down. Graphics are drawn faster if the turtle is not shown.
Shows the turtle.
Clears the drawing canvas, sends the turtle home to 0, 0 and resets turtle properties to default values.
Assigns x to be a new turtle. Note the capital T. You can have as many independent turtles as you need. Storing them in a list is a good way to iterate over them.
Sets the mode of the turtle to x. The variable can be "standard" or "logo". A turtle in standard mode, initially points to the right (east) and angles are counter-clockwise. A turtle in logo mode, initially points up (north) and angles are clockwise. This command is not supported in all implementations of the turtle library and should generally be avoided.
w is assigned to be the turtle output window. Follow this command with:
w.setup(x, y)
Sets the size of the turtle output window to x pixels width and y pixels height.
Set the scrollable drawing canvas to x pixels width and y pixels height. Use with turtle.done() to activate scroll bars in some implementations.
Questions to think about with this program to check your understanding:
What would be the result of swapping lines 13 and 14?
for turtle_number in range(number_of_turtles):
before...
for circles in range(50)
Instead of the red and blue circles both being drawn one at a time, all the red circles would be drawn first followed by all the blue circles. That would result in the blue circles always appearing above the red circles when they overlap.
Lines 33-36 create a new turtle and its properties. Line 37 adds the new turtle to a list of turtles before looping back to line 32 to start creating a new turtle. Why create a list of turtles when you could instead create and initialise two turtles like this:
red_turtle = turtle.Turtle()
blue_turtle = turtle.Turtle()
As we saw in level 6, using lists, indexes and iterators instead of multiple variables with different names makes the program more scalable. You can increase the number of turtles that are drawing by changing line 28 to add more or less turtles and line 29 to add more colours. Changing the scale of the program only requires changing two lines of code.
Taking the alternative approach with multiple variables would mean that you couldn't use a loop in line 14. Instead you would need to copy the code in lines 15-21 for every turtle resulting in code duplication.
It is a misconception that using lists makes the program faster. It just means the program uses less memory (fewer instructions to store) and easier to change. Arguably it may be easier to debug too because any errors will affect all turtles and not just one.
Change the program below so that it:
Due to the random nature of the program, the picture that is drawn will be different each time. To find patterns that are particularly pleasing you could experiment with making the program more deterministic by adding the line random.seed(x) where x is any integer.
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.