October 23, 2016
Hot Topics:

Using Graphics: Making a Lander Game - Part 2

  • November 19, 2002
  • By John Percival
  • Send Email »
  • More Articles »

Since VB has no functions to play WAV files, we must again look to the API to satify our needs. We will be using the sndPlaySound API. Here are the declarations:

Private Declare Function sndPlaySound _
  Lib "winmm.dll" Alias "sndPlaySoundA" _
  (ByVal lpszSoundName As String, _
  ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1

As you can see, this function takes two parameters. The first is the name of the sound file to play, and the second can be used to specify certain options for the playing of the sound. We will be using the SND_ASYNC flag, which allows us to call the function, and it will return straight away, letting us get on while the sound is playing. This is know as playing "asynchronously", as opposed to "synchronously", which plays the entire sound before returning.

Since we are going to be calling the sound file when any of the thrusters are applied, either left, right, or up, and we don't want to repeat all the code, we will be using a variable to specify whether the sound should be played. At the end of the timer event, we should check to see if the sound is currently playing, and if it is not, play it.

At the begginging of the timer event, we need two new variables:

Dim dothrust As Boolean
Static lastplayed As Long

dothrust will be set to true if one of the arrows is pressed, thus telling the program to play sound. lastplayed stores the value from GetTickCount when the sound was last played. This will be used to check whether the sound is currently playing.

The first change is to go through and add

dothrust = True
everywhere the thrust can be applied. I recommend adding it after this line:

If Fuel < 0 Then Fuel = 0
' Add:
dothrust = True

Then at the end of the timer function, add this code to play the sound:

If dothrust = True Then
' if one of the thrusters has been pressed...
If timenow - lastplayed > 805 Then
' and it is more than 805ms since the sound last played
' ( the length of the sound is 805ms ) ...
' then play the sound again!
sndPlaySound "thrust.wav", SND_ASYNC
lastplayed = timenow
End If
End If

The thrust sound that I used was about 805ms long, hence the 805 in the code. If you decide to use a different one, you may want to change that value accordingly. Also, the file shoud be in the same directory as the project, unless you specify the full path.

Just one more feature this week to make our game fiendishly difficult!

Page 3 of 5

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel