Adding a starting countdown to the Racecar world
Pre-Requisites: Lesson 7 Racecar world, variable creation
Using Boolean variables
In this lesson, we want to add a starting countdown to our racing game. To do this, we will make use of an important computer science concept called the if/then/else statement.
In programming, we sometimes want our program to decide between two different behaviors. For example, suppose that we want to program a game of tic-tac-toe. We will probably want the game to be over when the player wins/looses the game by getting three in a row on the tic-tac-toe board. If the player has not won or lost yet, we want the game to keep going instead so that the player can make the next move. We can help our program to make the decision of what to do to determine if the game is still going on by using an if/then/else statement.
Let us restate the problem of whether or not the game should continue more formally using an if/then/else statement: IF the player has won/or lost our game (gotten three in a row) THEN the game is over and the program should stop the game. Otherwise (ELSE) the player must have not gotten three in a row and the game should continue.
If/then/else statements allow us to use what computer scientists refer to as conditional logic. Basically, we want our programs to make decisions on what to do if certain requirements, or conditions are met, and what to do if they aren’t. If/then/else statements are extremely useful for making our programs flexible.
Making a countdown to start our race:
We will use if/then/else statements to make a starting countdown for our race. During the countdown, we don’t want the player to be able to drive the car. The program should allow the player to move the car after the countdown has finished, but not before.
Creating the countdown method:
Create a new empty method in convertibleCorvette and name it countdown. To do this, select the convertibleCorvette object from the object tree and then select the methods tab. Click “create new method” and name the new method “countDown.”
We need to add someone to our game to actually call out the countdown. To do this, first move the camera to the car view. To do this, select camera from the object tree, right-click it and go to methods->set point of view to->Dummy Objects->carView. This should move our camera to the car view. Now, we will add our countdown starter. Click the green “Add Objects” button, and find an animal or person object to be the countdown starter. Drag the character of your choice to the side of the road just in front of your car. Make sure the character is visible in the carView view. If the character is very large, you can resize it by clicking the “resize” button from the row buttons on the top right of the add objects screen, the second to the last.
Starting the countdown:
Now return to the countdown method. To do so, click the “Done” button to leave the add objects mode. Then make sure the “world” object is selected and click on the methods tab, find the countDown method and edit it. Now find the character you just added to the world in the object tree, and drag it to the “do nothing” block of the method. Then select [your character]->say->other. For the text, enter “Ready…” Now drag your character again from the object tree, after the “Ready” line, and make the character say “Set…” as the next line. Do this one more time to make the “GO!” line.
Calling the countdown method:
Now we only need to call the countdown method. Select the “world” object from the object tree and click the methods tab. Edit the method “my first method”. Drag the countdown method after the camera.followCar method.
Keep the car from moving during the countdown:
Now when we start up the race car game, we have an Alice character to give us a countdown. But we can still move the car while the countdown is being given! We don’t want the player to move the car while the countdown is still going on.
Create the startrace variable:
We can use a special variable called a Boolean variable to keep track of whether the race has started or not (the race officially starts when the countdown ends.) A Boolean variable can only have one of two values: TRUE or FALSE. You can think of a Boolean variable as if it were a light switch. A light switch only has two states: on or off, just like a Boolean variable can only be true or false. We can create a Boolean variable that is false when the countdown is still going on, and then true when the countdown is finished, to tell the program whether the car should be able to move or not.
Make sure the convertibleCorvette is selected in the object tree. Select the properties tab and click the “Create new variable” button. Enter “startrace” as the name, and select “Boolean” as the type. Then under the drop down box, select “false.” This creates a Boolean variable that has an initial value of false. The countdown will start immediately when we start our Alice world, so the “startrace” variable so be false at the start, since the car should not be able to move until the countdown finishes.
Adding if/then statements to the movement methods:
Now we need to change the methods we created to move the car, so that they won’t work while our Boolean variable startrace is false. Select convertibleCorvette from the objects tree and click the methods tab. Edit the moveForward method. Drag an if/else block into the method body. The “if/else” statement is at the bottom of the method body, it is in a green bubble. From the “condition” drop down box that appears, select “true.” Drag the line that moves the convertibleCorvette forward into the “do nothing” just below the “if” part of the block.
This dragging the move command to this part of the block will make it so that the move command will only execute if the condition of the “if else” statement is true. Since the condition is “true” currently, the command will always execute. If you were to run the Alice world now, the car’s motion would be the same as before. We want the move command to only execute when startrace is true, so we need to replace the “true” next to the if with our startrace variable. Select the properties tab of convertibleCorvette. Find the startrace variable and drag it into the rectangle directly to the right of the “If” where it says “true.” The top of the “if” statement should now read: If [convertibleCorvette.startrace]. Now when you run the program, the car will not be able to be moved forward with the “w” key anymore, since startrace is false.
Now we need to do the same procedure for the “moveBackward” “turnLeft” and “turnRight” methods. Edit each of these methods and add an if/else block to each. Then drag the move (or turn) command that is in the method into the first “do nothing” just under the “if” line. Then replace the “true” next to the “if” with the startrace variable of convertibleCorvette.
Changing the startrace variable to true:
If we run the Alice world now, we won’t be able to move the car at all, even after the countdown finishes! This is because startrace is initially set to false, and never changes. We need to change startrace to true after the countdown finishes so that the car can move after our countdown says go.
Select the world object in the object tree, and the methods tab. Edit the “countDown” method. With the method body open, select the convertibleCorvette object in the object tree, and select the properties tab. Find the startrace variable and drag it right after the last [your character]->say command. From the drop down box choose set value->true.
Now, when we run the racecar world, we should not be able to move our car until the countdown finishes.