Tutorial by Nicholas Gorski
This tutorial will show you the basics of creating a high score manager. Feel free to download the VB6 source code for this tutorial .
Creating the Class
Create a new Standard EXE. Insert a new class called Highscore_Manager. The first and most obvious function that we would need is a function that allows us to add scores to the high score table. But how do we save them? There is a function in Visual Basic called 'SaveSetting', which saves a setting in the registry. The function takes 4 parameters, the Application Name, Section, Key, and Value. Here is a visual reference:
-Application Name
-Section
-Key1 = 5
-Key2 = "Hello!"
So our function will only need to take 3 parameters, Application Name, Player Name, and Score:
Public Function SaveScore(ByVal ApplicationName As String, _ ByVal PlayerName As String, ByVal Score As Long) Call SaveSetting(ApplicationName, "Highscore", _ PlayerName, Str(Score)) End Function
I see many problems with this. One, however, is the fact that it doesn't test to see if the score is even high enough to make it onto the list. But in order to check if a score is valid, we need a way to load the high score list. This function will take five parameters, one is the number of scores in the list. The second is the default score if no entry is found, and the third is the default name. The fifth parameter is the Application Name, and the last parameter is a Boolean, which is used for sorting. If it is True, the scores will be sorted as the higher the score, the better (like a shooting game). False will sort them as the lower the score the better (such as golf). In order to save the scores in memory, we need two dynamic arrays, Scores and Names, along with their counting variable. At the top of the class, add:
Private Scores() As Long Private Names() As String Private NumberEntries As Long
Now make the load function:
Public Function LoadScores(ByVal NumberScores As Long, _ ByVal DefaultScore As Long, _ ByVal DefaultName As String, ByVal ApplicationName As String, _ Optional ByVal SortDirection As Boolean = True) Dim i As Long, j As Long, t As Long, ts As String 'Make room for scores NumberEntries = NumberScores ReDim Scores(NumberScores) As Long ReDim Names(NumberScores) As String 'Load them For i = 0 To NumberScores Scores(i) = GetSetting(ApplicationName, "Highscore", _ i & "S", DefaultScore) Names(i) = GetSetting(ApplicationName, "Highscore", _ i & "N", DefaultName) Next i 'Sort them For i = 0 To NumberScores For j = 0 To NumberScores If SortDirection Then 'Higher the better If Scores(i) > Scores(j) Then t = Scores(i) ts = Names(i) Scores(i) = Scores(j) Names(i) = Names(j) Scores(j) = t Names(j) = ts End If Else If Scores(i) < Scores(j) Then t = Scores(i) ts = Names(i) Scores(i) = Scores(j) Names(i) = Names(j) Scores(j) = t Names(j) = ts End If End If Next j Next i End Function
However, you will notice that this function loads two settings from the registry. But we only saved one. We need to change the way we save. The procedure now needs to see whether the score is high enough, and if so, insert it into the proper place:
Public Function SaveScore(ByVal ApplicationName As String, _ ByVal PlayerName As String, ByVal Score As Long, _ Optional ByVal SortDirection As Boolean = True) As Boolean Dim i As Long, j As Long Dim lT1 As Long, lT2 As Long, sT1 As String, sT2 As String Dim lFound As Long lFound = -1 For i = 0 To NumberEntries If SortDirection Then 'Higher the better If Score > Scores(i) Then SaveScore = True lFound = i Exit For End If Else If Score < Scores(i) Then SaveScore = True lFound = i Exit For End If End If Next i If lFound = -1 Then Exit Function 'Shift around scores and names lT2 = Score sT2 = PlayerName For i = lFound To NumberEntries lT1 = Scores(i) sT1 = Names(i) Scores(i) = lT2 Names(i) = sT2 lT2 = lT1 sT2 = sT1 Next i 'Save all scores For i = 0 To NumberEntries Call SaveSetting(ApplicationName, "Highscore", _ i & "S", Scores(i)) Call SaveSetting(ApplicationName, "Highscore", _ i & "N", Names(i)) Next i End Function
Much bigger! We need two more functions to allow access to the arrays:
Public Function GetPlayerScore(ByVal Index As Long) _ As Long On Error Resume Next GetPlayerScore = Scores(Index) End Function Public Function GetPlayerName(ByVal Index As Long) _ As String On Error Resume Next GetPlayerName = Names(Index) End Function
And one more to check whether a score is high enough to be on the list:
Public Function CheckScore(ByVal Score As Long, _ Optional ByVal SortDirection As Boolean = True) As Boolean Dim i As Long, j As Long For i = 0 To NumberEntries If SortDirection Then 'Higher the better If Score > Scores(i) Then CheckScore = True Exit Function End If Else If Score < Scores(i) Then CheckScore = True Exit Function End If End If Next i End Function
Now you have a basic high score list.
Done
You can download the source code here.
I suggest that you look up some encrytion methods. That will prevent people from creating false scores. Hopefully this tutorial helps as you develop your game if you have any questions or comments feel free to post them below.
Where can I download visual
Where can I download visual basic 6.0?
google it... download the
google it... download the portable edition.
ummm , longer then it needs to be
thats um... longer then it needs to be , in my game on gameover i made it check to see if it was higher then certain ones and made certain things become visible , heres the code for that part
If intLives = 0 And intClicks >= HSarray(1) Then
cmdOK1.Visible = True
txtName.Visible = True
ElseIf intLives = 0 And intClicks < HSarray(1) And intClicks >= HSarray(2) Then
cmdOK2.Visible = True
txtName.Visible = True
ElseIf intLives = 0 And intClicks < HSarray(2) And intClicks >= HSarray(3) Then
cmdOK3.Visible = True
txtName.Visible = True
ElseIf intLives = 0 And intClicks < HSarray(3) And intClicks >= HSarray(4) Then
cmdOK4.Visible = True
txtName.Visible = True
ElseIf intLives = 0 And intClicks < HSarray(4) And intClicks >= HSarray(5) Then
cmdOK5.Visible = True
txtName.Visible = True
ElseIf intLives = 0 And intClicks < HSarray(5) Then
cmdRestart.Visible = True
End If
AND this also can be shortend with a select class statement but i just dont wanna cuz this works fine , and then i have some others for changing the scores after i click the corresponding button , its not perfect but its still pretty good for somone who started from scratch and is teaching himself , heres the next part
cmdOK1.Visible = False
cmdOK2.Visible = False
cmdOK3.Visible = False
cmdOK4.Visible = False
cmdOK5.Visible = False
txtName.Visible = False
Label2.Visible = False
HSarray(1) = intClicks
HSNarray(1) = txtName.Text
lblHSN5.Caption = lblHSN4.Caption
lblHS5.Caption = lblHS4.Caption
lblHSN4.Caption = lblHSN3.Caption
lblHS4.Caption = lblHS3.Caption
lblHSN3.Caption = lblHSN2.Caption
lblHS3.Caption = lblHS2.Caption
lblHSN2.Caption = lblHSN1.Caption
lblHS2.Caption = lblHS1.Caption
lblHSN1.Caption = HSNarray(1)
lblHS1.Caption = HSarray(1)
lblHS.Visible = True
lbl2nd.Visible = True
lbl3rd.Visible = True
lbl4th.Visible = True
lbl5th.Visible = True
lblHS1.Visible = True
lblHS2.Visible = True
lblHS3.Visible = True
lblHS4.Visible = True
lblHS5.Visible = True
lblHSN1.Visible = True
lblHSN2.Visible = True
lblHSN3.Visible = True
lblHSN4.Visible = True
lblHSN5.Visible = True
cmdRestart.Visible = True
and then for the other buttons i just take out certain lines about 2 lines i think and change a few numbers , in all it takes about a few minutes of time and not alot of code or knowledge of the program to make this , anyways this was also my first game , thank you for your time.
it is much easier than the one that posted on this tut,
thank you so much.. it is not much complicated than the tutorial :)
That's some extremely basic
That's some extremely basic code that you've shown us there, and is kind messy.
I don't see how you can say his code is "longer than it needs to be" when yours is pretty big, aswell.
Not to mention I don't see anything about saving/loading hiscores in your code, whereas his code can load/save them.
Post new comment