Using Graphics: Making a Lander Game - Part 3
Where a certain score is placed in the table depends on the score itself and whether bigger is better, or smaller is better. At first we will look at a system using bigger is better, although we will not be using it for the Lander game, it is easier to understand.
The first thing is to check whether the score is good enough to get on the score board:
Public Function InsertScores(intScore As Integer) _ As Integer Dim insertscore As Integer, updatescore As Integer Dim strName As String If intScore < Scores(numberplaces).intScore Then InsertScores = 0 Exit Function End If
If it is not good enough, the function exits, returning 0 for the position where the score was inserted. Otherwise it just keeps going. The only difference between the 'bigger is better' system and the 'smaller is better' system is the operator (< or >) used to determine whether the score is elegible. The best way to evaluate this in just one line of code is to use the IIf function. If you have not come across it before, look it up in the help file, as it can be really useful. This is how we will use it:
If IIf(ScoreOrder = SmallerIsBetter, intScore > _ Scores(numberplaces).intScore, intScore < _ Scores(numberplaces).intScore) Then 'etc
Now that we have decided that the score can go into the table, we must first find where to put it. Once we have found it, we must go through, moving all the others down, then insert it into the new position. In the middle of all this, we must also get the user's name:
For insertscore = 1 To numberplaces If IIf(ScoreOrder = SmallerIsBetter, intScore < _ Scores(insertscore).intScore, intScore > _ Scores(insertscore).intScore) Then ' we've found the score's position, so move ' all the other scores down: For updatescore = numberplaces - 1 To insertscore + 1 Step -1 Scores(updatescore) = Scores(updatescore - 1) Next ' then get the user's name strName = InputBox("Please enter your name") ' set the new name Scores(insertscore).strName = IIf(strName = "", "Anonymous", _ strName) ' and the new score Scores(insertscore).intScore = intScore ' and return the new position InsertScores = insertscore Exit For End If Next End Function
And that's it! The score can now be inserted by using some code like this:
Now that we can get scores into the table, lets figure out how to get scores out of the table.
Page 5 of 6