Empty Your Mind Tutorial 9

This tutorial will teach you how to use the SwinGameSDK to develop a simple danmaku game which looks cool. At the end of this tutorial you should be able to use pascal to implement your own space invaders or scrolling shooter style game which makes use of Vectors, animated Sprites, SoundEffects, and Music.

Preparing A Data Required
In danmaku game, the "hit box" is much smaller than the actual ship. Therefore, we will need an image that defines the player's ship's collision box. The example image can be found [[Media:ShipC.png|here]]. This data should be stored in the ShipData structure. The new implementation of the ShipData structure follows.

Do not forget to initialise the theSpriteC in the LoadGame procedure. We will need to synchronise the position of the collision box with the actual ship's position to use the collision box. This should be done after moving the ship's position (i.e. after MovePlayerShip). The code to synchronise the position should be:

Collision Detection With Bullets
We will be writing a single procedure that will check a collision with bullets for both the player and the enemies. The following steps must be takes to check a collision between a bullet and a ship. The steps should be repeated until it checks all bullets. The implementation of the steps follows.
 * 1) Find a bullet that is alive
 * 2) Check if the bullet is against the ship specified. Go back to the step 1 if not.
 * 3) Initialise the sprite used to check the collision depending on the side of the ship.
 * 4) Check if the ship and the enemy have collided. Go back to the first step if not.
 * 5) Subtract the ship's health by the amount of damage the bullet should do.
 * 6) Kill the ship and play a sound effect if the health is lower than 0.
 * 7) Kill the bullet collided.

This method to check and process a collision needs to know which side the ship is on because the procedure must check if the bullet is not fired by the ship's own side. The sound effect played in the procedure is played when a ship gets destroyed. The sound effect I have used in my game can be found [[Media:Destroy.wav.zip|here]]. The ProcessShipCollision routine should be called after processing the ship and from the UpdateEnemies procedure.

To update the player's ship:

To update a enemy's ship, you will need to add a new line after checking if the enemy has gone out of the screen. The new implementation of the UpdateEnemies procedure:

The final thing you need to do to kill the player is to end the game if the player is not alive. Insert the following line into your game loop to quit the game when the player gets hit.

Summary
In this tutorial, I have gone through: The current project files can be downloaded from [[Media:EYM_11_06.zip|here]].
 * Collision detection