Space Invaders Writeup

My Space Invaders is set to an aspect ratio of 16:9. I have made my own assets for this game which I’ll display in the images below. The first of these is the player ship.

This is the player ship.

This is the enemy ship.

This is the player’s bullet.

This is the enemy’s bullet, however I didn’t end up using this asset.

I have added some of these sprites to my scene, however they won’t function unless I add some code to them. I started off by adding code for the player’s ship.

I start off this code by declaring my variables for both player movement and bullet creation. The transform variable called “playerTransform” detects which object we want to transform. The float variable “speed” holds the speed that the player moves at and the float variable “moveH” holds the position of the player.

I have also declared four bullet creation variables. the GameObject “bulletFired” stored the bullet that the player has just fired. the transform “bulletSpawn” stores the information for the bullet spawning. The float “fireRate” stores the speed at which bullets are fired and the float “nextFire” stores the time required before the next instance of the bullet being fired.

In void start I have included a GetComponent command which automatically locates the required component.

In void update I have included a function to control the firing of the bullet. This function both controls the rate at which the bullets are fired and also handles the spawning of the bullet when the jump button is pressed.
In void FixedUpdate I have included a function which controls the movement of the player.

Now I can control the movement of the ship, however I need to include code for my bullet prefab before I can actually fire.

Here is the information for my variables. I have created a bullet prefab and set that as the bullet to be fired. I have also set the speed to 0.3, the fire rate to 0.2 and the bullet spawn location to obj_player.

The image above shows the code for my bullet behaviour script. I start off by declaring three variables, these being bulletTransform which controls the movement of the bullet, speed which controls the speed at which the bullet travels and enemyRespawnPrefab which will store the prefab for enemies that will respawn when killed.

In void start I include the same GetComponent as was included in the player behaviour code.

In void fixedUpdate I feature the code for the bullet’s movement. For this I use a vector which moves straight up and multiply by the speed to create the movement of the bullet.

In void OnTriggerEnter2D I have included the code for the respawning enemies. For this I have used an if statement that checks whether or not the gameobject that the bullet has hit has the tag “Enemy” and if it does, the if statement is ran. When the if statement is executed, the enemy is destroyed, then a new enemy is spawned through the prefab at a random location and the bullet is destroyed.

The code above is for the enemy prefab. I start off by declaring my variables. The first of these is enemyTransform which controls the movement of the enemy and the second is speed which controls the speed at which the enemy moves.

In void update I control the movement of the enemy. This is the opposite of the movement that the bullet does as the enemy moves down instead of up.

In void OnTriggerEnter2D, I handle what happens when the enemy makes contact with the player. For this I handle the scene manager which allows me to reset the scene when the player makes contact with the enemy.

While this game is functional, there are many improvements that I could make. First of all, I could add a feature that makes the enemies speed up as the game progresses. Secondly I could add a score function that counts up the number of enemies killed. Another feature that I could add is a border that stops the player from going offscreen and a border on the bottom that kills the player if the enemy hits it. Finally, I would like to add some basic audio.

Leave a comment

Your email address will not be published. Required fields are marked *