The final lesson where students combine loops, events, and sensing to build a simple interactive game.
Have you ever wondered how your favorite video game knows exactly when you've touched a secret coin or reached the finish line? Today, you become the architect who builds those rules!
Every great game needs a goal. Without a goal, a game is just a digital playground! In coding, we create a goal by telling the computer what to look for. This is called a Win Condition. For example, your goal might be for a cat sprite to touch a glass of milk. To make this work, we use a Sensing Block inside an If-Then Block. The computer constantly asks: 'Is the cat touching the milk?' If the answer is 'Yes,' then something awesome happens, like a 'You Win!' message appearing on the screen.
Let's make a sprite win when it touches a treasure chest: 1. Drag out a Forever loop. 2. Place an If-Then block inside it. 3. Put a Touching [Treasure Chest] sensing block in the 'If' slot. 4. Inside the 'Then' space, add a Say [I found it!] block.
Quick Check
What do we call the specific rule that tells the computer how a player wins the game?
Answer
A Win Condition.
How do we remember how many coins a player has collected? We use a Variable! Think of a variable as a labeled bucket that holds information. We can name our bucket 'Score.' At the start of the game, the bucket is empty, so we set the score to . Every time the player touches an item, we drop a point into the bucket. In block coding, we use the block Change [Score] by [1]. This tells the computer to take the current number in the bucket and add to it: .
Follow these steps to build a scoring system: 1. Create a new variable named Score. 2. At the very start (When Green Flag Clicked), use Set [Score] to [0]. 3. Create a loop that checks if the player is touching a 'Coin' sprite. 4. If they are touching, use Change [Score] by [1] and then make the coin Hide so it can't be collected twice!
Quick Check
If your current score is and you use the block 'Change Score by 2', what is your new score?
Answer
7
The final step of game design is Playtesting. This is when you let someone else play your game to see if it is too easy, too hard, or if there are any 'bugs' (mistakes in the code). When you share your game, you are looking for Feedback. A good designer asks: 'Was the goal clear?' or 'Did the score work correctly?' Sometimes, you might find that you need to change your code to make the game more fun. This process of testing and fixing is how professional game studios like Nintendo or Mojang make their hits!
Combine everything to make a 'Level Up' feature: 1. Create a script that checks if Score > 10. 2. If the score is greater than , use the Switch Backdrop block to move the player to a new, harder level. 3. Add a Play Sound block to celebrate the level up!
Which block is best for checking if two sprites are bumping into each other?
Why should you 'Set Score to 0' when the Green Flag is clicked?
A 'Variable' can only hold numbers and cannot be renamed.
Review Tomorrow
Tomorrow morning, try to explain to a family member what a 'variable' is using the 'bucket' analogy.
Practice Activity
Open your project and try to add a 'High Score' variable that only changes if the current score is higher than the old one!