Create a Locker in Scratch: Step-by-Step Guide
Welcome to the world of creative coding! In this step-by-step guide, we'll show you how to create a functional locker system in Scratch, a popular online platform for coding and game creation. This project will not only help you learn Scratch programming but also develop your problem-solving skills. So, let's dive in!
Before we start, ensure you have a Scratch account. If you're new, sign up at scratch.mit.edu and familiarize yourself with the interface. We'll be using blocks of code to create our locker, so don't worry about typing out complex commands!
designing the locker
The first step in creating our locker is to understand what we want it to do. Here's a simple plan:



- We'll create two sprites, one for the locker and one for a padlock.
- The locker will start closed, and the padlock will be over it.
- Clicking the green flag will start the game. When you click the locker, it will open, and the padlock will move away.
- Clicking the locker again will close it, and the padlock will return.
Now, let's turn this design into reality.
Creating the Sprites
First, let's create our sprites. Click on the 'Choose a Sprite' button and select 'Make a Sprite'. Here's what to do:



- Draw a simple rectangle for the locker and a small circle for the padlock. Give them suitable costumes (colors).
- Change the locker's starting position to -140 (x) and -60 (y), and the padlock's to -160 (x) and -80 (y).
Your lobby should now look like a closed locker with a padlock over it.
Scripting the Locker
Next, let's add code to our locker sprite. Click on it, then click on 'Events' > 'When green flag clicked'. Add these blocks:
- Set 'Locker v' to 0 (this will control the locker's state - 0 for closed, 1 for open).
- Show the padlock.
The locker should now start closed with the padlock over it when you click the green flag.






Now, let's add clicking functionality to the locker. Click on 'Events' > 'When this sprite clicked'. Add these blocks:
- If 'Locker v' is 0, change 'Locker v' to 1 and hide the padlock. Otherwise, change 'Locker v' to 0 and show the padlock.
- Change the locker's costume based on 'Locker v'.
Your locker should now open when clicked and close when clicked again, moving the padlock in sync.
Adding a locker combination
Let's make our locker more interesting by adding a combination. Here's our new plan:

- We'll add numbers 1-3 to our locker sprite. Clicking these numbers will change the combination input.
- The correct combination will be 123. If the user guesses it, the locker will open.
Sounds fun? Let's code it!
Adding Numbers to the Locker
Change your locker's costume to include numbers 1, 2, and 3. Make them look like buttons. Then, add these scripts:
- Under 'Events' > 'When this sprite clicked', add blocks to change 'Number v' (a variable to keep track of the combination attempt) based on which number is clicked:
- For 1, change 'Number v' to 1 * 100 + (Number v mod 10 + 1)
- For 2, change 'Number v' to 1 * 100 + (Number v mod 10 + 2)
- For 3, change 'Number v' to 1 * 100 + (Number v mod 10 + 3)
Now, clicking the numbers will change the combination attempt.
Implementing the Combination Mechanism
Add these blocks to 'When green flag clicked' script:
- Set 'Number v' (our combination attempt variable) to 0.
- Repeat 'forever':
- If 'Number v' equals 123, broadcast 'Open Locker' and stop the script.
Now, when the correct combination is entered, the locker will open.
Finally, add these blocks to the locker's 'When I receive Open Locker' event:
- Set 'Locker v' to 1 (to open the locker).
- Hide the padlock.
And that's it! You've created a functional locker with a combination in Scratch. Now, go ahead and show it off to your friends or create more complex projects.
Happy coding! Remember, the key to mastering Scratch (and any other skill) is practice. Keep creating and exploring!





