This is a tutorial on how to make a simple slot machine video game on Scratch.
Preparation time for facilitator
Time needed to complete activity (for learner)
Name of author
Support material needed for training
Resource originally created in
We will program a slot machine game. Three objects will remain on screen but change constantly. The player will need to press space at the moment the three objects are identical.
Note : in the version presented here, the code is simple and can be show to an audience who is not familiar with Scratch or block programming. However, for the more advanced version which can be played several times consecutively (cf. section “Going further”), it would be best to already have experience with the platform.
Creating the backdrop
Click on the backdrop icon on the bottom right of the creation screen.
Choose one from the library.
Adding a sprite and two costumes
For the duration of the game, there will only be three sprites visible on the screen.
You will need to make it so that they will change appearance by using costumes.
Given that the three sprites will be identical, you will only need to program one and then copy it twice.
Add a sprite by clicking on the icon near the bottom right of the screen. Choose the default costume from the library, for example ‘Basketball’.
Click on the ‘Costumes’ tab on the top left of the screen and choose two extra costumes by clicking on the sprite icon, this time on the bottom left.
In the library, choose two costumes, for example ‘baseball’ and ‘beachball’. Tip: choosing costumes with similar appearances makes the game more challenging.
Spinning the reels
In a real slot machine, the three reels will turn and show one image at a time. We will want to imitate this movement by ‘turning’ one costume to another (with only one being visible at a time).
To do this, write the following to the ball’s script :
When the player clicks the green flag, the sprite changes its costume and waits 0.5 seconds. This repeats indefinitely. Click on the green flag to test the result. Adjust the waiting time value to accelerate or decelerate the movement.
Pulling the lever
With a real slot machine, the lever is pulled to stop the reels. Here, the player must push on the space bar to stop the ‘rotation’ of the costumes. Here is the simplest solution to make this happen : when the bar is pressed, the game stops.
Add this code to the script :
Click the green flag to test.
Randomising the costumes
The test will show that the costumes always changes in the same order. This means that if we copy the ball, each copy will always change costumes at the same time and in the same order. The player will be able to stop the rotation whenever and they will always win! We will therefore need to randomise the changes.
Modify the ball’s script to use a random number instead of ‘next costume’, as such :
When the player clicks on the green flag, the sprite starts its animation randomly with one of the three costumes. It will then indefinitely repeat the cycle of waiting 0.5 seconds and then changing.
As said above, we will need to copy the sprites in order to have three instead of just one.
Right click on the sprite and choose ‘duplicate’.
Now do it a second time.
Click on the green flag to test the game. If you stop the game at the point where three sprites look identical, congratulations, you win !
Here are some ways to improve the game
- Accelerate the speed of the costume changes
- Adding costumes
- Adding costumes only one or two sprites display
- Add music
As we are now using ‘stop all’ to stop the rotation of the costumes, the game cannot continue after one round (Scratch stops). How can we do this differently so the game doesn’t stop? Find a way to do it* and you will then be able to add the following mechanics:
- a way to restart the rotation to play several times in a row
- add a point counter which will increase when the player stops at the right moment and decreases otherwise
*Need a hunt? There are (at least) two possible solutions. One uses a variable, another a message…