Motion Using Conditional Logic

Working with Logic

The best way to develop an understanding of programming concepts is to practice them. You’re going to build out a motion system for your game using conditional statements and logical operators. At this point the motion in the project progressed from being static to dynamic with the inclusion of player input, but now you’re going to integrate a series of logical checks to power the motion. It will still result from player input, but now Scratch will check for a series of conditions that need to be met before the motion occurs.

Logic Driven Motion

One of Scratch’s strengths for education is that it naturally supports scaffolding for developing an understand of programming concepts. Right now the player motion relies on Scratch’s Event blocks. You’ve used the “when [ ] key pressed” block to launch the script. Essentially, this block is a condensed version of a conditional statement that does the following:

Pseudo code :

The next activity is to restructure your player motion so that you’re constructing the if/then statements instead of the Event block.

Motion Example

Take a look at this screenshot of a motion script. In this example, a friendly ghost sprite moves in different directions based on the user input. As a bonus, he changes colors based on which way he’s heading. Let’s examine the image:

IntGames - Logic Motion Example 1

Code Breakdown:

Let’s examine the script:

  1. The top block is again the gameStart Hat block. Once the Start flag is clicked, Scratch will begin monitoring for the actions contained in the script.
  2. Next is the forever loop. In a script like this that is essentially the motion engine, Scratch must be told to check for the conditions constantly. Without the forever loop, it would check a single time at the beginning of the game and then stop. Considering this is the motion engine of the game, that wouldn’t be effective.
  3. Next are the actual conditional statements. There are 4 in total, each governing a direction of movement. Let’s look further at the structure of the if/then statements:
    • In between the if and the then you’ll notice the condition that is being evaluated.
    • In this case, it takes an OR logical operator (the green block) that itself contains two additional Boolean expressions that are checked.
    • On either side of the OR are the Boolean expressions. You’ll be using the “key [ ] pressed” expression from the Sensing category.
  4. The script evaluates if either key is pressed. If the value returns as true (the key has been pressed) then the code executes.
  5. The code within the if/then is a quick costume change and a motion command that corresponds to the keypress. For example, to move left, the sprite’s x-value is changed by -10.
  6. This code pattern is then repeated for all the different movements for the sprite.

Benefit of Logic Driven Motion

In this example there are 2 different keypresses for each directional move. This is to provide the option for players to use either the arrow keys or the common w,a,s,d movement. This can be condensed into the 4 conditional statements in the above example whereas if you were to do the same thing using the “when [ ] key pressed” block, your code would be much more cluttered than necessary. Additionally, the use of the relational operators allows for combinations of keypresses that can result in more advanced actions.

Take a look at this example:

IntGames - Logic Example 2

This script combines 2 relational operators to monitor for a specific keypress combination. Combining operators can be used for several different things in your projects, especially once you begin to work with variables.

Activity: Animate Your Sprite With Logic

Change your motion script for your player sprite to include costume changes and conditional logic.

Steps:

  1. The resource pack includes two key frames for your sprite: resting and walking.
  2. Click on your sprite, and then open the Costumes tab.
  3. Upload a new costume from the resource pack. You should now have both the resting and walking images for the sprite.
  4. Replace your directional input to include the conditional statements. Look at the example if you’re stuck!
  5. Make sure to use the appropriate values for each direction.
  6. Test out your new motion engine!

Related Posts