Easy to use High Scores Table for Games W/ Display

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Easy to use High Scores Table for Games W/ Display

Post by rolliebollocks »

Code: Select all

type HighScore
    as string playername
    as string score
    as string dateof
    
    declare sub Set ( pn as string, inscore as string, indateof as string )
end type

sub HighScore.Set ( pn as string, inscore as string, indateof as string )
    
    this.playername = pn
    this.score = inscore
    this.dateof = indateof
    
end sub

#DEFINE MAX_SCORES 20

type HighScoreTable
    dim as HighScore     Record(MAX_SCORES)
    dim as string        filename
    
    declare sub Load ( fn as string = "highscores.txt")
    declare sub Insert ( in_score as HighScore )
    declare sub Save ()
    declare sub PrintHST ()
    declare sub Display()
       
end type

sub HighScoreTable.Insert ( in_score as HighScore )
    
    for i as integer = 1 to MAX_SCORES
        if val(in_score.score) > val(Record(i).score) then
            for ii as integer = MAX_SCORES to i step -1
                Record(ii).score = Record(ii-1).score
                Record(ii).playername = Record(ii-1).playername
                Record(ii).dateof = Record(ii-1).dateof                
            next
            Record(i).score = in_score.score
            Record(i).playername = in_score.playername
            Record(i).dateof = in_score.dateof
            exit for
        endif
    
    next
    
end sub

sub HighScoreTable.Save ()
    
    if filename = "" then filename = "highscores.txt"
        
    
    open filename for output as #1
        
    for i as integer = 1 to MAX_SCORES
        print #1, Record(i).playername
        print #1, Record(i).Score
        print #1, Record(i).Dateof
    next
    
    close #1

end sub

sub HighScoreTable.Load ( fn as string = "highscores.txt" )
    
    open fn for input as #1
    
    for i as integer = 1 to MAX_SCORES
        line input #1, Record(i).playername
        line input #1, Record(i).Score
        line input #1, Record(i).dateof
    next
    
    this.filename = fn
    close #1
    
end sub

sub HighScoreTable.PrintHST()
    
    for i as integer = 1 to MAX_SCORES
        ? i,Record(i).playername, Record(i).Score,Record(i).DateOf
    next

end sub

sub HighScoreTable.Display()
    
for i as integer = 75 to 550 step 25
    line ( 800-700,i) - (800-100, i-25),&hfff*RND*1000000,b
next

Draw String (150, 31), "Player", &hff
Draw String (350, 31), "Score", &hff
Draw String (490, 31), "Date", &hff

for i as integer = 1 to MAX_SCORES
    Draw String ( 150, 31 + (i*25) ), Record(i).Playername
    Draw String ( 350, 31 + (i*25) ), Record(i).Score
    Draw String ( 490, 31 + (i*25) ), Record(i).Dateof
next

    
end sub
'///////////////////////////////////////////////////////////////////////////////
dim as HighScoreTable hst
dim as HighScore hs(20)

for i as integer = 1 to 20
    'Generates Random Values
    hs(i).score = str(int(rnd*300))
    hs(i).playername = str(i) 
    hs(i).dateof = date
    
    hst.Insert ( hs(i) )
next
    
screen 19,32

'hst.Load("temp.txt") 'Also sets the filename to save

'st.Save()
hst.Load ("highscores.txt")
hst.Display
sleep
Frank Dodd
Posts: 444
Joined: Mar 10, 2006 19:22

Post by Frank Dodd »

Very cool Rollie, quick and simple way to implement a high score table in a game. Thanks very much for the code.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

Thanks!
Post Reply