Empty Your Mind Tutorial 7

From SwinGame

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.

This page contains a Tutorial. Tutorials are designed to walk you through the development of a small game.
Warning: You must have completed the previous tutorial(s) to go through this tutorial

Contents

Implementing The Enemies

We are going to start implementing the enemies. I will not cover a complex algorithm for enemies in this tutorial. The following things need to be implemented in order to get the enemies to work.

  • Enemy data structure
  • Enemy scheduler
  • Simple enemy bullet pattern

Enemy Data Structure

The enemies' data structure will need the following data in addition to the player ship's data.

  • Time (used for the enemy scheduler)
  • Offset (used for the bullet pattern that will be implementing later)
  • Direction (left to right or right to left)
  • Health
  • Alive (true if the enemy is alive)

As you can see from the list of things needed to implement, the enemy data structure is not very big. Therefore, we will be extending the ShipData which will be used for both player's ship and an enemy's ship. The new implementation of the ShipData:

ShipData = record
	theSprite : Sprite;
	speed : Single;
	health : Integer;
	alive : Boolean;
	shootDelay, currentDelay : Integer;
	maxMagazine, currentMagazine, reloadDelay: Integer;
	//Enemy data
	time, offset : Integer;
	direction : EnemyMovement;
end;

If you read the new implementation of the ShipData record, you will see the new enumeration called the EnemyMovement. This enumeration defines how a enemy enters the screen and exits from the screen. The implementation of the EnemyMovement enumeration:

EnemyMovement = (
	LeftToRight,
	RightToLeft
);

We have finished structuring the enemies' data structure but we have not used it in the GameData structure. We will have to add a new entry into the GameData. The new implementation of the GameData:

GameData = record
	player : ShipData;
	images : Array of Sprite;
	bullets : Array of BulletData;
	sounds : Array of SoundEffect;
	enemies : Array of ShipData;
	music : Music;
end;

As you can see, I have added the array of ShipData which will contain all enemies. This must be initialised in the LoadGame procedure. The initialisation part will be explained in the next section.

Creating An Enemy

We will be creating the enemies using the new data structure we have defined. We will be writing a function that will create an enemy and return the new enemy. The implementation is of the CreateEnemy function is…

function CreateEnemy(yPos, time : Integer; direction : EnemyMovement) : ShipData;
begin
	result.theSprite := CreateSprite(GameImage('Enemy'), 2, 2, 75, 85);
	//Initialise the enemy's position
	result.theSprite.xPos := -1 * result.theSprite.width;
	result.theSprite.yPos := 0;
	result.speed := 1;
	result.health := 50;
	result.time := time;
	result.alive := true;
	result.shootDelay := 4;
	result.currentDelay := result.shootDelay;
	result.maxMagazine := 7;
	result.currentMagazine := result.maxMagazine;
	result.reloadDelay := 15;
	result.offset := -75;
	result.direction := direction;
	result.theSprite.yPos := yPos;
	//Fix the enemy's position if the direction is RightToLeft
	if result.direction = RightToLeft then begin
		result.theSprite.xPos := SCREENWIDTH;
		result.speed := result.speed * -1;
	end;
end;

The example image I have created can be found here. Details of the offset and the time will be explained later. Most of the enemy details are static for now. You may extend the game by adding more flexibility.

Continue to the next tutorial…

List Of Tutorials

  1. Introduction
  2. Player Ship
  3. Background
  4. Bullet pt.1
  5. Bullet pt.2
  6. Music and SoundEffect
  7. Enemies pt.1
  8. Enemies pt.2
  9. Collision Detection
  10. Further Extensions