[RESOLVED] Need help with a high score system by reading from txt file and player score. Help!
I am reading things from a txt file. It is attached bellow. I know how to compare the scores to the scores in the text file, but, i have no idea how to make everything move down one... i have 10 labels. Lets say label5 is going to be replaced with the players score, label5's value will become label6's, label6's to label7, and so on. Its like it is bumping everything else down. label10's will be bumped off(unless that is the score being replaced). I have the code the replaces the score, but dont know how to bump everything down before that. Here is my code:
Code:
Dim i
Dim overwrite As Boolean
overwrite = True
For i = 0 To hsn
If score < highscores(i) Then
lblName(i).Caption = scorename(i)
lblScore(i).Caption = highscores(i)
lblName(i).FontSize = 8
lblScore(i).FontSize = 8
ElseIf score > highscores(i) And overwrite = True Then
'code here to bump everything down
scorename(i) = username
highscores(i) = score
lblName(i).Caption = username
lblScore(i).Caption = score
lblName(i).FontSize = 8
lblScore(i).FontSize = 8
overwrite = False
ElseIf overwrite = False And i < 10 Then
lblName(i).Caption = scorename(i)
lblScore(i).Caption = highscores(1)
lblName(i).FontSize = 8
lblScore(i).FontSize = 8
End If
Next i
Please help!
Last edited by Gamemaster1494; May 23rd, 2010 at 08:38 PM.
Re: Need help with a high score system by reading from txt file and player score. Hel
Here's how I'd do it.
1. Load text file contents to a String, Split it into an array by new line. (after that names are in even and points in +1 odd index numbers)
2. Sort the array based on score
3. Do a For loop to load the first 1o scores into labels.
EDIT: Just noticed the link to a Nebula Forums in your signature. I think advertising other forums with similar content is against the AUP. You might want to remove it.
Re: Need help with a high score system by reading from txt file and player score. Hel
I can load them fine. Its just when i want to replace a high score, and bump the highscore i will replace and the ones down farther down, it doesnt. All it does replaces the score... Do you know what i mean?
Re: Need help with a high score system by reading from txt file and player score. Hel
I think so, but you don't need to fiddle with that. When a new score is achieved, simply save it to the list/file. When you sort the array the will be sorted in order you want them.
To prevent the file from growing to large (if you just keep adding on scores), after you sort and show the scores, For loop through the (sorted) array and save first X scores you want to save.
Re: Need help with a high score system by reading from txt file and player score. Hel
Hmm... okay. i know what you mean.... But how would i be able to do that? I already have the 10 scores(thats the limit) in the .txt file... how would i write the new score, and then bump them down, and delete the last line(if it would be 11)?
Re: Need help with a high score system by reading from txt file and player score. Hel
ReDim the Array (to which you loaded the saved scores) then append the new score to the end. Then re-sort, display, and save first 10 to the file again.
Re: Need help with a high score system by reading from txt file and player score. Hel
I don't think you really need to sort them as such. It is simple moving around of data.
Anyway, I think what you have hard time with is usage of data structures. Using stuff such as user defined types (UDTs) makes your life so much easier. This requires a separation of stuff you display and the data itself. Attached is a sample project to show you exactly this. Code follows here as well:
Code:
Option Explicit
' length of Name set to specific number of characters to make saving easier
Private Type HIGHSCORE
Name As String * 24
Score As Currency
When As Date
End Type
' it is super easy to save this to a file: open for binary, Put #1, , Highscores – loading is the same, Get #1, , Highscores
Private Highscores(0 To 9) As HIGHSCORE
' this function returns True if the scores was added to the highscores list
Private Function AddHighscore(Name As String, ByVal Score As Currency, Optional ByVal When As Date) As Boolean
Dim I As Long
' see if the score fits within highscores
For I = 0 To UBound(Highscores)
If Highscores(I).Score < Score Then Exit For
Next I
' did it fit in the scoreboard?
If I <= UBound(Highscores) Then
' start from end, copying better score down the list
For I = UBound(Highscores) To I + 1 Step -1
Highscores(I) = Highscores(I - 1)
Next I
' add the new highscore to it's rightful place
Highscores(I).Name = Name
Highscores(I).Score = Score
If When = 0 Then When = Now
Highscores(I).When = When
' tell we succeeded
AddHighscore = True
End If
End Function
Private Sub UpdateHighscores()
Dim I As Long
' this code makes a simple listbox update
List1.Clear
For I = 0 To UBound(Highscores)
' Right limits to certain length of characters
List1.AddItem Right$(Space$(14) & Highscores(I).Score, 15) & _
" " & Right$(Space$(23) & Highscores(I).Name, 24) & _
Format$(Highscores(I).When, " YYYY\-MM\-DD HH\:NN\:SS")
Next I
List1.ListIndex = 0
End Sub
Private Sub cmdHighscoreAdd_Click()
' if changes made then update
If AddHighscore(txtHighscoreName.Text, txtHighscore.Text) Then UpdateHighscores
End Sub
Private Sub Form_Load()
List1.FontName = "Lucida Console"
' sample data: date is an optional parameter here
AddHighscore "Jacob", 1000000000, #1/1/2010 1:01:01 AM#
AddHighscore "Eric", 100000000, #1/1/2009 1:01:01 AM#
AddHighscore "Jon", 10000000, #1/1/2008 1:01:01 AM#
AddHighscore "Joaquim", 1000000, #1/1/2007 1:01:01 AM#
AddHighscore "Shaun", 100000, #1/1/2006 1:01:01 AM#
AddHighscore "Mike", 10000, #1/1/2005 1:01:01 AM#
AddHighscore "Nebula", 1000, #1/1/2004 1:01:01 AM#
AddHighscore "Inc.", 100, #1/1/2003 1:01:01 AM#
AddHighscore "Nexxoz", 10, #1/1/2002 1:01:01 AM#
AddHighscore "Inc.", 1, #1/1/2001 1:01:01 AM#
' then display the changes on screen
UpdateHighscores
End Sub
So the separation here:
You have a data structure Highscores, which is an array of 10 items of the UDT called HIGHSCORE.
A separate function is made to update this array. If changes are made (score is good enough for the list), the function returns True and makes the changes to the data structure.
A separate procedure UpdateHighscores handles updating the stuff on a form. In this case just a listbox, but it could very well be your labels.
Added bonus: saving and loading the highscores is a piece of cake, you can survive with three lines of code! Save: Open, Put, Close – Load: Open, Get, Close.
Added bonus: dates of highscores!
Added bonus: it is easy to separate the UDT, array & AddHighscore function to a public module. Makes code a bit cleaner as not all code needs to be on a form.
Re: Need help with a high score system by reading from txt file and player score. Hel
Type = user defined type.
Private = only available to module/object it is declared in.
Public = available to all modules/objects.
You'll probably need to take it slow as there is a lot of information even if the code isn't all that long. You probably want to look for a tutorial on user defined types, or about basics of VB6 in general.
Re: Need help with a high score system by reading from txt file and player score. Hel
If you don't explicitly set the Step, it is defaulted to 1.
So,
- For I = 1 To 3 will go: 1, 2, 3
- For I = 3 To 1 wont loop at all
- For I = 3 To 1 Step -1 will do: 3, 2, 1
But you can also do something like this:
- For I = 0 To 10 Step 2 and it will do: 0, 2, 4, 6, 8, 10
- For I = 0 To 9 Step 3 will do: 0, 3, 6, 9
- For I = 0 To 10 Step 3 will also do: 0, 3, 6, 9