Space Invaders Iteration Three

Previous Iteration

Space Invaders Iteration Two

Iteration 3: Draw and move the alien fleet

In this iteration, you will add artefacts to model and move the alien fleet. At the end of this iteration, you will have ten columns and five rows of aliens marching together, threatening your territory.

  1. First, you will create your own MovementDirection enumeration. Table 5 contains a data dictionary that describes the fields of the MovementDirection enumeration.
Value Name MoveLeft Description

MoveLeft

When any alien hit the right screen edge

MoveRight

When any alien hit the left screen edge

Table 5: Fields of MovementDirection enumeration

  1. Next part of building this game will be to create your own Alien record. Table 6 contains a data dictionary that describes the fields of the Alien record/structure.
Type Field Name Description

Boolean

alive

To check if an alien is alive or dead

Sprite

alienSprt

To make the an alien a sprite

Table 6: Fields of Alien record

  1. Next, you will create your own Fleet record. Table 7 contains a data dictionary that describes the fields of the Fleet record/ structure.
Type Field Name Description

Array [0..COLS-1] of Array [0..ROWS-1] of Alien

aliensArray

This will store all the aliens in (0 to COLS - 1) or 10 columns and (0 to ROWS - 1) or 5 rows

Table 7: Fields of Fleet record

  1. Here you have to add another field to your SpaceInvadersData record. Table 8 contains a data dictionary that describes the fields of the SpaceInvadersData record/ structure.
Type Field Name Description

Player

playerData

To store all data of the player

Fleet

fleetData

To store all data of the alien fleet

Table 8: Fields of the SpaceInvadersData record

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

Figure 6: Structure Chart for Iteration 3 with functions and procedures to work with the alien fleet’s data

  1. Write the code for these functions and procedures in GameMain.pas
  • Place Alien (procedure PlaceAlien(var a: Alien; x, y: Integer))
  • This procedure will place each alien to its position. It also starts the animation for the alien bitmaps. The pseudo code for this is as follows:

Constants:

  • COLS is 10
  • ROWS is 5
  • ALIEN_DISTANCE is 40
  • SCREEN_EDGE is 5
  • ALIEN_SPEED is 1


Procedure: Place Alien

Parameters:

  • a (Alien, passed by reference)
  • x (Integer)
  • y (Integer)

Steps:

1: Call Sprite Set X with a’s alienSprt and x

2: Call Sprite Set Y with a’s alienSprt and y

3: Call Sprite Start Animation with a’s alienSprt and the animation name ‘move’

  • Move Alien (procedure MoveAlien(var a: Alien; dx: Single))
  • This procedure will make the aliens move in horizontal direction. The pseudo code for this procedure is as follows:

Procedure: Move Alien

Parameters:

  • a (Alien, passed by reference)

  • dx (Single)

Step:

1: Call Sprite Set Dx with a’s alienSprt and dx

  • Setup Fleet (procedure SetupFleet(var f: Fleet))
  • This procedure will create all alien sprites; it will give the initial position of the sprites and start moving them. This procedure will also say that at this moment all the aliens are alive. The pseudo code for this is as follows:

Procedure: Setup Fleet

Parameters:

  • f (Fleet, passed by reference)

Local variables:

  • i (Integer)
  • xPos (Integer)
  • yPos (Integer)
  • col (Integer)
  • row (Integer)


Steps:

1: Assign xPos, ALIEN_DISTANCE

2: Assign yPos, ALIEN_DISTANCE

3: For col is assigned from 0 to COLS – 1 do

4: For row is assigned from 0 to ROWS – 1 do

5: Assign, f’s aliensArrays[col, row].alienSprt, call Create Sprite with Bitmap Named ‘Invader1’ and Animation Script Named ‘InvaderAnim’

6: Call Place Alien with f’s aliensArray[col, row], xPos and yPos

7: Call Move Alien with f’s aliensArray[col, row] and ALIEN_SPEED

8: Assign f’s aliensArray[col, row].alive, true

9: Assign yPos, add yPos and ALIEN_DISTANCE;

10: Assign yPos, ALIEN_DISTANCE

11: Assign xPos, add xPos and ALIEN_DISTANCE

  1. Alter InitGame by calling SetupFleet procedure. The pseudo code for this is:
Procedure: Init Game

Parameters:

  • data (SpaceInvadersData, passed by reference)

Steps:

1: Call Init Player with data’s playerData

2: Call SetupFleet with data’s fleetData

  • New Alien Direction (procedure NewAlienDirection(var f: Fleet; direction: MovementDirection))
  • This procedure will move the alien towards the right of the screen or at the left of the screen. The pseudo code for this is as follows:

Procedure: New Alien Direction

Parameters:

  • f (Fleet, passed by reference)
  • direction (MovementDirection)

Local Variables:

  • col (Integer)
  • row (Integer)

Steps:

1: For col is assigned from 0 to COLS – 1 do

2: For row is assigned from 0 to ROWS – 1 do

3: If direction is MoveRight then

4: Call MoveAlien with f’s aliensArray[col, row] and ALIEN_SPEED

5: Else, if direction is MoveLeft then

6: Call MoveAlien with f’s aliensArray[col, row] and negative ALIEN_SPEED

  • Check Alien Direction (procedure CheckAlienDirection(var f: Fleet))
  • This procedure will check if any alien hit the right edge or the left edge of the screen. If it does, then it will call the procedure NewAlienDirection. The pseudo code for this is as follows:

Procedure: Check Alien Direction

Parameter:

  • f (Fleet, passed by reference)

Local variables:

  • col (Integer)
  • row (Integer)
  • foundLeftMost (Boolean)
  • foundRightMost (Boolean)

Steps:

1: Assign foundLeftMost, false

2: Assign foundRightMost, false

3: For col is assigned from 0 to COLS – 1 do

4: For row is assigned from 0 to ROWS – 1 do

5: If f’s aliensArray[col, row].alive is true then

6: Assign foundLeftMost, true

7: If call SpriteX with f’s aliensArray[col, row].alienSprt is less than SCREEN_EDGE then

8: Call NewAlienDirection with f and MoveRight

9: If f’s aliensArray[COLS – (col+1), row].alive is true then

10: Assign foundRightMost, true

11: If call SpriteX with f’s aliensArray[COLS – (col + 1), row].alienSprt is greater than (ScreenWidth() – (SPRITE_WIDTH + SCREEN_EDGE) then

">12: Call NewAlienDirection with f and MoveLeft

13: If foundRightMost and foundLeftMost is true then

14: exit

  • Draw And Update Fleet (procedure DrawAndUpdateFleet(var f: Fleet))
  • This procedure will draw and update all the alien sprites. The pseudo code for this is as follows:
Procedure: Draw And Update Fleet

Parameter:

  • f (Fleet, passed by reference)

Local variables:

  • col (Integer)
  • row (Integer)

Steps:

1: For col is assigned from 0 to COLS – 1 do

2: For row is assigned from 0 to ROWS – 1 do

3: If f’s aliensArray[col, row].alive is true then

4: Call DrawSprite with f’s aliensArray[col, row].alienSprt

4 Call UpdateSprite with f’s aliensArray[col, row].alienSprt

  1. Now alter Update Game procedure for moving the aliens. The pseudo code for this is:
Procedure: Update Game

Parameters:

  • data (SpaceInvadersData, passed by reference)

Steps:

1: Call Update Player And Bullet with data’s playerData

2: Call Draw And Update Fleet with data’s fleetData

3: Call Check Alien Direction with data’s fleetData

4: Call Refresh Screen with 60

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

Figure7: Space Invaders at the end of Iteration 3 where all the animated aliens are placed at the top of the screen