There is not much results that shows working highscore table, and definetly not in code snippets.I`m in sharing mood right now, and i have some problems with dbpro recently, eating my code, so it`s safer to post something valuable here instead loosing it.
This is my way to do it.There can be other ways involving less coding.And the code can be made pluginless.I`m just a bit lazzy now to rewrite it with pure dbpro commands.
Font plugin i use in this snippet:
http://forum.thegamecreators.com/?m=forum_view&t=92836&b=5
This is the thread and the plugin is on "B" the first one.
The other plugin i used is famous Matrix1, if you do not have it, get it.Just search arround.
To display the table just hold left mouse button.
With space you refresh values in the table with random value and you can see the changes.
sync on : sync rate 0
backdrop on
`Font Used to Draw the Highscore Table
font=1000
load image "font.bmp",999
make bmp font 999,font
`Highscore type containing the player name and the points which he have.
type highscore
name$ as string
points as integer
endtype
`Constant max_board as pointer to how many places the highscore board have.We put in mind that we`ll use 0 in arrays too.
`0 to 9 are 10 positions
#CONSTANT max_board = 9
`Highscore arrays to hold highsocres and to draw them in real time.
dim highscores(max_board) as highscore
dim bmp_listing$(max_board) as string
`Variable to reverse datafile writing of highscore.dat for the first time so the higher value is the first.
pos_offset = max_board
`Writing of highscore.dat if it does not exists.
FileID = find free datafile()
if file exist ("highscore.dat") = 0
open datafile to write FileID,"highscore.dat"
for write = 0 to max_board
highscores(pos_offset-write).name$ = "anarhist"
write datafile string FileID,highscores(pos_offset-write).name$
highscores(pos_offset-write).points = (pos_offset-write)*10
write datafile integer FileID,highscores(pos_offset-write).points
next write
close datafile FileID
endif
`If highscore.dat exists, we read it.
if file exist ("highscore.dat") = 1
open datafile to read FileId,"highscore.dat"
for reading = 0 to max_board
highscores(reading).name$ = datafile string$ (FileID)
highscores(reading).points = datafile integer (FileID)
next reading
close datafile FileID
endif
do
text 0,0,"fps: "+str$(screen fps())
text 0,20,"name: "+ name$
text 0,40,"points: "+str$(points)
text 0,60,"temp: "+str$(temp)
if mouseclick() = 1 and mouse_toggle = 0
mouse_toggle = 1
endif
if mouseclick() = 0 and mouse_toggle = 1
mouse_toggle = 0
endif
`When conditions are met the highscore board is drawn to screen.
if mouse_toggle = 1
for listing = 0 to max_board
if listing <> max_board
bmp_listing$(listing) = str$(listing+1)+". "+highscores(listing).name$ +" "+str$(highscores(listing).points)
else
bmp_listing$(listing) = str$(listing+1)+". "+highscores(listing).name$ +" "+str$(highscores(listing).points)
endif
btext font,bmp_listing$(listing),200,50+(15*listing),10,1
next listing
endif
`When conditions are met the player`s result is checked against the highscores, then if it`s better than
`other result his name and points are writen at position where matched.
if keystate(57)=1 and space_toggle = 0
space_toggle = 1
space_pressed = 1
`Next 2 lines are not needed in real game, just for speed tesing.
name$ = "luskos"
points = rnd(1000)
if points > highscores(max_board).points
highscores(max_board).name$ = name$
highscores(max_board).points = points
sort array highscores(),-2
open datafile to write FileID,"highscore.dat"
for write = 0 to max_board
write datafile string FileID,highscores(write).name$
write datafile integer FileID,highscores(write).points
next write
close datafile FileID
endif
endif
if keystate(57)=1 and space_pressed = 0
space_toggle = 0
endif
if keystate(57) = 0
space_pressed = 0
endif
sync
loop
EDIT: The code need some polish work because new scores go to nearest position that match for them instead pushing down everything with one position.I`ll do it when there is some free time.But anyone willing to modify it and post it here is apprecieted.
EDIT2: I managed to deal with the sort problem, the code is complete and fully functional.It`s plug and play.Maybe need minor adjustments to fit your needs.Enjoy!And coment please!
Big thanks to IanM!Helpful as allways!
Cheers!
Where there is a will, there is a way.
I often edit my posts, that`s who i am
