Lesson 11 Using Variables to Control the Car Speed Pre-Requisites: Post lesson 5 or post lesson 8 world Topics: 1. Variables 2. Changing a variable’s value 3. Deleting a method Introduction: We have made some progress but our car’s movement is still pretty unrealistic. How a car turns depends on which direction the car is moving and if the car is moving at all. We also want to change how fast our car is going. Changing directions: Let’s look at how we currently move our car. Select convertibleCorvette in the object tree, and then select the methods tab. Click the “edit” box next to our moveForward method. The moveForward method only has one command, an Alice move method that moves the car forward 5 meters. What would happen if we changed the “5 meters” to “0 meters” in this method? [The car does not move] What about if we changed the “5 meters” to “-5 meters”? [The car moves backwards]. If we decide that the car is moving “forward” when the number in the move method line is a positive one, “backward” when the number is negative, and that the car must not be moving if the number is zero, we can check which direction the car is moving (or if it is moving at all) by checking if the number is positive, negative, or zero. However, we cannot check or change the number in the move method when our Alice world is running unless we use a variable. Variables A variable is like a placeholder for a value that our program uses when running. Unlike the specific numbers we have set in our moveForward and moveBackward methods, a variable’s value can be checked and even changed by our program world when it is running (when we hit the “Play” button). So if we use a variable, we can solve our problem of checking whether the car’s speed is positive, negative or zero. Create a car variable called "speed" Find convertibleCorvette in the object tree and select it. Then select the “properties” tab below. This will bring up the properties of convertibleCorvette, which is where variables can be created. Click the “Create new variable” button at the top of the tab. When prompted, name the new variable speed. Make sure that the “Number” option is selected. Click the drop down box labeled “Value” and select “Other” and then enter 0. This will create a new variable named speed with an initial value set to 0. Make sure that the initial value is zero: in the “properties” tab there should be a box labeled speed and it should be set equal to 0. This will ensure that our car starts out parked and not moving. speedUp and speedDown Because the car isn't moving when the game starts, 0 will be correct for the car's speed at the beginning. Now we need to change it when we start moving in a direction and back to 0 when we stop moving. We can use the “begin” and “end” blocks of our W and S events for this, just like when we made the tires turn. To help with this, we need to create two methods under convertibleCorvette: slowDown and speedUp. These will change the value of the speed variable. Create a method called "speedUp" Make sure convertibleCorvette is selected in the object tree. Select the “methods” tab and click on the button “Create new method”. When prompted, name the method speedup. Click the edit button next to the speedUp method. Now select the “properties” tab. Drag the speed variable over "do nothing" and set the value to convertibleCorvette.speed under the “expressions” drop down menu. Click the down arrow after the value on the convertibleCorvette.speed value on the right side of the line, select "math" -> "speed +" -> “other” ->"5". Create a method called "speedDown" Again with convertibleCorvette is selected in the object tree, select the “methods” tab and click on the button “Create new method”. Name the method “speedDown”. Switch back to the “properties” tab and drag the speed variable to “do nothing”. Repeat the steps for speedUp but instead of “speed +” select “speed –” and then “5”. Using speedUp and speedDown Now we need to modify our events to speed up or slow down the car when we press or release the right keys. It's obvious that we want to speed up when we move forward and slow down when we move backward, but how do we stop? [Guide students to using the other method to make value of speed 0.] Changing W and S key events to use speedUp and speedDown Drag speedUp over the "begin" block of the W event and the "end" block of the S event. Drag slowDown over the "begin" block of the S event and the "end" block of the W event. Creating the drive method Now we have methods that increase and decrease the car’s speed, but we still need to use the speed variable to actually move the car. To do that we need to create a new method that moves the car forward an amount equal to the value of the speed variable. Create the drive method Select convertibleCorvette in the object tree. Select the “methods” tab and create a new method named drive. Click the “edit” button next to the new drive method. Drag convertibleCorvette into drive, and then select "move -> forward -> expressions -> convertibleCorvette.speed. Remember to change the style of the animation so that the car moves smoothly. Click "more..." in the move method and set the style to "abruptly". Finally, Drag the drive method over the "during" blocks in both the W and S events. Deleting methods Now we have methods for both increasing/decreasing speed and then using speed to move the car, we can get rid of moveForward and moveBackward. We can delete them by moving them to the trash can next to the "redo" button. Delete the moveForward and moveBackward methods Select the convertibleCorvette in the object tree. Select the “methods” tab. Drag the moveForward and moveBackward methods to the trash can by the redo button to delete them.