The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 5 Programming with Python 109

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


Project 2: Scary Spot the Difference
Python can also handle pictures and sounds as well as turtle-based graphics, which can be
used to great effect as a prank on your friends – a spot-the-difference game with a scary
secret at its heart, perfect for Halloween!


This project needs two images – your spot-the-difference image plus a ‘scary’ surprise
image – and a sound file. Click on the raspberry icon to load the Raspbian menu, choose
the Internet category, and click on Chromium Web Browser. When it has loaded, type
rpf.io/spot-pic into the address bar followed by the ENTER key. Right-click on the picture
and click on ‘Save image as...’, choose the /home/pi folder, then click Save. Click back on
Chromium’s address bar, then type rpf.io/scary-pic followed by the ENTER key. As before,
right-click the picture, click ‘Save image as...’, choose the /home/pi folder, then click Save.
For the sound file you’ll need, click back into the address bar and type rpf.io/scream
followed by the ENTER key. This file, the sound of a scream to give your player a real
surprise, will download automatically – but it will need to be moved before you can use it.
Click on the raspberry icon, find the Accessories category, and click on File Manager. In the
folder list, double-click on Downloads. Find the file scream.wav within this folder, right-click
on it, and then click Cut. Click on pi in the folder list to the left, then right-click on a blank
space in the main window area before clicking Paste. You can now close the File Manager
and Chromium.
Click the New icon in the Thonny toolbar to begin a new project. As before, you’re going
to be using a library to extend Python’s capabilities: the Pygame library, which as the name
suggests was created with games in mind. Type the following:


import pygame

You’ll need some parts of other libraries, and from a subsection of the Pygame library, too.
Import these by typing the following:


CHALLENGE: WHAT NEXT?
Can you use your commented-out instruction to have the
branches of the snowflake drawn in different colours? Can
you create a ‘snowflake’ function, and use it to draw lots
of snowflakes on the screen? Can you have your program
change the size and colour of the snowflakes at random?

ONLINE PROJECT
This project is also available online at rpf.io/scary-spot
Free download pdf