Setting Up and Using High Scores (Written by:Unknown)
 

global

//So I had this struct to save them in
struct highscore[10];
 score;
end

//Set up the highscore table
process initHighscore()

private
string fileName = "highscore.file";
i;

begin

if(file_exists(fileName))

 //File exists and can be loaded:
 load(fileName, highscore);

else

 //File does not exist, fill the highscore table with standard values:
 for(i = 0;i < 10; i++ )
   //Fill in a generated score(lower index means higher score)
   highscore[i].score = (10-i)*100;
 end

end

end

//Add a score
process addHighscore(int newScore)

private
i, newScorePosition;

begin
for( i = 0; i < 10; i++ )
 if(highscore[i].score < newScore)
   break;
 end
end

if(i == 10)
 //If i equals 10 all scores in the table were higher than the new one.
 return;
else
 //If lower, the score on place 'i' is the first one to be lower than the new score.

 //Save which position the new score is coming
 newScorePosition = i;

 //And move everything one place down, starting with 'i'
 for(;i<9;i++)
   highscore[i+1].score = highscore[i].score;
 end

 //Add in the new score
 highscore[i].score = newScore;
end

//And if you would want to, make it save when changed
save(fileName,highscore);

end

//print to the screen
process printHighscore()

private
i;

begin

for( i = 0; i < 10; i++ )
 write(0,screenx-150,200+i*20,0,i);
 write_int(0, screenx+150,200+i*20,2,&highscore[i].score);
end

end


  Back to Snippets

Home : Start Here : Sources : Snippets : Tutorials : Faq's : Links : Contact