This is a continuation of the Space Invaders Game Maker: Studio tutorial. Make sure that you have completed the steps in Part 1 . If you have – good for you. Let’s begin.
Just to refresh your memory, Part 1 of the tutorial ended with us setting up player movement. Well we are no where near finished with the player. An integral part of Space Invaders is allowing the player to shoot, in order to defend the bottom of the screen so in this part will be adding this functionality. For those of you that have actually played Space Invaders you know that it is perhaps one of the most notable arcade games with the One Bullet At A Time rule.
The shooting in Space Invaders basically works like this: You cannot shoot/produce another bullet until you hit an enemy or until your bullet goes off screen. This makes the game very challenging, so feel free to improvise where needed.
First of all, we need a sprite for your bullet. It can be a circle, a Ziz-Zag a triangle, whatever you want it to be. For the sake of being “historically accurate” I’m going to use an elongated rectangle just like classic Space Invaders game used. The color should also be the same as the cannon sprite that we used in the previous tutorial. It’s RGB value should be (0,255,0). You can also use white. Whatever color you choose, our background will always be black. Here’s the sprite that I am going to use.
(6×16 Solid Green Rectangle. Otherwise Known As Our Bullet)
This sprite will not be animated. Once you’ve made your sprite, it’s time to make the bullet’s object. Create a bullet object and give it the corresponding sprite, which should be the sprite that you are using for the bullet. Once you’ve done that it’s time to populate the object with events/actions. We will be editing this object in another part but for now, add a Create event to your object. In this event we need to give our object some speed. If you’re a keen observer, you should have noticed that our ship will be shooting vertically rather than horizontally. This means that we must alter the objects vertical speed. This variable is shared among all objects and it is called vspeed.
The create event will only be populated with one Action – and the action we are looking for is under the Control Tab. It allows you to set variables to any desired value. We will set vspeed to -8 for now, although you can tweak this value to your hearts desire later on. This is all you should have in the actions portion of your Create event:
We are almost done with adding content to the bullet object, but now we need a way to delete bullets as we don’t want to create unnecessary performance issues with our game due to us not deleting objects properly. We simply need to check if the bullet goes out of bounds( for now, because we don’t have any enemies in our room yet).
Add an event to your bullet object(under other) and you should see a context menu popup. Select “Outside room”. Once this is done, we need to populate the event object with actions. The only action we need for this event is under the main1 tab. It looks like a trash-bin with a recycle icon on it, but this basically destroys the instance once it exits the rooms boundaries. After you have done this, it is time to actually allow the player to shoot these bullet objects that we have created. Before we do this make sure that the Visible check-box is checked under the bullet object properties. Nothing else should be checked. We don’t want the player shooting invisible bullets do we? Although you can certainly do that if you wish, haha.
We are done with the bullet object now so you can close that and save the project. Save often as you don’t want to have to rewrite things over if Game Maker unexpectedly crashes on you. Open up the shooter object, and look in the Create Event. So far there is only one action here, which gives the player one health. This is kind of useless to us now, because we don’t actually have any enemies but we will in later parts of the tutorial – but keep it anyway!
Go ahead and get the “Set variable…” event and drag it into the actions box. The variable that we are creating is a boolean variable and it will determine whether or not the player can shoot. We must set this to true when the player is created, because it can be safely assumed that the player has not actually shot anything when he is spawned into the room. You can name it anything you want, but just make sure it has meaning. I named mine to_shoot, although I think can_shoot would be better in this case. Remember that 1 is true and 0 is false. Here’s a screenshot in case your lost:
Now we need to make sure that this variable actually updates itself. Remember that if the bullet exists and is inside the screen, this means that the user has already shot, and we need to make sure that it is false. However if it doesn’t exist that means that the user hasn’t shot, and we can set the variable to_shoot to true anyways. So let’s do that. Create a Step Event. We need to test if an expression is true, and you can find this action(as a ? mark) in the Control tab Under Questions. Double click the action and make sure you have this stuff in it:
Make sure that NOT is checked. Let me explain what we are doing here. We are testing to see if the instance does NOT exist (hence the checking of note). So we are looking for this to return false, and if it does…. well, we set the variable to_shoot to true. I’m not going to explain how to do this since it is fairly straight-forward.
Okay next – add a keyboard event that looks if the Space Key was pressed. Then add these actions to the event.
Basically you check to see if the player can shoot and if so we create an instance of the object. Then we set the variable of to shoot to false(until it goes out and deletes itself – in which it won’t exist anymore!). As for where to spawn the object, you’ll have to play around with it(based on what sprite you used), but for mine I used the position (1,-10) while keeping those coordinates relative to the object(so make sure the relative tick box is checked).
Your player should now be able to shoot! Once again, if you don’t like how fast the bullet is going just tweak the speed values. If you have any questions please just let me know in the comments or send me an email. If you’re actually following the tutorial just leave a friendly hello in the comments as well. Keep coding and keep making games!