Space Invaders Iteration One

Project “Space Invaders”

ConceptsCovered

  • Programs, Functions, Procedures
  • Global Constants, and Local Variables
  • Parameters, with Pass by Value and Pass by Reference
  • Control Flow, using loops and branches (sequence, selection, repetition)
  • Arrays, Custom data types (struct)
  • String and text processing
  • Managing multiple values
  • Custom data types, record/struct and enumeration

Project Description

In this tutorial, you are going to make the most popular arcade game ever. This game will have some alien ships arranged in a perfect block with columns and rows on top of the screen. Your ship is at the bottom of the screen from where you have to shoot at the alien ships and protect your ship from their bullets. You will have three lives, if you kill all the aliens before your lives run out you win, if the alien kills you three times, they win.

See figure 1 for an example

The screen shows three main elements. They are:

  1. Player ship. You need to protect your ship from alien bullets and throw bullets from your ship to destroy all the aliens.
  2. The aliens. Aliens will be at the top of the screen.
  3. Bullets. Both your ship and the aliens’ ship will throw bullets at each other. Whatever the bullet hits, it is destroyed.

Implement this game using the “Pascal” version of SwinGame. The following pages outline the process of designing and implementing this.

Getting Started

  • Create a new folder on your computer or on the H:\ drive for the lab machines
  • Download a blank “SwinGame template” from the Blackboard web site.
  • Extract this into the folder you created, and rename the “Project Template” to “SpaceInvaders”
  • Open MinGW Shell (Start > Programs > Utilities > MinGW), and “cd” to the SpaceInvaders folder.
  • Open NotePad++, and in it open GameMain.pas for the SpaceInvaders (.../SpaceInvaders/src/GameMain.pas)

Iterations

Iteration 1: Creating your ship

Perform the following steps to build the artefacts needed to be able to create and move your ship.

  1. First, you will create your own player record. Table 2 contains a data dictionary that describes the fields of the player record/structure.

Type Field Name Description

Sprite

Fighter

To make the player a sprite

Integer

Life

Number of lifes a player have

Table 1: Fields of the player record


  1. Next part of building this game will be to create your own SpaceInvadersData record. Table 2 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

Table 2: Fields of the SpaceInvadersData record

  1. In GameMain.pas write the code to declare this record/structure.

  1. Next, you need to create functions and procedures to process all events, initialize the game data, draw the player, update the player and move the player. See figure 3 for the structure chart showing these functions and procedures.

Figure 3: Iteration 1 structure chart- showing creating and moving the player

  1. Write the code for these functions and procedures in GameMain.pas.


  • Init Player (procedure InitPlayer(var p: Player))

  • This procedure will initialize the player by creating a sprite with a bitmap and then place that sprite in a particular position of the screen. The pseudo code for this is as follows:

Constants:

  • PLAYER_LIFE is 3
  • PLAYER_X_POS is 300
  • PLAYER_Y_POS is 410
  • SPRITE_WIDTH is 40
  • PLAYER_SPEED is 2

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: Assign p’s life, PLAYER_LIFE


  • Init Game(procedure InitGame(var data: SpaceInvadersData))

  • This procedure will initialize the game. It will call all the procedures to setup the entire game.

Procedure: Init Game

Parameters:

  • data (SpaceInvadersData, passed by reference)


Steps:

1: Call Init Player with data’s playerData

  • Handle Input (procedure HandleInput(var p: Player))

  • This procedure will take the input and move the player accordingly.


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

  • Update Player And Bullet (UpdatePlayerAndBullet(var p: Player))

  • This procedure will update the player and the bullets that the player throws. But for now the concern is to just update the player so the player can move in either direction.


Procedure: Update Player And Bullet

Parameters:

  • p (Player, passed by reference)


Steps:

1: Call Update Sprite with p’s fighter

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


  • Draw Player And Bullet (procedure DrawPlayerAndBullet(var p: Player))

  • This procedure will draw the player and the player’s bullet. But for now the concern is to just draw the player


Procedure: Draw Player And Bullet

Parameters:

  • p (Player, passed by reference)


Steps:

1: Call Draw Sprite with p’s fighter


  • Update Game (procedure UpdateGame(var data: SpaceInvadersData))

  • This procedure will update all the game data.


Procedure: Update Game

Parameters:

  • data (SpaceInvadersData, passed by reference)


Steps:

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

2: Call Refresh Screen with 60


  • Draw Game (procedure DrawGame(var data: SpaceInvadersData))
  • This procedure will draw the whole game each time it is called.

Procedure: Draw Game

Parameters:

  • data (SpaceInvadersData, passed by reference)


Steps:

1: Call Clear Screen

2: Call Draw Bitmap with Bitmap Named ‘Background’ and 0, 0 as the x and y position of the bitmap

3: Call Draw Framerate with 10, 8

4: Draw Player And Bullet with data’s playerData


  1. The last step for this iteration will be to process all the events that you have just created.


Procedure: Main

Local variables:

  • gameData (SpaceInvadersData)


Steps:

1: Call Open Audio

2: Call Open Graphics Window with the text ‘Space Invaders’ and set the window size as 640, 480

3: Call Load Resource Bundle with ‘SpaceInvaders.txt’

4: Call Init Game with gameData

5: repeat

6: Call Draw Game with gameData

7: Call Update Game with gameData

8: Call Process Events

9: Call Handle Input with gameData’s playerData

10: until WindowCloseRequested()

  1. Call the Main() procedure

begin

Main();

end;

  1. Compile the program, and correct any syntax errors.

  1. Run the program and you should see something like this:


Figure 4: Space Invaders game after Iteration 1