Next we are going to explore how to build the game we saw in section 9.1 with the colored balls and the spaceship. If you don't remember what that did, go back and play that video again. Remember, each time the user clicks the handle on the right, all three balls are changed to a random color (either blue or red). Sometimes a ball doesn't appear to change color after the handle is clicked -- this is because it "changed" to the same color it was the last round of the game (time the handle was clicked). At some point, the space ship takes off.
[Q9.4.1] How would you describe in English what the condition of the balls is when the space ship takes off?
We've built a good bit of this world for you, but we don't yet have the part that controls how the space ship takes off. To get started, download the SpaceShipGame.a2w world at the bottom of this page. Next we'll walk you through the what is already there, then we'll build an if statement that will control when the space ship should take off -- when all the spheres are blue.
Let's start by looking at the objects in our world. There's many of the things you expect:
But there's some things there you might not expect -- a cylinder. Note: you don't see it in the world, because we set it's opacity to zero (invisible). If you want to see it, right click on the cylinder object name in the upper left, choose methods, an set opacity to 100 (fully opaque). We are using this cylinder to do some kind of complex stuff to set the sphere's color randomly. You don't need to know how this works for this module. However, if you WANT to know how it works, click here for an explanation. Otherwise you can ignore the cylinder and just believe us about how it works.
The basics of the world are as follows:
However, there is no code that controls when the dropShip takes off. (You can check this out yourself by playing the world and clicking on the switch multiple times until you see all the spheres become blue. Nothing will happen. You can click the switch again and they will continue to change colors.).
Our goal is to check after each time the switch is flipped whether all the spheres are blue -- and if they are, then the space ship should take off into the sky (move up 30 meters will do).
Let's start by finding the place we want to make this "check". We'll want to put it right after the call to the SetBallsColorRandomly method inside the method Flip Switch. Drag an if/else tile in so we can use it to control our check (pick either true or false as a default value -- it doesn't matter which one -- we're just using it as a placeholder we will replace).
Let's set up the Boolean expression so that when it evaluates to true, then we will have the dropShip take off. We'll want this to happen when all the spheres are blue. In computing, we often have to take complex real world ideas or English expressions and break them down into smaller parts. That's going to be the case here just like it was with the either/or/orBoth from the car magnet example.
We say in computing speak that we want the dropShip to take off when both the sphere's color is blue and sphere2's color is blue and sphere3's color is blue. We have to check the each sphere's color to the value blue -- the computer can't figure out how to compare them ALL to blue on it's own.
Our goal will be to create the following Boolean expression for our if/else.
Like we did with the compound either/or/orBoth expression, we'll need to build this expression up a bit at a time from left to right.
We will start by replacing the true (or false) you picked as a default value when you dragged in the if/else tile. Click on the world object in the upper left window, click on world's functions tab below that, and at the very top of the set of functions, select (under boolean logic) the both a and b tile. Then drag that over the default true (or false) expression controlling the if/else statement.
You should select the "true" option so that the expression is now as follows:
Now we need to replace the first "true" in this expression. We want to have the true replaced by a check to see if the sphere's color is blue. Checking for an object's color is a little different in Alice -- we don't use the World's functions (math a == b). Instead, click on the sphere object in the upper right window, then below that, click on the properties tab (next to methods and functions). Then click on the color tile and drag that over top of the first true. A pop up window should give you some options -- pick the top one (==) and then select the color blue from the next popup list.
That takes care of the leftmost ball. Now to check for sphere2. Click on sphere2, then the properties tab in the lower left window, then drag the color tile over top of the other true, select == and then the color blue. Now you have checked for 2 conditions -- that sphere is blue and the sphere2 is blue. Your expression should look like this.
Just like with the either/or/orBoth, we now want to add another condition. Clicking on the down arrow just to the right of the blue in the second condition, you can select from the popup window to add another both/and tile. This will add a third "true" to your expression, which you then replace as you did above -- but to check that sphere3's color is blue.
Whew! Making these compound Boolean expressions can be time consuming! If you ever get lost, our best advice is to make sure you have the expression you want to create written out on paper, then build it up a bit at a time from left to right -- using the down arrows to add new parts as needed.
OK. So far all we have is our if/else statement boolean condition -- but it's not controlling any code! We went to all that trouble to build an expression that only evaluates to true as long as all the sphere's colors are blue. So, now, create a statement to make the dropShip move up 30 meters (making it appear to blast off out of the world) when all the spheres' colors are blue and put it inside of the if/else statement in the appropriate place.
Hit Play and test your code. Remember, you may need to play multiple rounds (clicking on the switch) until the spheres are all made blue!
Impress your friends by showing them your cool game!
Save the world from this page as M9-DropShip-pg4-Compound-YourName.a2w