• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Snippet Easy High Scores

TheMrCerebro

Member
Tutorial Author
Joined
Apr 7, 2018
Messages
18
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:
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!
 

Attachments

  • 1548881654092.png
    1548881654092.png
    27.5 KB · Views: 340
Last edited by a moderator:
Back
Top Bottom