Space Invaders Iteration Two

Previous Iteration

Space Invaders Iteration One

Iteration 2: Draw and move player’s bullet

In this iteration you will add artefacts to model and move the bullet that the player is going to throw. At the end of this iteration you will have a bullet shooting from the player ship when the space bar is pressed.

  1. First you will create your ownbulletrecord. Table 3 contains a data dictionary that describes the fields of the bullet record/structure.


Type Field Name Description

Sprite

bulletSprt

To make the bullet a sprite

Boolean

inUse

To see if the current bullet is active

Table 3: Data dictionary for the space invaders Bullet record

  1. Modify the Player record and add another field in that record to store information about the player’s bullet. Table 4 contains a data dictionary that describes the additional field of the Player record/structure.
Type Field Name Description

Sprite

Fighter

To make the player a sprite

Bullet

playerBlt

To store information about player’s bullet

Integer

Life

Number of lives a player have

Table 4: Data dictionary for the space invaders Player record

  1. Next we need to create functions and procedures to interact with the bullet’s data.

Figure 4: Structure Chart for Iteration 2 with functions and procedures to work with the bullet’s data

  1. Write the code for these functions and procedures in GameMain.pas
  • Setup Bullet (procedure SetupBullet(var b: Bullet; isPlayers: Boolean))
  • This procedure will initialise the bullet by creating a sprite with a bitmap. Then it initialise the speed of the bullet and make the bullet inactive. The pseudo code for this is as follows:

Constant:

  • BULLET_X_OFFSET is 12
  • BULLET_SPEED is 4
  • PLAYER_BULLET_Y_DIFF is 1

Procedure: Setup Bullet

Parameters:

  • b (Bullet, passed by reference)
  • isPlayers (Boolean)

Steps:

1: Assign b’s inUse, false

2: If isPlayers is true then

3: Assign b’s bulletSprt, Call Create Sprite and as parameter call Bitmap Named and pass the bitmap called ‘PlayerBullet’ as parameter.

4: Call Sprite Set Dy with b’s bulletSprt and negative BULLET_SPEED.

  1. Alter InitPlayer procedure by calling Setup Bullet. The pseudo code for this is:

Procedure: Init Player

Parameters:

  • p (Player, passed by reference)

Steps:

1: Assign p’s fighter, Call Create Sprite and as parameter call Bitmap Named and pass the bitmap called ‘Player’ as parameter.

2: Call Sprite Set X with p’s fighter and PLAYER_X_POS.

3: Call Sprite Set Y with p’s fighter and PLAYER_Y_POS.

4: Call SetupBullet with p’s playerBlt and true

5: Assign p’s life, PLAYER_LIFE

  • Fire Bullet (procedure FireBullet(var b: Bullet; s: Sprite; isPlayer:Boolean))
  • When this function will be called, it will make the bullet active and give the initial position for the bullet in relation to the player’s position


Procedure: Fire Bullet

Parameters:

  • b (Bullet, passed by reference)
  • s (Sprite)
  • isPlayer (Boolean)

Steps:

1: Assign b’s inUse, true

2: Call Sprite Set X with b’s bulletSprt and addition of BULLET_SET_OFFSET and calling Sprite X with s

3: If isPlayer is true then

4: Call Sprite Set Y with b’s bulletSprt and subtract PLAYER_BULLET_Y_DIFF from calling Sprite Y with s.

  1. Now alter Handle Input procedure by checking if the player pressed space bar. The pseudo code for this is:

Procedure: Handle Input

Parameters:

  • p (Player, passed by reference)


Steps:

1: If KeyDown(VK_RIGHT) is true then

2: If, call Sprite X with p’s fighter is less than, call ScreenWidth minus SPRITE_WIDTH, is true then

3: Call Sprite Set Dx with p’s fighter and PLAYER_SPEED

4: Else if KeyDown(VK_LEFT) is true then

5: If, call Sprite X with p’s fighter is greater than 0, is true then

6: Call Sprite Set Dx with p’s fighter and –PLAYER_SPEED

7: If KeyDown(VK_SPACE) is true then

8: Call Fire Bullet with p’s playerBlt, p’s fighter and true

  1. Now alter Update Player And Bullet procedure by updating the bullet as well. The pseudo code for this is:

Procedure: Update Player And Bullet

Parameters:

  • p (Player, passed by reference)

Steps:

1: Call Update Sprite with p’s fighter

2: If p’s playerBlt’s inUse is true then

3: Call Update Sprite with p’s playerblt’s bulletSprt

4: Call Sprite Set Dx with p’s fighter and 0

  1. Alter Draw Player And Bullet procedure by drawing the bullet as well. The pseudo code for this is:

Procedure: Draw Player And Bullet

Parameters:

  • p (Player, passed by reference)

Steps:

1: Call Draw Sprite with p’s fighter

2: If p’s playerBlt’s inUse is true then

3: Call Draw Sprite with p’s playerBlt’s bulletSprt

  1. Compile the program, and correct any syntax errors.
  2. Run the program and you should see something like this:

Figure 5: Space Invaders at the end of Iteration 2 where if you press space bar the fighter will shoot a bullet