4 Shooting The Ghosts
Create a new sprite called spr_bullet and load the bullet image.
Create an object called obj_bullet and choose the bullet sprite from the list. Make the bullets solid.
Go back to the ship object window. In the Create Event, add a Set Variable icon. Choose to set bullet_timer to 0.
The bullet_timer variable will be used to make sure that there is a slight delay between each bullet. That stops the game being too easy.
Still in the obj_ship window, add a Step Event. Drag in the Set Variable icon and fill in the pop-up as follows,
We will need a sound when the ship fires. Go to create a sound called snd_zap and load the zap sound.
Now return to the obj_ship window and add a Keyboard event. Choose the CTRL key. Drag in a Test Expression icon. We will only fire if the bullet_timer variable is 0 or less. Fill in the pop-up as shown
Drag in a Start Block arrow followed by a Play Sound icon set to play the zap sound we added earlier.
Drag in the Create Instance icon and fill in the pop-up as shown,
Drag in a Set Variable icon and set bullet_timer to 20. That means that there will be 20 game steps before the player can fire again. You will able to adjust this figure later if you need to.
Add an End Block icon to finish off, leaving your code looking like this,
So far, we have made the bullet appear but haven't made it move. When the bullet is created, we want it to start moving directly up the screen.
Open up the obj_bullet window and add a Create Event. Drag in the Move Fixed icon and fill in the pop-up so the bullet moves up the screen at speed 5.
When the bullet goes off the top of the screen, we might as well destroy it. Add an Intersect Boundary event (under Other) and drag in a Destroy Instance icon.
We also need to make the bullet disappear when it hits a ghost. We should also play another sound. Create a sound called snd_boom and load the boom sound.
Add a Collision Event (with the ghost object) and drag in the play sound icon, set it to play the boom sound. Drag in a Destroy Instance icon.
Finally, we need to make the ghosts disappear when the bullet hits them.
Open up the ghost object window. Add a Collision Event (with the bullet).
Drag in a Set Score icon, type in 10 and check the Relative checkbox.
Drag in a Destroy Instance icon.
This is a good time to save your work and test the game so far. Make any adjustments to ship movement or firing that you think might be necessary.