Empty Your Mind Tutorial 6

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

Playing A Music

Music playback can be done very easily. To load a music you will need to use the Game Resources. The loaded music data can be obtained by calling GameMusic routine.

Note: I highly recommend you to use a mp3 as your music format

The game's music should be contained in the GameData structure to simplify the data structure of our game. The following code is the new implementation of the GameData structure.

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

You will need to assign the music entry in your LoadGame routine so you can play the music when the game starts. The loaded music can b played by calling the PlayMusic routine.

To sptop the music you need to call the StopMusic routine. This routine does not take any parameters.

The PlayMusic routine must be called before the game loop. The StopMusic routine must be called after the game loop. Thefore, your MainGame routine should look like something similar to this…

procedure MainGame();
var
	game : GameData;
begin
	LoadGame(game);
	PlayMusic(game.music);
	repeat
		ProcessEvents();
		UpdateBackground(game.images);
		UpdateShip(game);
		RefreshScreen();
	until WindowCloseRequested();
	StopMusic();
end;

Playing A Sound Effect

Sound effect can be played easily like the music. Sound effect should also be loaded from the Game Resources. To manage the sound effect, we will need to expand the GameData structure again. This can be done by creating an array of sound effect. You will need to initialise the array and assign the sound effects to the array. The new GameData structure should look something similar to this…

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

Sound effect can be played very easily by calling PlaySoundEffect routine. You will need to pass the sound effect you have loaded from the Game Resources by calling GameSound routine. Therefore, you will need to insert the following code into your ShootPlayerBullet to play the bullet sound.

PlaySoundEffect(game.sounds[0]);

The example sound effect data can be found here.

Summary

In this tutorial, I have gone through:

  • Playing a music
  • Playing a sound effect

The current project files can be downloaded from here.

Note: The music I have used in my game is P8107 by onoken

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