Pages

Tuesday 22 November 2016

Assignment - Space Invaders Game

Introduction

For the second part of this assignment I am going to be making a remake of Space Invaders in GameMaker. If you are unaware, Space Invaders is a game where the player controls a cannon at the bottom of the screen, and you have to shoot the endless waves of aliens that gradually move down the screen before they get to you. As you kill enemies, they get faster and harder to hit. They can also destroy the barriers that you use for cover. I will put a video of the original Space Invaders below:

In this written part of the assignment I will show you step by step how I made the game and what code I used. I will also show you the assets I used (sprites etc.) To help make this game, I followed a detailed YouTube tutorial that shows the code step by step and explains what it does.

Assets (Sprites and Objects etc)

These are the sprites used for Space Invaders. As you can see there are not many! I tried to do pixel perfect copies of the original sprites because I wanted retro graphics. Each sprite used a 32x32 box, but the sprites were quite a lot smaller.







The sprites were created with GameMakers built in sprite editor, which I will show below.



The alien sprites are also animated. They each have 2 different poses that they change between as you play the game. Below is an example of alien 1's animation.



Spr_barrier sort of has animation, however it only changes when it is hit by an alien bullet because the point of the animation is to show the barrier taking damage.



The objects and their functions will be described below, and some of them don't really need a detailed explanation. I will describe a couple here.

Obj_controller is placed in the room and allows the background music to play. The blank obj_alien is also like a controller. It is a parent alien that controls all of the other aliens. This saves time as you don't have to put code and events into each individual alien. 










I also have some sounds in the game which I created myself/with classmates. I have a background track the same as the one from the original SA, however I created it with my voice. The player shooting sound is a random sound that we picked up in the background while recording, however when a wahwah or phaser effect is added to it, it sounds like a laser. The alien death sound is a simple beeping sound that we got from pressing the buttons on a printer. And the player death sound was originally me hitting some bed sheets, however I upped the bass to make it sound like a muffled explosion. Each sound is an mp3 file, and was edited and exported in Audacity.




1) Player Movement

In Space Invaders, the player can only move left and right, so not a lot of code is needed. The first thing I did was add some simple movement code in a step event for obj_player. This code simply checks whether the player has pressed the left or right arrow key (you can only move left and right in Space Invaders) and moves them along the x coordinate.


2) Alien Movement

In Space Invaders the aliens move left or right as a group, and when they hit a wall the move down and then continue in the opposite direction. The movement for the aliens takes a bit more code than the player movement. Firstly, I made some global variables in the create event of obj_player. This includes the global alien count and the direction that the aliens will move in.


(alien count went down to 45 shortly after.)

Next up is the alarm. The alarm is used to speed up the enemies every time 10 of them are killed. There are 2 separate alarms, the first one is in the create event of obj_alien, the second one is in an alarm 0 event of obj_alien.




This code is a main part of the movement. It lets the alien know that when it hits the edge of the map that it is time to change direction. The alien will move down, and will begin moving in the opposite direction (left or right.)



Next up is a step event in obj_alien. This code is also for the aliens changing direction. If the aliens reach certain parts on the map they will change direction.





3) Player Shooting

Player shooting is getting the player to fire a bullet when they press space. The first bit of code is in an obj_player spacebar event. This code adds a new variable called "fire" and it simply creates an instance of the bullet when space is pressed. The alarm is used to stop the player from shooting too fast, since that would make the game a little bit too easy.





Fire=true is also put into a separate alarm event.



Then I went into the bullet object and added some code. First I added a create event, which simply sets the vertical speed of the bullet after it has been fired by the player. 



Then I added a collision event with obj_alien, which destroys the alien instance when the bullet hits it. The bullet itself is also destroyed in the process.



 Lastly, there is a small bit of code in an outside room event, which destroys the bullet if it misses the aliens and leaves the room.



4) Alien Shooting

First, I went into the create event in obj_alien. I added fire=true, which of course just allows the alien to shoot. Front=false stops enemies that aren't on the front row of the group from firing. If all of the enemies could fire, then they would be shooting each other.





Then I went into the step and added lines 10-15. This code checks whether there is space in front of the aliens, so that they can fire without hitting other aliens. If there is space in front of them, front is set to true, if not it is set to false.




I then went into the alarm 0 event and added a shooting section at the bottom. This stops the enemies from shooting constantly, and gives them a 1 in 7 chance of shooting a bullet.





Then I added and alarm 1 event and added fire=true, which allows the enemies to fire.





I then added a create event to obj_bulletalien and added vpseed=4. This is half the speed of the players bullets.





5) Lives

In Space Invaders, when the player dies they go invisible for a short period of time and then respawn. This next code in obj_alienbullet is in a collision event with obj_player, and it checks whether the player can take damage or not. If the player is visible, then they can be hit by the bullet and they will lose 1 life, however if they are invisible (respawing) then they cannot be hit. Like the player bullet, the enemy bullet is also destroyed upon impact.





I also added another outside room event that destroys the enemy bullet if it leaves the room.



When the player dies, the game also freezes for a short period of time. During this period all bullets in the room are destroyed. To do this, I simply added this code in a step event for the player bullet and alien bullet which destroys all bullets while the enemy is invisible (respawning.)



We also want to enemies to stop moving while the player is respawning, and to do this I simply added a line at the top of the movement code in the alarm event for obj_alien. This line only lets the aliens move if the player is visible.




Now we move on to adding the lives to the player, first a variable needs to be created in the create event called live=3, and then in the step event we use code that restarts the game when the lives are lower than 0.





Next I created a font for the HUD.




Next I put a draw event in obj_player that puts HUD/text/lives on the screen.





6) Barriers

Whenever the barriers are hit by the aliens, they are weakened slightly, and damage is shown on the barrier. The separate stages of damages are inside the sprite, like an animation. However, we don't want the barrier to be constantly animating through these stages, so we add image_speed=0 to make sure it doesn't animate normally.





Then, inside a collision event for obj_alienbullet and obj_barrier, we use this code. This code checks which stage of damage the barrier is on. There are 3 stages of damage, so if the barrier isn't on stage 3 and is hit, it needs to move up to the next stage. However, if it goes higher than 3 it will be destroyed. The bullet that hit the barrier will be destroyed as well.




If the players bullet hits the barrier, we don't want the barrier to be damaged, so all we want to do is destroy the bullet on impact.





7) Bonus Spaceship

First, we want to set the hspeed, which is done in a create event in obj_ship.




Next, just like the bullets we want to make sure that the ship despawns when it leaves the room. We cannot use the outside room event for this one though, because the ship spawns outside the room, so it would be destroyed instantly. Instead we will do it in a step event. This code is basically saying that if the ship is at an x coordinate that is greater than the room width, it should be destroyed.



Next we need to set up and alarm that spawns in the ship. This alarm is done in obj_player because it's something that is always on the screen. The alarm spawns in a ship every 12-15 seconds.



This other part of the alarm spawns the ship outside the room.



8) Score

In Space Invaders you get a different amount of points depending on which enemies you kill. This means that we will have to make some separate events for each alien. These collision events are done in obj_playerbullet. First however, we need to create a points variable in obj_player.




Next some short code is put into a bullet collision event with each alien. The aliens at the back give 30 points, the middle ones give 20, and the front ones give 10. The bonus ship gives 100 points. Each alien uses the same code but with different points.






No comments:

Post a Comment