In our first lesson about conditional statements, we learned what they are and even started making a fun fish game! We coded a fish to react when it touches a pufferfish. Now, let’s make the pufferfish do something cool too. We’ll add code so that the pufferfish changes its look and winks when it touches the fish.
Hmm, the fish is reacting, but the pufferfish isn’t doing anything. Let’s figure out why. The problem is that when we start the game by clicking the green flag, the fish and the pufferfish aren’t touching. Since they aren’t touching, the condition isn’t true, and the code stops running. To fix this, we need to put our conditional statement inside a loop. This way, the game will keep checking if they’re touching.
Now, let’s make the game even more interesting. We want the pufferfish to make a warning face when the fish gets close, and then wink when they touch. To do this, we’ll use something called “nesting.” We’ll put one “if then else” statement inside another one. The first one will check how far apart the fish and pufferfish are.
If the distance is less than 150 pixels, the pufferfish will show an annoyed face. But wait, we’re not seeing the wink! Why not? It’s because the program first checks if the distance is less than 150 pixels. If it is, it shows the annoyed face, even when they’re touching. So, it never gets to the part where it shows the wink.
To fix this, we need to change the order of our statements. First, check if the fish and pufferfish are touching. If they are, show the wink. If not, move to the “else” part, which checks the distance. If the distance is less than 150 pixels, show the annoyed face. This way, everything works perfectly!
What else can we do with conditional statements? We can use them to make a game where you earn points when certain conditions are met. For example, we could use a “repeat until” or “wait until” statement to check the score and see if the player has won.
We can also use the “touching color” condition. Imagine using the eyedropper tool to pick the pink color of coral in the background. Then, we can add code so that when the fish touches the pink coral, it changes to an eating costume and says, “Yum!”
Conditional statements can be used in many different projects. You could make a maze game where the sprite can’t walk through walls by using a condition that senses the color of the maze walls. Or create a fun animation where a character is searching for cheesy puffs, and new text appears when they find them. The possibilities are endless. What will you create?
Try to find out why the pufferfish isn’t reacting in your game. Run the game and observe what happens. Then, use a loop to keep checking if the fish and pufferfish are touching. Can you fix the code so the pufferfish reacts?
Use nested “if then else” statements to make the pufferfish show different faces. First, make it show an annoyed face when the fish is close. Then, make it wink when they touch. Experiment with the order of your statements to see how it affects the game.
Reorder the conditional statements so that the pufferfish winks when touching the fish and shows an annoyed face when the fish is close but not touching. Test your changes to ensure everything works perfectly.
Think of a new reaction for the pufferfish when it touches the fish. Use your imagination to add a new condition and see what fun interaction you can create. Share your idea with the class!
Use conditional statements to add a new feature to your game. For example, make the fish earn points when it touches a certain color or object. How many points can you collect?
Here’s a sanitized version of the provided YouTube transcript:
—
In our first video on conditional statements, we explored what they are and practiced creating an interactive project. We started working on a fish game and coded one sprite (a fish) to react when it touches another sprite (the pufferfish). Now, let’s add some code to the pufferfish so that it changes costumes and winks when it touches the fish.
Hmm. The fish is reacting, but the pufferfish isn’t. Let’s debug what’s happening here. The issue is that, as soon as I hit the green flag, the fish and the pufferfish weren’t touching. Since the condition “touching” wasn’t true, it stopped checking and ended the script. I need to remember to put my conditional statement inside a loop to create a conditional loop. I want it to continually listen to see if they’re touching.
Now, let’s say I want to add code so that as the fish approaches the pufferfish, the pufferfish makes a warning face, like an annoyed expression. When they finally touch, then the pufferfish winks. I’m going to nest my original “if then else” conditional statement inside another “if then else” statement that checks the distance between the pufferfish and the fish. I’m nesting it under “else.”
Now, the program says if the distance is less than 150 pixels, then show the annoyed pufferfish face. Hmm. Notice that I’m not getting the winking face at all. Why is that? Well, the way this program is currently written, it’s first checking to see if the distance between the sprites is less than 150 pixels. If that’s true, it shows the annoyed costume. Because this condition is true when they’re touching, it never moves on to the code in the “else” statement. The order of the nested statements can really make a difference.
By switching the order, so the first conditional statement checks if they are touching, when the sprites are touching, that statement is true and it shows the wink. When it is false and they aren’t touching, it moves on to the “else,” which is our second conditional statement: checking the distance between the sprites. If the distance is less than 150 pixels, that statement is true and we see the annoyed face.
Where can I go from here? Well, I could use additional “repeat until,” “wait until,” or “if then” conditional statements to create a game where a conditional statement assigns points if a condition is met and checks the score to see when the player has won. I could experiment with different conditions. For instance, I could use the “touching color” condition in another nested “if then” statement and use the eyedropper tool to select the pink of the coral in the backdrop. Then, I can add code so that when the fish is touching the pink of the coral, it displays an eating costume and says, “Yum!”
Conditional statements can be used in a wide variety of projects. For example, I could use that same “touching color” condition in a maze game, where a conditional statement reverses a move when sensing the color of the maze walls, preventing a sprite from walking through them. Or I could use a conditional statement in a fun animation, where a character is looking for cheesy puffs. The conditional statement triggers new text when they finally touch. The possibilities are endless. What will you create?
—
This version maintains the original content while ensuring clarity and coherence.
Conditional – A command in programming that runs only if certain conditions are true. – If the player collects all the coins, the conditional code will make the door open.
Statements – Instructions written in a programming language that tell the computer what to do. – The program has several statements that control the character’s movement.
Pufferfish – A type of fish that can inflate itself, often used in coding games as a character or obstacle. – In the game, the pufferfish blocks the path until you find the key.
Fish – A common character or object in coding games, often used to teach loops and animations. – The fish swims across the screen when you press the spacebar.
Code – A set of instructions written in a programming language to make a computer perform tasks. – We wrote code to make the robot move forward and turn left.
Distance – The amount of space between two points, often calculated in coding to determine how far objects are from each other. – The game calculates the distance between the player and the treasure chest.
Wink – A quick closing and opening of one eye, often used in coding animations to make characters more lifelike. – The character will wink when you click on its face.
Annoyed – A feeling of slight anger, sometimes used in coding to describe a character’s reaction to an event. – The cat looks annoyed when you try to move it without permission.
Game – An interactive program designed for entertainment, often created using coding skills. – We used Scratch to create a fun game where you catch falling stars.
Pixels – The tiny dots that make up the images on a computer screen. – The character is made of pixels, and you can change its color in the game editor.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |