Resources for the 'Digital Flag Design' Creative Computing Club.
During this workshop, you will use Python's Turtle module to draw flags, for Europe Code Week.
This page recaps what we discussed as a group.
To skip to the examples and ideas, click here.
Load the starter code into Mu editor.
Click the Mode button in Mu's toolbar, and select 'Python 3'.
Test out the code by pressing the Run button. What happens?
Modify the starter code to change the behaviour:
bgcolor('whitesmoke') color('blue', 'pink') height = 100 width = 200
The first two lines effect the colour of the shape. Click here to see other colour options.
The second two lines effect the size of the shape. That happens if you swap the two numbers?
The current program has two sections of code that are identical:
forward(width) left(90) forward(height) left(90)
Instead of repeating this, you can write a new line of code, to ask the computer to do it twice:
for i in range(2): forward(width) left(90) forward(height) left(90)
The number at the end of that first line range(2)
, is how many times you want the code to repeat.
Edit your code to add this loop, instead of having the code written twice.
Test your code - does it still work exactly the same way as it did before?
Now you have drawn a single rectangle, and the turtle is back where it started.
To draw a second rectangle above the one you just drew, you need to move the turtle.
What would the turtle need to do to move to here:
To move to the next location, the turtle needs to:
left(90)
, so that it is pointing up.forward
by the height
of the rectangle, to get to the top corner.right(90)
so that it is pointing in the correct direction.We can also add a couple of extra lines to make the turtle stop drawing while it moves.
The code for all of this is:
penup() left(90) forward(height) right(90) pendown()
Add the new code underneath everything that is in your program so far.
Test it - does your first rectangle still get drawn, and is the turtle in the right place?
We can add a second rectangle now that the turtle is in the right place.
If we want the second rectangle to be the same size as the first one, but a different colour, we need to write the following algorithm:
All of this code is already in your program from the first rectangle - you can copy and paste it underneath everything that is already there.
An example of this code is here.
Most flags have at least two rectangles! Choose a flag to draw, and think about the algorithm you need to draw it.
For example, the Austrian flag:
An algorithm for this could be:
Have a go yourself!
Now that you know how to draw and combine rectangles, you can design your own flag.
By changing the angle that the turtle turns, you can create different shapes, such as triangles.
You can also draw circles by using circle(100)
- try it out!
The links below give you some other ideas of things you could add to your program.