Easy High Scores

Hi all!
I leave a code that creates a list of scores, no matter in what order you enter the score since they are sorted automatically.

Cerberus X:
Class SCORE
    Field n$,v%
End class

Class HIGHSCORE Extends List<SCORE>

    Method New( data:SCORE[] )
        Super.New( data )
    End

    Method Equals:bool( lhs:SCORE,rhs:SCORE )
        Return lhs.v=rhs.v
    End

    Method Compare( lhs:SCORE,rhs:SCORE )
        Return lhs.v-rhs.v
    End

End class

Class Game Extends App

    Method OnCreate()

        'Your code.

        'Limit the size of the score list.
        limit=5

        'Create a fictitious score list.
        AddScore(122,"Eli")
        AddScore(533,"Rubi")
        AddScore(206,"Mike")
        AddScore(603,"Simon")
        AddScore(263,"Ash")

    end
   
    Method OnUpdate()

        If ...IN_GAME...
            If ...THE_PLAYER_IS_ELIMINATED...
                CheckRanking()
                'Your code.
                ...GO_TO_GAME_OVER
            endif
        endif
   
        If ...GAME_OVER...
            If rankin

                Repeat
           
                    Local char=GetChar()
                    If Not char Then Exit
               
                    If char>=32 Then txt+=String.FromChar(char)

                    'If the entered character limit is greater than...
                    If txt.Length()>6
               
                        'Add a new punctuation to the list.
                        AddScore(score,txt)
                        ranking=false

                    Endif
               
                Forever

            Else

                'If there is no ranking, return to the main menu after pressing the "X" key.
                If KeyHit(KEY_X)
           
                    'Cleans the variables of the score.
                    txt=""
                    score=0
               
                    'Your code.
               
                Endif

            Endif

        Endif
   
    End

    Method OnRender()
   
        Scale(8,8)
        Cls(0,0,0)

        'If your score is the highest...
        If rankin
   
            DrawText("PUT YOUR NAME",32,16)
            DrawText(txt,32,24)
       
        Else

            DrawText("GAME OVER",32,8)
           
            'Displays all scores stored in the list.
            Local i%
            For Local hsc:=Eachin hscA

                'If the score and the name of the player on the list is equal to yours...
                If score=hsc.v And txt=hsc.n
                    SetColor(0,255,0)
                Else
                    SetColor(255,255,255)
                Endif

                DrawText(hsc.n+":"+hsc.v,32,16+(i*8))
                i+=1

            Next

        Endif

    End

    'Add score to the list.
    Method AddScore:void(v%,n$)

        If hscA.Count()=limit Then hscA.RemoveLast()
       
        Local scr:=New SCORE
        scr.v=v'Value
        scr.n=n'Name
        hscA.AddLast(scr)
       
        'Sort
        hscA.Sort(false)

    End

    'Check the list for any higher score than yours.
    Method CheckRanking:Void()

        For Local hsc:=Eachin hscA
            'If the current score is greater than any on the list...
            If score>hsc.v
                rankin=true'Activate the ranking.
                exit
            endif
        Next

    End

Private

    Field hscA:=New HIGHSCORE
    Field rankin?
    Field txt$,score%
    Field limit%

End
The code I use in some of my games, so it works even if it looks like a disaster :D
Bye!
 
Last edited by a moderator:

Comments

Top Bottom