Results 1 to 15 of 15

Thread: [RESOLVED] Need help with a high score system by reading from txt file and player score. Help!

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Resolved [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!
    Attached Files Attached Files
    Last edited by Gamemaster1494; May 23rd, 2010 at 08:38 PM.
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    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?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    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)?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Need help with a high score system by reading from txt file and player score. Hel

    Okay. How would i re-sort them?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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:
    1. You have a data structure Highscores, which is an array of 10 items of the UDT called HIGHSCORE.
    2. 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.
    3. 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.
    4. 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.
    5. Added bonus: dates of highscores!
    6. 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.
    Attached Files Attached Files

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Need help with a high score system by reading from txt file and player score. Hel

    O_o o_O I am totaly confused...... What do the Private Types do? And does it read it from a .txt file?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Need help with a high score system by reading from txt file and player score. Hel

    I know what Private and Public ment. Didnt knew what type ment. Thanks. ill have to look threw it in detail...
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Need help with a high score system by reading from txt file and player score. Hel

    So, it lloks like all i need to add to my code is the Step -1. I had no idea you could do that. So, like if i do

    For i = 0 to 10 Step -1

    Does it go 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0? or 0, -1, -2, -3, -4 etc.?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  13. #13
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Need help with a high score system by reading from txt file and player score. Hel

    Quote Originally Posted by Gamemaster1494 View Post
    So, it lloks like all i need to add to my code is the Step -1. I had no idea you could do that. So, like if i do

    For i = 0 to 10 Step -1

    Does it go 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0? or 0, -1, -2, -3, -4 etc.?
    No, try this
    Code:
    Private Sub Command1_Click()
       Dim i
        For i = 10 To 0 Step -1
            List1.AddItem i
        Next i
    End Sub

  14. #14
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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

    etc...

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Need help with a high score system by reading from txt file and player score. Hel

    Nevermind! I got it! Thanks so much everyone!!!! =D =D =D
    Last edited by Gamemaster1494; May 25th, 2010 at 01:46 PM. Reason: fixed it!
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width