Lesson 13

advertisement
Lesson 13
Realistic Turning Movements
Pre-Requisites: Lesson 12
Topics:
1. If/Else Statement
2. Comparisons
3. Nesting If/Else Statements
Introduction:
In the previous lesson we made it so that our car cannot turn when it is not moving. This made
our game follow real world rules and more realistic. However, our game is still not quite like the
real world. Try playing your world and turning the car while going backward. If you look
carefully you will notice that the car is turning the wrong direction with respect to the front tires.
Today we will fix this.
Realistic Car Turning:
Recall that the speed variable remembers which direction the car is moving. If it is positive, then
the car is moving forward. If it is negative, then the car is moving backward. If it is zero, then the
car is not moving forwards or backwards.
When we turn the steering wheel to the right, we want the car to turn right only if the car is
moving forward, i.e. the speed is greater than 0. If the car going backward, i.e. the speed is less
than 0, then the car should turn to the opposite direction, left. If the car is stopped, i.e. the speed
is equal to 0, then the car should not turn. In the same way, when we turn the steering wheel to
the left, the car should turn left when going forward, turn right when going backward, and not
turn when stopped.
How can we do this? Alice has an if/else statement that allows us to do one thing if a condition is
met and another thing when the condition is not met. In this case, the condition is the speed of
the car, and the thing we should do is turn the car. Let's try this in our world. Open the turnLeft
method.
Create an if/else block: Drag the if/else block from the bottom of the screen into the turnLeft
method and drop it just above the line that says convertibleCorvette turn left. Alice will ask for
an initial value of either true or false. The value we choose here does not matter since we will
replace it soon. Choose either one.
Set the condition to “speed > 0”: Drag the speed variable from the properties tab and drag it
over the condition you just chose to replace it. Select “speed > 0” for the condition. This means
that if the condition is true, meaning that the speed is positive and the car is moving forward, we
will execute the first block. Otherwise, we will execute the second block.
Turn left when the car is moving forward: Drag the original “[convertibleCorvette] turn
[left] [0.25 revolutions] [style = abruptly]” statement into the first block. This block executes
only when the speed is greater than 0, meaning that the car is moving forward. This means that
the car turns left only when the car is moving forward. If the car is not moving forward, it will
not turn left. When the car is not moving forward, we will execute the next if/else statement.
Turn right when the car is moving backward: Drag the car into the second block. Choose turn
right 0.25 revolutions. Change the style to abruptly. The statement should say:
“[convertibleCorvette] turn [right] [0.25 revolutions] [style = abruptly]”. This block executes
only when the car is not moving forward and it is moving backward, meaning that the car will
only turn right when the car is moving backward.
Apply the same thing to the method turnRight with opposite directions: We can repeat the
same steps with the method turnRight, but there is a simpler way. We can copy and paste both
if/else blocks from turnLeft to turnRight then reverse the directions.
To copy and paste, drag the outer if/else block to the clipboard. Delete the turn command from
the method turnRight by dragging it into the trashcan. Then drag the clipboard into turnRight.
Change left to right and vice versa.
When we play the world, the car should turn realistically.
Download