Scary Spot the Difference - Python
In this resource you will make a Spot the Difference game with a difference! While the player is focused intently on the screen trying to figure out the difference between two images, they'll have no idea they're about to receive the fright of their life.
If you would like to make this project using Scratch instead of Python, head over to the Scratch worksheet.
Gathering the assets
You're going to need two images and a sound file for this activity. Firstly, you'll need a Spot the Difference image. This one is free to use, but you can make or find your own if you prefer.
Next, you'll need the image you're going to swap out to give them a fright. This zombie face is cool, but you can make or find your own if you like. Just remember the age of the people you're trying to scare and choose something appropriate.
Lastly, you'll need a scary sound to really give them a fright. You can download this one, which should do the trick. Make sure you download the
wavversion of the file, or again find your own. Once you have the file, rename it to
- Save all the files in a single directory, where your Python script will be.
Setting up your file
Open IDLE by clicking on Menu> Programming > Python 3 (IDLE), then click File and New File.
Save your file straight away, in the same directory as your images. If you call it
run_me.py, you won't be giving the game away to your victim.
To begin with, you'll need to import the Pygame library and some of its modules. You're also going to need the
import pygame from pygame.locals import* from time import sleep from random import randrange
Next, you need to initialise Pygame so that it's ready to be used. As different monitors have different sizes, you need to find out the width and height of your monitor, and save them as variables.
pygame.init() width = pygame.display.Info().current_w height = pygame.display.Info().current_h
Lastly for this section, you can instruct Pygame to create a full-screen window for the game to be played in, and then get Pygame to quit.
screen = pygame.display.set_mode((width, height), FULLSCREEN) pygame.quit()
Save your file again and then run it by pressing
- You should see a blank rectangular window open. This is the Pygame window. It should close itself straight away, as your program reaches the
Displaying an image
To display an image in Pygame, you first need to load the image. You can start with the Spot the Difference image. Place this line above the
pygame.quit()line. If you've used a different image, don't forget to change the name.
difference = pygame.image.load('spot_the_difference.png')
The image may be too big or small for your monitor, so the next step is to scale it using the
heightvariables you created earlier.
difference = pygame.transform.scale(difference, (width, height))
These lines have just loaded the image into the Raspberry Pi's memory. To display them on the screen, you need to 'blit the image onto the window, and then update the display to show it.
screen.blit(difference, (0, 0)) pygame.display.update()
This line blits the image to the coordinates
x = 0and
y = 0; that is, the top-left corner of the image is being placed in the top-left corner of the window.
Save and run the file again to see the image displayed.
You might find that the image doesn't display for long enough for you to see it, so you can add a small
sleepto the end of the program, just before Pygame quits.
screen.blit(difference, (0,0)) pygame.display.update() sleep(3) pygame.quit()
To switch the image to the scary one, you need to add a pause and then re-blit another image.
First, you load the scary image into memory and scale it in the same way you did before with the
zombie = pygame.image.load('Scary_Face.png') zombie = pygame.transform.scale(zombie, (width, height))
Then get your program to pause for a few seconds.
Then you can blit the image to the display, and update the display.
screen.blit(zombie, (0,0)) pygame.display.update()
Save and run the program again, to see the new image being placed.
It's a little predictable at the moment, so you can add some randomness by changing the
sleeptime between the two images to a random number.
Adding some sound
Now it's time to add the scream, to make the game a little scarier. In Python, just like you did with the images, the sound needs to be loaded first. You want to do this before the second image is displayed. Change your code so it looks like this:
zombie = pygame.transform.scale(zombie, (width, height)) scream = pygame.mixer.Sound("scream.wav") sleep(randrange(5,15))
Then, you want to start playing the sound just before the second image is shown, as soon as the sleep has finished.
Next, stop the scream just before Pygame quits:
- You might find that, depending on your Raspberry Pi model, the scream takes a little time to start playing. You could, therefore, add a little sleep just after the
Your complete code should look like this:
import pygame from pygame.locals import* from time import sleep from random import randrange pygame.init() width = pygame.display.Info().current_w height = pygame.display.Info().current_h screen = pygame.display.set_mode((width, height), FULLSCREEN) difference = pygame.image.load('spot_the_diff.png') difference = pygame.transform.scale(difference, (width, height)) screen.blit(difference, (0,0)) pygame.display.update() zombie = pygame.image.load('scary_face.png') zombie = pygame.transform.scale(zombie, (width, height)) scream = pygame.mixer.Sound("scream.wav") sleep(randrange(5,15)) scream.play() sleep(0.4) screen.blit(zombie, (0,0)) pygame.display.update() sleep(3) scream.stop() pygame.quit()
Pranking your friends
- Now all you need to do is have a friend come and try out your Spot the Difference game. Tell them they have a minute to find as many differences as they can, and then watch them jump out of their skins when the scary image appears!
- Could you make the game more realistic? Have a look at the Pygame website and see if you can learn how to capture mouse clicks from your victim.
- Could you make the game could keep a fake score of differences spotted before the scary face jumps out?