Conditional Statements: Nesting, Debugging, and Beyond (Part 2)| Tutorial

Alphabets Sounds Video

share us on:

In this lesson, we expanded on conditional statements by enhancing our fish game, allowing the pufferfish to react when it touches the fish. We learned to debug our code by placing conditional statements inside loops and using nested “if then else” statements to manage multiple reactions based on distance and interactions. Additionally, we explored various creative applications of conditional statements, such as scoring systems and interactive animations, highlighting the versatility of coding in game development.

Conditional Statements: Nesting, Debugging, and Beyond (Part 2)

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.

Debugging the Pufferfish

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.

Adding More Reactions

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.

Fixing the Order

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!

Exploring More Possibilities

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!”

Endless Creative Ideas

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?

  1. Reflect on the debugging process described in the article. How do you approach debugging in your own projects, and what strategies do you find most effective?
  2. The article discusses the concept of nesting conditional statements. Can you think of a real-life scenario where nested conditions might be necessary? How would you apply this concept?
  3. Consider the importance of the order of conditional statements as highlighted in the article. Have you ever encountered a situation where changing the order of conditions solved a problem? Share your experience.
  4. The article mentions using conditional statements to create a game with points. How would you design a simple game using conditional statements, and what unique features would you include?
  5. Explore the idea of using the “touching color” condition in a project. What creative application can you think of for this feature, and how would it enhance the user experience?
  6. Reflect on the endless possibilities of using conditional statements in various projects. What is a project you have in mind that could benefit from using conditional statements, and why?
  7. Think about the concept of “repeat until” or “wait until” statements. How might these be useful in a project you are currently working on or planning to start?
  8. The article encourages creativity with conditional statements. What is a unique or unconventional way you could use conditional statements in a project, and what inspired this idea?
  1. Debugging Challenge

    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?

  2. Nesting Adventure

    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.

  3. Order Fixing Race

    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.

  4. Creative Coding

    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!

  5. Game Expansion

    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.

ConditionalA 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.

StatementsInstructions written in a programming language that tell the computer what to do. – The program has several statements that control the character’s movement.

PufferfishA 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.

FishA 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.

CodeA 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.

DistanceThe 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.

WinkA 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.

AnnoyedA 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.

GameAn interactive program designed for entertainment, often created using coding skills. – We used Scratch to create a fun game where you catch falling stars.

PixelsThe 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.

All Video Lessons

Login your account

Please login your account to get started.

Don't have an account?

Register your account

Please sign up your account to get started.

Already have an account?