To finish the game, all we have left to do is handle the endgame. We’ll finish the game when an asteroid hits the ship. At that moment, we’ll show a “game over” message and offer the player the option to start over. To visualize the endgame, we’re going to create a layer which will include two texts, one saying “Game Over”, and another saying “Press spacebar to start”. Click on the button to go to the scene editor and create a new layer called game over. In it, create a text object and call it game over message. Modify its properties as follows:
- fontSize = 120
- text = Game Over
- fill color:
- r = 255
- g = 0
- b = 0
- anchor = CENTER
- x = 515
- y = 331
- scaleX = 0,1
- scaleY = 0,1
As you’ll have noticed, we’ve placed a scale of 0,1 both on the X and Y axes. The scale accepts value between 0 and 1, where 0 means no size and 1 means 100% of the original size. The 0,1 represents 10%, so we’ve scaled the text down to 10% of its original size. The reason for this is that we’re going to create an animation to have the text scale up from 10% to 100% of it size. That way, it will seem that the text is coming from the background.
We’re going to create a second text which we’ll place below the game over message indicating that the user should press the spacebar to start a new game. So, create a new text object and call it restart. Since we’re going to be giving the same animation to both texts, it’s a good idea to make the restart message the daughter of the game over message, because by animating the mother, we’ll animate both. So, in the Asset Manager panel, drag restart over game over message. You’ll see that it inherits the scaling properties of its mother, being scaled down to 10%. Later, when we change the scale of the game over message, it will also change its daughters; that is, restart. Modify the following properties in restart:
- fontSize = 40
- text = Press space to start
- anchor = CENTER
- x = 0
- y = 74
- visible = false
The last property sets restart as invisible. We do this so that when we do the text animation coming from the background, the second text won’t show up until we expressly indicate for it do so through the logic we’ll program shortly.
To finish off the design of the visual elements, click on the game over layer in the Asset Manager panel, and set its visible property to false. This way, the elements of the layer won’t be seen until we set them to when we detect that the ship has collided with an asteroid.
Let’s go back to logic by clicking on . The first thing we have to do is detect at what moment the ship collides with an asteroid. Since we already dealt with this in the Spaceship ? Collisions ? Spaceship-Asteroids script, we’re going to simply notify the outside via an output connector. So, drag out an output connector and call it collision. The point at which we know the collision has taken place is the true trigger in BoundingCollision, so we connect the connector you just created to this trigger.
Go up a level and create another output connector with the name spaceshipDestroyed. Connect it to the collision trigger we just created in the last step.
Go up another level. In this level, we’re in the Spaceship script. To better order our game’s logic, we’re going to notify the ship’s collision outside so that it’s another script that handles what needs to be done. So, again, create an output connector with the same name, spaceshipDestroyed, and connect it to the trigger in the Collisions script with the same name.
Go up yet another level to get to the root level. Here, we already have a Spaceship script with a new trigger, spaceshipDestroyed, which tells us when the ship is destroyed. To keep our logic clean, create a new script we’ll call Game over. Connect the spaceshipDestroyed trigger to the in1 activator in Game Over.
Go into Game over. First, rename the in1 input connector to show. As we mentioned earlier, when the ship is destroyed, we’re going to show a “Game over” sign with an animation that scales from 10% of its size (which is where we left it in the scene editor) to 100% in one second. And when it’s at 100%, we’ll make the second message about starting a new game visible.
Let’s start by making the game over layer, which contains the two texts we’re going to show, visible. Drag out the layer from the AssetManager panel to create an ActionOnParam and select the setVisibility action. Double-click on the visibility parameter to create a variable and set its value to true in its properties. Connect the box to the input connector.
Now we’re going to perform the animation. This consists in having the game over message text object grow from its originally-scaled 10% up to 100% in one second. So, we’ll use the MoveFromTo box we’ve used before, but in this case, we’re going to configure it to modify the scale instead of the position. Drag a MoveFromTo from the Animation category in the Blackboxes tab. Click on it to see its properties, and modify translate to false and scale to true.
Now we’re going to fill in the following parameters:
- target: drag the game over message object from the Asset Manager panel to this parameter.
- seconds: create a variable and assign it the value 1
- fromScale: create a variable and assign its properties the values x=0,1 and y=0,1.
- toScale: create a variable and assign its properties the values x=1 and y=1.
Now, we have to make the restart object visible when the animation finishes (it will take one second). Drag the restart object from the Asset Manager panel and select its setVisibility action. Create a variable in the visibility parameter and change its content property to true. The arrived trigger from MoveFromTo tells us that the animation has ended, so you can connect it to the do activator.
Now we have to listen for the spacebar so that when it’s pressed, the game restarts. We’ll use the KeyboardListener box that’s in the Device category in the Blackboxes tab. If you click on it, you’ll see that it has many properties, one for each key on the keyboard. Set the Space property to true. Connect the done trigger from the last box to the on trigger in KeyboardListener.
When the spacebar is pressed, it will trigger Space. The first thing we should do is deactivate KeyListener so that it stops listening for that key, because remember, we’ll be listening for the same key press to have the ship fire. If we left it active, every time we’d press the spacebar after the first game over, the ship would fire a shot and then restart the game. So, we need to connect the Space trigger to the off activator in the KeyboardListener itself.
And lastly, we’re going to notify the outside that the spacebar has been pressed, so we’ll use an output connector. Drag one out and call it restart. Finally, connect it to the Space trigger.
NOTE: You can connect multiple inputs and outputs to both activators and triggers. You can also select the order in which they’ll be called by clicking on them and modifying their call order property.
Go up a level. In the root level, you’ll see that Game over has a reset trigger that we still haven’t connected to anything.
If you did a preview right now, you could see that when the ship hits an asteroid, the Game Over sign comes out animated, and then you’d see the second message telling you to press the spacebar to start over.
However, if you press the spacebar, the ship, even though it’s hidden, will keep firing. And if an asteroid hits it, despite being invisible, it will do the Game over animation again. What’s happening? Well, as you’ve probably guessed, since the Spaceship script is still active, its collision, keyboard, and shot creating scripts are all still working, so we need to stop them. Remember that we already foresaw this situation by including an off connector that disconnects all the scripts, including the Spaceship one, which is why we’re going to use it. Connect the spaceshipDestroyed trigger to the off activator in the same script.
Do a preview and check that after the ship is hit by an asteroid and the Game Over message pops up, that if you press the spacebar, no shots are fired, and that it’s not continuing to collide with asteroids.
Now that we’ve reached this point, all we have to do is start a new game when the restart trigger in the Game over script is launched. You might be tempted to connect restart to the on activator in Spaceship; however, we’d have to make some modifications to make that work correctly. Better yet, we’re going to create a new game This is very common in game creation and in programming in general, and it will help us keep our code orderly.
Create a script and call it init. Go into it and change the name of the in1 connector to do. When we start a new game, we want the scene and its elements to start with the values we gave them when we designed them. To do this, drag the game scene from the Asset Manager panel (the game scene is under the scenes element, not in layers, which is what you see first). As always, an ActionOnParam is created, where you need to select the reset action.
Next, we’re going to empty both shots and asteroids collections, because after the first game, they’ll contain obsolete elements that do not interest us. We’re going to first create a script to initialize the shots collection. Create a script and call it Reset Shots. Go into it and rename the in1 connector to reset. Add an output connector and call it done. Go back to this level and connect the done trigger in ActionOnParam to the reset activator.
Go back into Reset shots. To run through the collection, we’ll again use the ForEach box, which is under the Collections category in the Blackboxes tab. This is going to eliminate all the elements contained in the collection and finally empty the collection, to guarantee that each game starts with an empty shots collection.
Drag a ForEach box to the panel and connect the reset connector to the start activator.
Oops! We need the shots collection, but we don’t have it in this script, so we’ll have to share it in order to be able to access it. Go to the Spaceship script where we have access to both collections, and drag the shots collection to the shared bar.
Now go up a level and share both collections with the Init script. Remember that they’re shared by dragging the variables to the script where you want to use them. So, drag shots and asteroids from the Spaceship script to Init.
Now that we have both collections that we need in the Init script, go in so as to continue with the logic.
You should see both collections in the shared bar. Drag the shots collection to the Reset shots script.
Go into Reset shots and drag the shots collection from the shared bar to the target input parameter in the ForEach box. Remember that this box runs through all the elements of the collection, depositing with each step an element in the item output parameter and launching the step trigger. Double-click on the item parameter to create a variable and collect each shot. Then, drag it to the panel to create an ActionOnParam and select the action delete. Connect the step trigger to the do activator.
Once we’ve eliminated the shots, we have to empty the collection because it contains elements that have been destroyed. Drag the shots variable from the shared bar (or the target parameter) and drop it in the panel so as to create an ActionOnParam. Select the empty action. Finally, connect the finished trigger in ForEach with the do activator in ActionOnCollection, and the done trigger in that last one to the done output connector.
Go up a level. Now, we have to do the exact same thing with the asteroids collection. We can repeat the same steps, just using the asteroids collection this time. However, we’re going to make a copy of the Reset shots script. Right-click on Reset shots and select the option copy.
Then right-click again on the background and select paste. You’ll see a new script appear with the same name and same parameters. Modify the new script’s name to Reset asteroids and connect it to the done trigger in Reset shots.
In Reset asteroids, we need the asteroids collection, but since we’ve copied the shots one, we have shots. Right-click on the shots variable in Reset asteroids and select the option Remove.
Now drag the asteroids collection that’s in the shared bar and drop it over the Reset asteroids script.
Check that the script now has the asteroids collection. Go into the Reset asteroids script. In this script, what we want to do is empty the asteroids collection. So, we’re going to substitute the shots collections in the target parameters of the ForEach and ActionOnParam boxes for asteroids. Drag asteroids from the shared bar over the target parameter in ForEach. You’ll see asteroids substitute shots. That is, if we drop a variable over a parameter that already has a variable, the new one we drop will substitute the old one. Again drag the asteroids variable over the target parameter in ActionOnCollection.
Go up a level. We’re going to finish the init script by adding an output connector. Drag an output connector from the Connectors tab and call it done. Connect the done trigger in Reset asteroids to the output connector.
We are finally in the last step for finishing our game. Go up a level, and we’re going to perform the final changes before sharing our game with the world.
At the beginning of this tutorial, we united the initial gameStarted event with the Background, Spaceship and Asteroids scripts. Now what we want to do is to have the Init script be the first thing run, and when that’s finished, then call on the rest. So eliminate the connections that come out of gameStarted. To eliminate a connection, right-click on a connection and select Delete. Connect gameStarted to the do activator in Init. And then connect the done trigger in Init to the on activators in Background, Spaceship and Asteroids.
Now all we have left to do to finish the game is reinitiate everything when the user presses the spacebar; that is, when the restart trigger in the Game Over script is launched. Connect the restart trigger to the do activator in Init.08/03/2016 / No Comments
My first game
- My first game with WiMI5
- Signing up
- Before getting started
- Designing the game
- The background
- The ship
- The asteroids
- Collisions between shots and asteroids
- Collisions between the asteroids and the ship
- Collisions between the ship and the edge of the scene
- Game over
- And now what?