Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Projects
Modern Art
Code your own computer-generated modern art.
Step 1 Introduction
In this project you will create computer generated modern art. You will use functions to write code that you can use over and over
again.
Additional information for club leaders
If you need to print this project, please use the Printer friendly version (https://projects.raspberrypi.org/en/projects/modern-
art/print).
1 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Club leader notes
Introduction:
This project introduces functions through a colourful modern art generator. Functions are used to package useful turtle graphics
code which can then easily be used to create funky art.
Online Resources
This project uses Python 3. We recommend using trinket (https://trinket.io/) to write Python online. This project contains the
following Trinkets:
‘Modern Art’ starting point – jumpto.cc/modern-go (http://jumpto.cc/modern-go)
There is also a trinket containing a sample solution to the challenges:
‘Modern Art’ Finished – rpf.io/modern-�nished (https://rpf.io/modern-�nished)
O�ine Resources
This project can be completed o�ine (https://www.codeclubprojects.org/en-GB/resources/python-working-o�ine/) if
preferred. You can access the project resources by clicking the ‘Project Materials’ link for this project. This link contains a ‘Project
Resources’ section, which includes resources that children will need to complete this project o�ine. Make sure that each child has
access to a copy of these resources. This section includes the following �les:
modern-art/modern-art.py
modern-art/snippets.py
You can also �nd a completed version of this project’s challenges in the ‘Volunteer Resources’ section, which contains:
modern-art-�nished/modern-art.py
(All of the resources above are also downloadable as project and volunteer .zip �les.)
Learning Objectives
Functions;
This project covers elements from the following strands of the Raspberry Pi Digital Making Curriculum (http://rpf.io/curriculum):
Combine programming constructs to solve a problem. (https://www.raspberrypi.org/curriculum/programming/builder)
Challenges
Turtle art - de�ne a new function to complete the turtle art generator.
More modern art - create a new function that calls other functions to generate modern art.
Frequently Asked Questions
To avoid having to wait for earlier code to run when adding to the project children can comment out code using a ‘#’ at the
beginning of a line.
If they do want all of their code to run then clear() can be used to clear the screen.
2 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Project materials
Project resources
.zip �le containing all project resources (http://rpf.io/p/en/modern-art-go)
Online Trinket containing ‘Modern Art’ starter resources (http://jumpto.cc/modern-go)
Club leader resources
.zip �le containing all completed project resources (http://rpf.io/p/en/modern-art-get)
Online completed Trinket project (https://trinket.io/python/47bbc2fc2b)
3 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Step 2 Random colours
Open this trinket: jumpto.cc/modern-go (http://jumpto.cc/modern-go).
You can set the colour of a turtle by saying how much red, green and blue you would like from 0 to 255.
Add the following code to get a purple turtle:
Purple is made by mixing together red and blue.
Try some di�erent numbers to get di�erent colours.
Remember each number can be from 0 to 255.
How about choosing a random colour?
Update your code to choose a random number between 0 and 255 for the red, green and blue values:
Click ‘Run’ a few times to get di�erent coloured turtles.
That’s fun, but it’s a lot to remember and type every time you want to set a turtle to a random colour and it’s not very easy to read.
In Python we can write def to de�ne a function that we can call whenever we need to set the turtle to a random colour.
You’ve been calling functions already, color() and randint() are functions that have been de�ned for you.
Let’s put the random colour code into a function using def:
Make sure you indent the code inside the function. Functions are usually placed at the top of the script after the imports.
If you ‘Run’ your code now you don’t get a random coloured turtle. That’s because you have de�ned your function, but not called it
yet.
Add a line to call your new function:
4 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Notice that your new code is much easier to understand because the complex part is in the function. It’s easy to work out what
randomcolour() does.
5 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Step 3 Random place
Let’s create another function to move the turtle to a random place on the screen. The center of the screen is (0,0) so we’ll place turtles
in a square area around the centre.
Add a randomplace() function:
Try your new function by calling it and then calling stamp(), you can call it more than once:
Ooops, the turtle draws when it moves. Let’s put the pen up at the beginning and down at the end so that the turtle doesn’t draw
while it’s moving:
Did you notice that you only had to ‘�x’ the code in one place? That’s another good thing about functions.
Now test your code a few times.
6 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Step 4 Challenge: Turtle art
Can you de�ne a randomheading() function that will make the turtle point in a random direction and make the following code work?
Hints:
setheading(<number>) will change the direction the turtle is facing in.
<number> should be between 1 and 360 (the number of degrees in a circle)
You can use randint(1, 360) to choose a number between 1 and 360.
7 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Step 5 Create rectangle modern art
Now let’s create some modern art by drawing lots of rectangles of di�erent sizes and colours.
First add the following code to the bottom of your script, after your challenge code, to clear the screen after your turtle art and
point the turtle in its usual direction:
You can comment out your turtle art code by placing a # at the beginning of each line so that it doesn’t run while you are working
on rectangle art. (Then you can uncomment it later to show o� all of your work.)
Now let’s add a function to draw a random-sized, random-coloured rectangle at a random location!
Add a drawrectangle() function after your other functions:
Look in snippets.py for some helper code if you want to save some typing time.
Add the following code at the bottom of main.py to call your new function:
Run your script a few times to see the height and width change.
The rectangle is always the same colour and starts at the same location.
Now you’ll need to set the turtle to a random colour and then move it to a random place. Hey, didn’t you already create functions to
do that? Awesome. You can just call them from the beginning of the drawrectangle function:
8 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Wow that was a lot less work, and it’s much easier to read.
Now let’s call drawrectangle() in a loop to create some cool modern art:
Gosh that was a bit slow wasn’t it! Luckily you can speed the turtle up.
Find the line where you set the shape to ‘turtle’ and add the highlighted code:
speed(0) is the fastest or you can use numbers from 1 (slow) to 10 (fast.) Experiment until you �nd a speed you like.
9 of 10 06/29/2020 04:44 AM
Modern Art | Raspberry Pi Projects https://projects.raspberrypi.org/en/projects/mode...
Step 6 Challenge: More modern art
Can you create a function that draws a shape and calls your randomcolour() and / or randomplace() functions?
You can call your function from inside a for loop as you did in the rectangle art to generate modern art.
Ideas:
Turtles have a function called dot that takes a radius (distance from the centre to the edge of the circle) as input. E.g. turtle.dot(10)
You could create a drawcircle() function that draws a circle with a random radius.
Look in snippets.py for example code to draw stars with the turtle.
Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons
license (https://creativecommons.org/licenses/by-sa/4.0/).
View project & license on GitHub (https://github.com/RaspberryPiLearning/modern-art)
10 of 10 06/29/2020 04:44 AM