Introduction
Written by JustinDaubenmire
This tutorial shows you how to speak a users score in a game. It assumes the following:
you are using Visual Studio 2003
you have the DirectX version 9 SDK installed.
You have a new project already started named SpeakScore with form1.vb in it
You have a reference set to Microsoft.DirectX and Microsoft.DirectX.DirectSound in your project. If you don't know how to do this, see the tutorial
Playing a sound effect. You have the following wav files that speak the associated number:
0.wav, 1.wav, 2.wav, 3.wav, 4.wav, 5.wav, 6.wav, 7.wav, 8.wav, 9.wav, 10.wav
11.wav, 12.wav, 13.wav, 14.wav, 15.wav, 16.wav, 17.wav, 18.wav, 19.wav
20.wav, 30.wav, 40.wav, 50.wav, 60.wav, 70.wav, 80.wav, 90.wav
hundred.wav, thousand.wav, million.wav, points.wav.
If you do not have those wav files, you can download my version of them for free using this link score_numbers.zip (500 kb).
And if you use them in your game or alter them to fit better in your game, please mention a thanks to "Justin Daubenmire from BSC Games" in the credit section of your game documentation is all I ask *grin*.
If you do not have the DirectX 9 SDK installed on your computer, prior to trying this source code, please download
The DirectX 9 SDK and install it.
To use this code, you have to add a class to your SpeakScore project since this code goes in a class. To do this, go to projects by pressing alt+p and then press the letter c to activate the item Add class. It will give you a file name of class1.vb so rename it to SpeakScore.vb then press enter. You now will have SpeakScore.vb in your solution explorer along with form1.vb.
Since we are using a class, we have to create a test application to use the class. We'll use form1 for this situation. I'll show you that in a minute but first, you have to add the score speaking code to the class. Here is the code, copy and paste all of it into the file SpeakScore.vb:
*** begin code ***
Code for Speak``Score Class
'================================================
'
' SpeakScore.vb
'
' Written by Justin Daubenmire, March 2005
' ©2005 BSC Games - All Rights Reserved
' http://www.BscGames.com
' Description: This class contains all the defined functions for speaking scores within an audio game.
' Simply call the SpeakScore function passing it a string of the score to be spoken
' .wav files to be used are as follows
' 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
' 20, 30, 40, 50, 60, 70, 80, 90
' hundred, thousand, million, points
''
'================================================
Imports Microsoft.DirectX.DirectSound
Public Class SpeakScore
' sound card device
' setup in constructor
Private oDev As Device
' sound buffer description
' setup in constructor
Private oBufferDescription As BufferDescription
' sound buffers
' loaded in constructor
Private o0 As SecondaryBuffer = Nothing
Private o1 As SecondaryBuffer = Nothing
Private o2 As SecondaryBuffer = Nothing
Private o3 As SecondaryBuffer = Nothing
Private o4 As SecondaryBuffer = Nothing
Private o5 As SecondaryBuffer = Nothing
Private o6 As SecondaryBuffer = Nothing
Private o7 As SecondaryBuffer = Nothing
Private o8 As SecondaryBuffer = Nothing
Private o9 As SecondaryBuffer = Nothing
Private o10 As SecondaryBuffer = Nothing
Private o11 As SecondaryBuffer = Nothing
Private o12 As SecondaryBuffer = Nothing
Private o13 As SecondaryBuffer = Nothing
Private o14 As SecondaryBuffer = Nothing
Private o15 As SecondaryBuffer = Nothing
Private o16 As SecondaryBuffer = Nothing
Private o17 As SecondaryBuffer = Nothing
Private o18 As SecondaryBuffer = Nothing
Private o19 As SecondaryBuffer = Nothing
Private o20 As SecondaryBuffer = Nothing
Private o30 As SecondaryBuffer = Nothing
Private o40 As SecondaryBuffer = Nothing
Private o50 As SecondaryBuffer = Nothing
Private o60 As SecondaryBuffer = Nothing
Private o70 As SecondaryBuffer = Nothing
Private o80 As SecondaryBuffer = Nothing
Private o90 As SecondaryBuffer = Nothing
Private oHundred As SecondaryBuffer = Nothing
Private oThousand As SecondaryBuffer = Nothing
Private oMillion As SecondaryBuffer = Nothing
Private oPoints As SecondaryBuffer = Nothing
' ***
' Function name: Constructor
' Parameters:
' Parameter 1: a form object to bind the DirectSound objects to
' Parameter 2: a string holding the path to the sound files
' returns: none
' ***
Public Sub New(ByVal frm As Form, ByVal strPathToSounds As String)
If frm Is Nothing Then
MessageBox.Show("Error in speak score constructor: the parameter form is required.")
Exit Sub
End If
If strPathToSounds = "" Then
MessageBox.Show("Error in speak score constructor: the parameter, physical path to the sound files, is required.")
Exit Sub
End If
' test for back slashin path to sound files
' if there is not one at the end, add one
If strPathToSounds.Substring(strPathToSounds.Length - 1, 1) <> "\" Then
strPathToSounds = strPathToSounds & "\"
End If
Try
' setup sound card
oDev = New Device
oDev.SetCooperativeLevel(frm, CooperativeLevel.Priority)
' setp buffer description
oBufferDescription = New BufferDescription
oBufferDescription.ControlFrequency = True
oBufferDescription.ControlPan = True
oBufferDescription.ControlVolume = True
oBufferDescription.GlobalFocus = True
' load the buffers
o0 = New SecondaryBuffer(strPathToSounds & "0.wav", oBufferDescription, oDev)
o1 = New SecondaryBuffer(strPathToSounds & "1.wav", oBufferDescription, oDev)
o2 = New SecondaryBuffer(strPathToSounds & "2.wav", oBufferDescription, oDev)
o3 = New SecondaryBuffer(strPathToSounds & "3.wav", oBufferDescription, oDev)
o4 = New SecondaryBuffer(strPathToSounds & "4.wav", oBufferDescription, oDev)
o5 = New SecondaryBuffer(strPathToSounds & "5.wav", oBufferDescription, oDev)
o6 = New SecondaryBuffer(strPathToSounds & "6.wav", oBufferDescription, oDev)
o7 = New SecondaryBuffer(strPathToSounds & "7.wav", oBufferDescription, oDev)
o8 = New SecondaryBuffer(strPathToSounds & "8.wav", oBufferDescription, oDev)
o9 = New SecondaryBuffer(strPathToSounds & "9.wav", oBufferDescription, oDev)
o10 = New SecondaryBuffer(strPathToSounds & "10.wav", oBufferDescription, oDev)
o11 = New SecondaryBuffer(strPathToSounds & "11.wav", oBufferDescription, oDev)
o12 = New SecondaryBuffer(strPathToSounds & "12.wav", oBufferDescription, oDev)
o13 = New SecondaryBuffer(strPathToSounds & "13.wav", oBufferDescription, oDev)
o14 = New SecondaryBuffer(strPathToSounds & "14.wav", oBufferDescription, oDev)
o15 = New SecondaryBuffer(strPathToSounds & "15.wav", oBufferDescription, oDev)
o16 = New SecondaryBuffer(strPathToSounds & "16.wav", oBufferDescription, oDev)
o17 = New SecondaryBuffer(strPathToSounds & "17.wav", oBufferDescription, oDev)
o18 = New SecondaryBuffer(strPathToSounds & "18.wav", oBufferDescription, oDev)
o19 = New SecondaryBuffer(strPathToSounds & "19.wav", oBufferDescription, oDev)
o20 = New SecondaryBuffer(strPathToSounds & "20.wav", oBufferDescription, oDev)
o30 = New SecondaryBuffer(strPathToSounds & "30.wav", oBufferDescription, oDev)
o40 = New SecondaryBuffer(strPathToSounds & "40.wav", oBufferDescription, oDev)
o50 = New SecondaryBuffer(strPathToSounds & "50.wav", oBufferDescription, oDev)
o60 = New SecondaryBuffer(strPathToSounds & "60.wav", oBufferDescription, oDev)
o70 = New SecondaryBuffer(strPathToSounds & "70.wav", oBufferDescription, oDev)
o80 = New SecondaryBuffer(strPathToSounds & "80.wav", oBufferDescription, oDev)
o90 = New SecondaryBuffer(strPathToSounds & "90.wav", oBufferDescription, oDev)
oHundred = New SecondaryBuffer(strPathToSounds & "hundred.wav", oBufferDescription, oDev)
oThousand = New SecondaryBuffer(strPathToSounds & "thousand.wav", oBufferDescription, oDev)
oMillion = New SecondaryBuffer(strPathToSounds & "million.wav", oBufferDescription, oDev)
oPoints = New SecondaryBuffer(strPathToSounds & "points.wav", oBufferDescription, oDev)
Catch ex As Exception
MessageBox.Show("Error in speak score constructor. " & ex.Message)
Exit Sub
End Try
End Sub
' ***
' Function name: Dispose
' Description: used to clean up objects prior to .net garbage collection
' if desired
' Parameters: none
' returns: none
' ***
Public Sub Dispose()
o0.Dispose()
o1.Dispose()
o2.Dispose()
o3.Dispose()
o4.Dispose()
o5.Dispose()
o6.Dispose()
o7.Dispose()
o8.Dispose()
o9.Dispose()
o10.Dispose()
o11.Dispose()
o12.Dispose()
o13.Dispose()
o14.Dispose()
o15.Dispose()
o16.Dispose()
o17.Dispose()
o18.Dispose()
o19.Dispose()
o20.Dispose()
o30.Dispose()
o40.Dispose()
o50.Dispose()
o60.Dispose()
o70.Dispose()
o80.Dispose()
o90.Dispose()
oHundred.Dispose()
oThousand.Dispose()
oMillion.Dispose()
oPoints.Dispose()
oBufferDescription.Dispose()
oDev.Dispose()
End Sub
' ***
' Function name: SpeakScore
' Parameters:
' Parameter 1: string that holds score to be spoken
' returns: none
' ***
Public Function SpeakScore(ByVal strScore As String)
' based on length of score
' call appropriate function to start parsing and processing the string
If strScore.Trim.Length = 0 Then
Exit Function
End If
Select Case strScore.Length
Case 1 ' one digit
Me.OneDigit(strScore)
Case 2 ' ten digit
Me.TenDigit(strScore)
Case 3 ' hundred digit
Me.HundredDigit(strScore)
Case 4 ' thousand digit
Me.ThousandDigit(strScore)
Case 5 ' ten thousand digit
Me.TenThousandDigit(strScore)
Case 6 ' hundred thousand digit
Me.HundredThousandDigit(strScore)
Case 7 ' one million digit
Me.OneMillionDigit(strScore)
Case 8 ' ten million digit
Me.TenMillionDigit(strScore)
Case 9 ' hundred million digit
Me.HundredMillionDigit(strScore)
End Select
' finished speaking all numbers so now speak points
oPoints.Play(0, BufferPlayFlags.Default)
Do While oPoints.Status.Playing = True
Application.DoEvents()
Loop
End Function
' ***
' Function name: OneDigit
' Parameters:
' Parameter 1: string holding number to be spoken
' Returns: none
' ***
Private Function OneDigit(ByVal strScore As String)
' see what single digit we need to speak
' and while speaking it, loop and do not return
' Application.DoEvents yields flow to the application so it does not lock up
' and prevent other game events from processing
' this prevents numbers being spoken overtop of one another
Select Case strScore
Case 0
o0.Play(0, BufferPlayFlags.Default)
Do While o0.Status.Playing = True
Application.DoEvents()
Loop
Case 1
o1.Play(0, BufferPlayFlags.Default)
Do While o1.Status.Playing = True
Application.DoEvents()
Loop
Case 2
o2.Play(0, BufferPlayFlags.Default)
Do While o2.Status.Playing = True
Application.DoEvents()
Loop
Case 3
o3.Play(0, BufferPlayFlags.Default)
Do While o3.Status.Playing = True
Application.DoEvents()
Loop
Case 4
o4.Play(0, BufferPlayFlags.Default)
Do While o4.Status.Playing = True
Application.DoEvents()
Loop
Case 5
o5.Play(0, BufferPlayFlags.Default)
Do While o5.Status.Playing = True
Application.DoEvents()
Loop
Case 6
o6.Play(0, BufferPlayFlags.Default)
Do While o6.Status.Playing = True
Application.DoEvents()
Loop
Case 7
o7.Play(0, BufferPlayFlags.Default)
Do While o7.Status.Playing = True
Application.DoEvents()
Loop
Case 8
o8.Play(0, BufferPlayFlags.Default)
Do While o8.Status.Playing = True
Application.DoEvents()
Loop
Case 9
o9.Play(0, BufferPlayFlags.Default)
Do While o9.Status.Playing = True
Application.DoEvents()
Loop
End Select
End Function
' ***
' Function name: TenDigit
' Parameters:
' Parameter 1: string holding two digit score to be spoken
' Returns: none
' ***
Private Function TenDigit(ByVal strScore As String)
' ***
' we need to test for 3 different values in this function
' based on the first digit in the number
' if the first digit is a 0 we ignore it and do nothing
' for example 09 would not be spoken zero 9
' it would be spoken as 9
' so if the first digit is a 0, we skip over the first if condition and process the 9 at the second if conditionin the function
' passing the 9 to the OneDigit function for processing
' secondly, we test for a 1
' if the first digit is a 1, then we know the number is from 10 to 19
' so we use the first if condition to speak the number from 10 to 19
' thirdly, if the first digit is not a 1 or 0, for example 2,3,4,5,6,7,8,9
' then we use the else condition in the first if test
' to process that number
' I.E 2 speak 20, 3 speak 30, and so on
' Finally, the second if condition in this function tests the second digit
' in the number and if it is not 0, then it must be spoken. For example 23
' if it needs spoken, we extract the single digit, 3 from 23, and pass it to the OneDigit function
' to be spoken
' ***
' if the first digit is a 1 and not a 0 then process it
If strScore.Substring(0, 1) = "1" And strScore.Substring(0, 1) <> "0" Then ' not 0 and is a 10 or a teen number
Select Case strScore
Case 10
o10.Play(0, BufferPlayFlags.Default)
Do While o10.Status.Playing = True
Application.DoEvents()
Loop
Case 11
o11.Play(0, BufferPlayFlags.Default)
Do While o11.Status.Playing = True
Application.DoEvents()
Loop
Case 12
o12.Play(0, BufferPlayFlags.Default)
Do While o12.Status.Playing = True
Application.DoEvents()
Loop
Case 13
o13.Play(0, BufferPlayFlags.Default)
Do While o13.Status.Playing = True
Application.DoEvents()
Loop
Case 14
o14.Play(0, BufferPlayFlags.Default)
Do While o14.Status.Playing = True
Application.DoEvents()
Loop
Case 15
o15.Play(0, BufferPlayFlags.Default)
Do While o15.Status.Playing = True
Application.DoEvents()
Loop
Case 16
o16.Play(0, BufferPlayFlags.Default)
Do While o16.Status.Playing = True
Application.DoEvents()
Loop
Case 17
o17.Play(0, BufferPlayFlags.Default)
Do While o17.Status.Playing = True
Application.DoEvents()
Loop
Case 18
o18.Play(0, BufferPlayFlags.Default)
Do While o18.Status.Playing = True
Application.DoEvents()
Loop
Case 19
o19.Play(0, BufferPlayFlags.Default)
Do While o19.Status.Playing = True
Application.DoEvents()
Loop
End Select
Else
' not a ten or teen number its 20 or higher
' In the select case get the first digit to see which number to speak
' for example, 2 you speak 20, 3 you speak 30, etc.
' after the select case structure
' you test the second digit and if it is not a 0
' then you extract it and pass it to the function OneDigit to be spoken
Select Case strScore.Substring(0, 1)
Case 2
o20.Play(0, BufferPlayFlags.Default)
Do While o20.Status.Playing = True
Application.DoEvents()
Loop
Case 3
o30.Play(0, BufferPlayFlags.Default)
Do While o30.Status.Playing = True
Application.DoEvents()
Loop
Case 4
o40.Play(0, BufferPlayFlags.Default)
Do While o40.Status.Playing = True
Application.DoEvents()
Loop
Case 5
o50.Play(0, BufferPlayFlags.Default)
Do While o50.Status.Playing = True
Application.DoEvents()
Loop
Case 6
o60.Play(0, BufferPlayFlags.Default)
Do While o60.Status.Playing = True
Application.DoEvents()
Loop
Case 7
o70.Play(0, BufferPlayFlags.Default)
Do While o70.Status.Playing = True
Application.DoEvents()
Loop
Case 8
o80.Play(0, BufferPlayFlags.Default)
Do While o80.Status.Playing = True
Application.DoEvents()
Loop
Case 9
o90.Play(0, BufferPlayFlags.Default)
Do While o90.Status.Playing = True
Application.DoEvents()
Loop
End Select
' now is the second digit in the two digits a 0?
' if not, we must extract the single digit and pass it to the OneDigit function
If strScore.Substring(1, 1) <> "0" Then
Me.OneDigit(strScore.Substring(1, 1))
End If
End If
End Function
' ***
' Function name: HundredDigit
' Parameters:
' Parameter 1: string holding the 3 digit score to be spoken
' Returns: none
' ***
Private Function HundredDigit(ByVal strScore As String)
' based on the first number in the string
' for example 198
' extract the 1 from 198 and call OneDigit
' passing the single digit as a parameter
' if the first digit is 0, it does not need spoke
' for example 098
' so extract the last two digits and pass them to TenDigit function
' for processing
If strScore.Substring(0, 1) = "0" Then
Me.TenDigit(strScore.Substring(1, 2))
Exit Function
End If
' first digit is not a 0 so extract it
' for example 1 from 198
' and pass it to the OneDigit function for processing
Me.OneDigit(strScore.Substring(0, 1))
' speak hundred now
oHundred.Play(0, BufferPlayFlags.Default)
Do While oHundred.Status.Playing = True
Application.DoEvents()
Loop
' now call TenDigit passing it the 2 digits from the hundred value
' I.E if it was 198 extract 98
' we use substring to extract the two digits and then pass them
' to the function TenDigit
Me.TenDigit(strScore.Substring(1, 2))
End Function
' ***
' Function name: ThousandDigit
' Parameters:
' Parameter 1: string holding the 4 digit score to be spoken
' Returns: none
' ***
Private Function ThousandDigit(ByVal strScore As String)
' based on the first number in the string
' for example 2399
' extract the 2 from 2399 and call OneDigit
' passing the single digit as a parameter
Me.OneDigit(strScore.Substring(0, 1))
' speak thousand now
oThousand.Play(0, BufferPlayFlags.Default)
Do While oThousand.Status.Playing = True
Application.DoEvents()
Loop
' now extract the hundred digits
' using SubString
' for example, 399 from 2399
' and pass them to the HundredDigit function
' to be spoken
Me.HundredDigit(strScore.Substring(1, 3))
End Function
' ***
' Function name: TenThousandDigit
' Parameters:
' Parameter 1: string that holds 5 digit score to be spoken
' returns: none
' ***
Private Function TenThousandDigit(ByVal strScore As String)
' based on the first two numbers in the string
' for example 59288
' extract the 59 from 59288 and call TenDigit
' passing the two digits as a parameter
Me.TenDigit(strScore.Substring(0, 2))
' speak thousand now
oThousand.Play(0, BufferPlayFlags.Default)
Do While oThousand.Status.Playing = True
Application.DoEvents()
Loop
' now extract the hundred digits
' using SubString
' for example, 288 from 59288
' and pass them to the HundredDigit function
' to be spoken
Me.HundredDigit(strScore.Substring(2, 3))
End Function
' ***
' Function name: HundredThousandDigit
' Parameters:
' Parameter 1: string that holds 6 digit score to be spoken
' returns: none
' ***
Private Function HundredThousandDigit(ByVal strScore As String)
' based on the first three numbers in the string
' for example 400499
' extract the 400 from 400499 and call HundredDigit
' passing the three digits as a parameter
Me.HundredDigit(strScore.Substring(0, 3))
' speak thousand now
oThousand.Play(0, BufferPlayFlags.Default)
Do While oThousand.Status.Playing = True
Application.DoEvents()
Loop
' now extract the hundred digits
' using SubString
' for example, 499 from 400499
' and pass them to the HundredDigit function
' to be spoken
Me.HundredDigit(strScore.Substring(3, 3))
End Function
' ***
' Function name: OneMillionDigit
' Parameters:
' Parameter 1: string that holds 7 digit score to be spoken
' returns: none
' ***
Private Function OneMillionDigit(ByVal strScore As String)
' based on the first number in the string
' for example 1399299
' extract the 1 from 1399299 and call OneDigit
' passing the single digit as a parameter
Me.OneDigit(strScore.Substring(0, 1))
' speak million now
oMillion.Play(0, BufferPlayFlags.Default)
Do While oMillion.Status.Playing = True
Application.DoEvents()
Loop
' based on the three numbers in the hundred thousand position in the string
' for example 399 from 1399299
' extract the 399 from 1399299 and call HundredDigit
' passing the three digits as a parameter
Me.HundredDigit(strScore.Substring(1, 3))
' speak thousand now
oThousand.Play(0, BufferPlayFlags.Default)
Do While oThousand.Status.Playing = True
Application.DoEvents()
Loop
' now extract the hundred digits
' using SubString
' for example, 299 from 1399299
' and pass them to the HundredDigit function
' to be spoken
Me.HundredDigit(strScore.Substring(4, 3))
End Function
' ***
' Function name: TenMillionDigit
' Parameters:
' Parameter 1: string that holds 8 digit score to be spoken
' returns: none
' ***
Private Function TenMillionDigit(ByVal strScore As String)
' based on the first two digits in the string
' for example 88399299
' extract the 88 from 88399299and call TenDigit
' passing the two digits as parameters
Me.TenDigit(strScore.Substring(0, 2))
' speak million now
oMillion.Play(0, BufferPlayFlags.Default)
Do While oMillion.Status.Playing = True
Application.DoEvents()
Loop
' based on the three numbers in the hundred thousand position in the string
' for example 399 from 88399299
' extract the 399 from 88399299and call HundredDigit
' passing the three digits as a parameter
Me.HundredDigit(strScore.Substring(2, 3))
' speak thousand now
oThousand.Play(0, BufferPlayFlags.Default)
Do While oThousand.Status.Playing = True
Application.DoEvents()
Loop
' now extract the hundred digits
' using SubString
' for example, 299 from 88399299
' and pass them to the HundredDigit function
' to be spoken
Me.HundredDigit(strScore.Substring(5, 3))
End Function
' ***
' Function name: HundredMillionDigit
' Parameters:
' Parameter 1: string that holds 9 digit score to be spoken
' returns: none
' ***
Private Function HundredMillionDigit(ByVal strScore As String)
' based on the first three digits in the string
' for example 288399299
' extract the 288 from 288399299and call HundredDigit
' passing the three digits as parameters
Me.HundredDigit(strScore.Substring(0, 3))
' speak million now
oMillion.Play(0, BufferPlayFlags.Default)
Do While oMillion.Status.Playing = True
Application.DoEvents()
Loop
' based on the three numbers in the hundred thousand position in the string
' for example 399 from 288399299
' extract the 399 from 288399299and call HundredDigit
' passing the three digits as a parameter
Me.HundredDigit(strScore.Substring(3, 3))
' speak thousand now
oThousand.Play(0, BufferPlayFlags.Default)
Do While oThousand.Status.Playing = True
Application.DoEvents()
Loop
' now extract the hundred digits
' using SubString
' for example, 299 from 288399299
' and pass them to the HundredDigit function
' to be spoken
Me.HundredDigit(strScore.Substring(6, 3))
End Function
End Class
Code For Test Application
Now that you have the SpeakScore class ready to be used in your project, lets construct a simple test program using form1 in our project that will use the SpeakScore class to speak scores. Go ahead and open up the code editor for form1. Now in the form1 class, just under the text "#Region Windows Form Designer generated code" add this variable declaration:
Private oScore As SpeakScore
We'll be using oScore, an object, in our code to speak various scores we type. We have declared oScore, but we will initialize it in our form load event. After we initialize it, we have to use a function to let us type scores and hear them spoken to us via the oScore object. I'll first show you the form1 load and then the function called TestSpeakingScore. Here is the code for the form1 load event:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strPathToSounds As String
Me.Show()
' path to the wav number sound files
' I have them in a score folder in my project bin directory
strPathToSounds = System.Windows.Forms.Application.StartupPath & "\score\"
' now initialize our speak score object
' passing it the form and the path to our sounds
' as parameters to initialize it
oScore = New SpeakScore(Me, strPathToSounds)
' now call our test function that speaks scores
' as we type them
Me.TestSpeakingScore()
End Sub
ok, I have commented the code to show you what I was doing. Basically, we pass an instance of our form and the path to the wav files to the constructor of the SpeakScore class. After that, it initializes the oScore object and it is now ready to be used at any place in our code or game. We created a test function called TestScoreSpeaking to let us type in scores and press enter to hear them spoken. In a game though, you would use the SpeakScore class to speak scores when a user, for example, presses a key to hear their current score. You would do this in the main game timer or loop. I.E oScore.SpeakScore(strScore) in your loop. Here is the TestSpeakingScore function, copy and paste it below the form1 load event.
Private Function TestSpeakingScore()
Dim strScore As String
' so we can continue to loop
' and prompt for scores
' set this to a value
strScore = "0"
' loop while the variable is not null/empty
' and ask for scores to be spoken via an input box
Do While strScore <> ""
' ask for the users score to be spoken
strScore = InputBox("score")
' now use our oScore object to speak it
oScore.SpeakScore(strScore)
' now loop and ask for another score until strScore is null
Loop
' strScore was null/empty so we are here now
' dispose of our oScore object
oScore.Dispose()
' end the program
Application.Exit()
End Function
So you see in the TestSpeakingScore function we are just in a loop prompting for scores to be spoken. Once you have the form1 load event in place, and the TestSpeakingScore function in place, press f5 to run the project and type in a score, no commas just all numbers ran together, and pres enter to hear it spoke to you. Once it is spoke to you, it will prompt you for another score to be spoken. To exit the loop and end the application, just press escape when prompted to type in a score.
Conclusions
You can port this score speaking class over to any of your game projects. It is quite easy to use. Just remember though, the score has to be a string and not an integer when you call the SpeakScore function in the SpeakScore class. If your score is currently an integer in your game, you can use .ToString at the end of the integer variable such as oScore.SpeakScore(intScore.ToString).