Empty Your Mind Tutorial 2

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.

Creating A Sprite
We need to create our ship so we can shoot bullets. To create an animating sprite, you will be using: Frames per cell will define the number of frames per cell. The animation image is a single image file which contains animation. Sample image can be found in [[Media:Ship.png|here]]. The game resources management is explained in here.

Steps I have taken to create the player's ship is: The following code is an implementation of the steps above.
 * 1) Create a sprite from the ship's image (multi-bitmap animation)
 * 2) Set the ship's x position to the middle of the screen
 * 3) Set the ship's y position (150 pixel offset from the bottom of the screen)
 * 4) Set the ship's speed

Make sure that the LoadGame procedure is called from the MainGame procedure.

Drawing A Sprite
We have created a sprite but we cannot see it yet because we are not drawing. Drawing and updating the animation can be done very easily. Drawing and animation update can be done by: Call these procedures in your loop before the RefreshScreen. I highly recommend you to create a procedure which is dedicated to process everything related to the player to simplify the program.

Screenshot of working animation sprite:

Moving A Sprite
We have got the ship to display but we haven't got the ship to move. I'm going to show how to move a ship properly in three steps.
 * Move left and right
 * Move up and down
 * Move up-right, up-left, down-right and down-left

Moving left and right is the first step to move a player's ship. In order to move a player's ship, we need to… We can the ship left and right by using the following code. Execute this procedure before drawing the player's ship and the ship will react to your key inputs. Now we are going to implement the up and down movement. This is very easy if you understand the left and right movement. The following code will extend the last procedure to detect the up and down movement. We now have a basic movement but we cannot go right-up, left-up and so on. This is the hardest part of the player's ship movement. To move right-up, we need to move the ship by the player's speed. Let's say the player's speed is 4. In this case, we need to move the ship by 4 pixel diagonally. To calculate the movement of X and Y from an angle, we need to come up with a formula. This can be done very easily by using Cos and Sin. This procedure will directly modify the sprite's position. The usage of this procedure is… This example code will move the sprite to up-left by 4 pixel. Finally, we can implement the complete movement code by using the UpdateEntityPosition procedure. We now have a ship movement in any direction! You might notice that the ship can go out of the screen. The procedure that limits the ship's movement can be written very easily. The FixPlayerPosition procedure will check if the player's ship has gone out of the screen and fix the position if it has.
 * 1) Check the key input
 * 2) Move the ship according to the input

Summary
In this tutorial, I have gone through: The current project files can be downloaded from [[Media:EYM_11_01.zip|here]].
 * Creating a sprite
 * Directly manipulating a sprite coordinate
 * Keyboard input processing