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.
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.
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.
List Of Tutorials
- Introduction
- Player Ship
- Background
- Bullet pt.1
- Bullet pt.2
- Music and SoundEffect
- Enemies pt.1
- Enemies pt.2
- Collision Detection
- Further Extensions

