Science Oxford Logo

CCC - Digital Flag Design

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.


Getting started

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?


Add a 'for loop'

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?


Move the turtle

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:

left: turtle on bottom left, facing right; centre: arrow pointing to next picture; right: turtle on top left facing right

To move to the next location, the turtle needs to:

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?


Add a second rectangle

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:

  1. Set the colour of the new rectangle.
  2. Start the fill.
  3. Draw the new rectangle.
  4. End the fill.

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.


Write an algorithm for your chosen flag

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:

Austrian flag - vertical stripes red white red

An algorithm for this could be:


Have a go yourself!


What next?

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.