Shooting Bullets
Bullet Moving, Destroy Itself
Go into the bullet object. Add a Create event….
When the bullet is created, we want it to move upwards so Set the vertical speed to “-8”.
Next we add a Outside Room event. We have to destroy it once it leaves the room or else you’ll have more and more bullets flying around.
- Destroy Instance: default, destroys itself
Player Can Shoot
We want the bullet to appear when the player (who controls the main plane) hits the space bar. Go into your myplane object and add a Space Bar event.
- Create Instance: we create a bullet just in front of the plane (0,-16). Make sure its relative so it’s just in front of the plane (not top left corner of the room!)
If you test it out, you’ll see something like this…
Bullets Spaced out
We don’t want too many bullets at a time, so only fire 2 bullets every second (= 15 steps).
In the myplane object, add a Create event…
- Set Variable: set the variable can_shoot to “1”
Go into the <Space> event you already have. We’ll first test if we can shoot, and if so, we’ll create a bullet, and then set a timer/alarm…
- First we check, are we allowed to shoot? (= if “can_shoot” true/1).
Test Variable: if can_shoot = 1
Then add the following actions…
- Set Variable: set the variable can_shoot to 0 (false, indicating we can no longer shoot).
- Set Alarm0: to 15. The alarm ticks down to 0 with one tick each step. You can change the speed with which you can shoot by changing the value of the alarm clock.
Add Start and End blocks around the actions as shown below…
You will also need an event Alarm0 – what happens then the alarm reaches 0, runs out of time? In this situation, we want to set the variable can_shoot back to true/1 so the plane can shoot again.
Now if you test it in your room, the bullets should be spaced out…
Enemies Hit by Bullets
If the enemy plane is hit by a bullet, then the enemy plane should be destroyed.
Explosion 1
Go into the object explosion1. The only thing we need to do is add an Animation End event so that once the explosion animation has played, it destroys itself.
Enemy1 Hit by Bullet
Go into your bullet object and add the following event: Collision with enemy1
- Create Instance: set to ‘other’ (so enemy1 plane turns into explosion, in the same place as the enemy plane)
object = explosion1, (0,0), relative - Set Score: give the player 5 points, make sure relative
- Destroy Instance: (self = the bullet)
- Destroy Instance (other =enemy1 plane)
Test it out – if you hit your enemy1 plane with a bullet:
- does the bullet disappear
- does the enemy plane explode/disappear?
Enemy 2 Hit by Bullet
Still in the bullet object:
- Right-click > Duplicate Event (on the Collision with enemy1 )
- set to Collision with enemy2
- change Set Score so player gets more points for shooting this plane since it will be harder to hit later on – set to 10 points
Enemy 3 Hit by Bullet
- Right-click > Duplicate either Collision with enemy 1 or 2 event for Collision with enemy3
- give the player 20 points for hitting this enemy plane
Test it out – you should now be able to hit all the enemy planes.
Special Fire Bonus
It is rather common in scrolling shooter games to find power-ups that give the player extra fire power, repair the damage, give extra points, give an extra plane, etc. Such power-ups can appear when the player shoots a plane or they might simply be encountered during the flying.
For example, you can have a special fire bonus (ability to shoot more bullets) when the player reaches a score of 50 and another one when he reaches 100 points. In a proper game you’d make it more like 400 and 1000 but we want it to be easy for testing.
Go into the main plane’s Space Bar event. Add a Test Score action (if the score > 50). If so we’ll set the Alarm to 10 (bit faster), otherwise the Alarm will be the normal amount we had.
Add Start & End blocks around the new Alarm action like this…
Inside the Start & End blocks we’ll do the following…
- Test Score: test if score > 100
- If so, Set Alarm: set alarm to be super fast, only “5”
- If not (score >50, but not 100) Else…
- Set Alarm: set alarm just a bit faster, “10”
So it now looks like this…
Game Ending
Go into the object life controller. Add a Step event that checks the score. When the player has more than 200 points, the game ends. (normally you’d set the amount to much higher, but we’re keeping it low so you can easily see it works).
If you test out your game, hold the Space Bar to keep shooting and go back and forth along the bottom of your screen to get the score up quickly. Once you get the score to over 200, you should see the high score table.
Test your game, make sure it’s working- keep it in play mode to get checked off