Project 265: Gravity Dodger

8. Make Hero Movement Dynamic

We want to have our hero where it moves back & forth to avoid enemies. What axis of coordinates are we going to modify for the hero?
Starting point file for this challenge

Your goal

Steps

1. Initializing Hero X Speed

Select the "set ___ to ____" block from the Variable Tab, and set the "x speed"  variable to zero after we move to the starting point.

2. Select the Proper Loop Type for the Hero

For our hero movement, do we want our hero to always be able to accelerate? Do we want our hero to only move for a period of time? Do we want our hero to move until a specific condition is met?

3. Create Variable Based Controls

Add the "forever" loop below the variable set.  Remember we want our hero to be able to move back / forth to avoid the enemies. So we'll use the "change x by ___"  block.   Then let's add the "x speed" variable to the "change x by ___"  block.

4. Adding Left Acceleration

Grab two "if ____ then _____" statements, and stack them inside of each other.  This is called a Nested If Statement.  It's important because it allows us to test two different things before we run the "change x speed by 1" code.   The first condition is whether we used the left arrow key, or "key left arrow pressed?" from Sensing. We'll fill in the second next.

5. Fix Left Movement and Duplicate

Now that we have the Left Movement, let's fix the change variable block we have.  Recall if we want to go left, we will need to move in the - X  direction.  So we should change by -1.   Now we are ready to add the second condition.  Since we don't want our character to move too fast, grab an "____ < _____" block form the Operators Tab.  We'll add -10 as the fastest we can go left, and add the "x speed" variable to test that it is always bigger than that.

Finally we'll right click on the first If Statement to duplicate it, and place it below the first set. 

6. Create right movement

Ok now we'll switch the second "key left arrow pressed?" block to a "right arrow"  and flip the order of the operator conditions so it reads:  "x speed" < 10.  Lastly adjust the change variable tab so that the "x speed" is increased by 1 instead.

7. Test Your Hero Code

Whenever we go through a big block of code, we want to make sure that everything is working as intended. Does your hero move back and forth using arrow keys?  If you want try to update it to other keys on your keyboard.