Main Menu

HighScore Manager/Library

Started by kevin, March 07, 2008, 07:13:40 AM

Previous topic - Next topic

kevin

  High Score Management Library


  The attached example provides you with a frame work for managing HighScore tables.  The frame work gives you functions for sorting / inserting and checking if a score is a high score or not.    It doesn't do the rendering, since you'll no doubt want to display the information yourself.


PlayBASIC Code: [Select]
;*=-----------------------------------------------------------------------------=*   
;
; >> HighScore Management Library <<
;
; 7th, Mar, 2008
;
; By Kevin Picone
;
; (c) copyright 2008 by Kevin Picone, All Rights reserved
;
;*=-----------------------------------------------------------------------------=*
;
;
;*=-----------------------------------------------------------------------------=*


; Numb of HighScores For highScore table
MaxHighScores=20


; Create a BLANK Hiscore Table
HighScoresTableSize(MaxHighScores)


; Create a list of names in the names$() array
Dim Names$(10)
NameLIst$="Bill,Jenny,Kevin,Olivia,Fred,Dude,Giant Alias"
; Use Split to array to split up the nameslist and place the names into the names$() array for us
NameCount=SplittoArray(NameLIst$,",",Names$(),0)


; Init the HighScore table with some Random Names and Random scores
For lp=1 to MaxHighScores

HighScores(lp).Score =Rnd(100000) ; players score
HighScores(lp).Name$ =Names$(rnd(NameCount-1)) ; name of this player
HighScores(lp).Level =rnd(100) ; The level the player achieved in string format

next



;*=-----------------------------------------------------------------------------=*
; >> Display the High Score Table <<
;*=-----------------------------------------------------------------------------=*

Setfps 60

SortDirection=1
SortHighScores(SortDirection)

Do
;clear the screen
Cls rgb(30,40,50)


; Display the HighScore Array
Display_HighScoreList(GetSCreenWidth()/2,50,MaxHighScores)


; Check if the F1 key was pressed
if FunctionKeys(1)=true
SortDirection=1-SortDirection
SortHighScores(SortDirection)
FlushKeys
endif


; Check if the space was pressed. If it was, then we add a new high score to the table
if Spacekey()=true

; Reset the

PlayerScore =Rnd(100000) ; players score
PLayerName$ =Names$(rnd(NameCount-1)) ; name of this player
PlayerLevel =rnd(100) ; The level the player achieved in string format


AddedHighScoreMessage$=PLayername$+" Didn't Get A High Score"

; Check if this score is a highscore?

HighScoreTablePosition=CheckIfScoreIsHighScore(PlayerScore,SortDirection)
if HighScoreTablePosition

; Insert empty space into the highscore table/array
InsertHighScore(HighScoreTablePosition,SortDirection)


HighScores(HighScoreTablePosition).Score =PlayerScore
HighScores(HighScoreTablePosition).name$ =PlayerName$
HighScores(HighScoreTablePosition).Level =PlayerLevel

AddedHighScoreMessage$=PLayername$+" Got A High Score - Position ["+str$(HighScoreTablePosition)+"]"

Endif

flushKeys
endif


print HighScoreTablePosition
Print PlayerScore
Print AddedHighScoreMessage$

Sync
loop






;*=-----------------------------------------------------------------------------=*
; >> Display the High Score Table <<
;*=-----------------------------------------------------------------------------=*

Function Display_HighScoreList(Xpos,Ypos,MaxHighScores)

DotString$=Make$(".",20)

For lp=1 to MaxHighScores

Score=HighScores(lp).Score
Level=HighScores(lp).Level

s$="#"+Digits$(lp,2)+" "

s$=s$+Digits$(Score,6)+" Level "+Digits$(Level,3)+" "
Name$=Right$(DotString$+HighScores(lp).Name$,Len(DotString$))

CenterText Xpos,Ypos,s$+name$

Ypos=Ypos+20
next
EndFunction




Download

   complete PlayBASIC project attached