(AppGameKit Official Tutorial Guide, page 462) :
First, this is your main program... 'HighScores.agc'
//
// Project: HighScores
// Created: 2015-04-16
#include "StringLibrary.agc"
//*** Define types ***
type PlayerType
name as string
score as integer
endtype
//***************************************
//*** Main program ***
//***************************************
topscores as PlayerType[] //Holds top scores
latestplayer as PlayerType //Details of latest game
//*** Set window properties ***
SetWindowTitle("Maintaining a Set of High Scores")
SetWindowSize(1024,768,0)
SetDisplayAspect(1024/768.0)
UseNewDefaultFonts(1)
//*** Clear screen ***
ClearScreen()
//*** Generate dummy score details ***
latestplayer.name = RandomString()
latestplayer.score = Random2(100, 20000)
//***Update highest scores list ***
topscores = UpdateHighScoresFile(latestPlayer)
do
for c = 1 to 5
Print("Name : "+topscores[c].name+" Score : "
?+Str(topscores[c].score))
next c
Sync()
loop
And this is 'String Library.agc'
//_______________________________"StringLibrary.agc" ________________________(put this as separate file , and name it "StringLibrary.agc").........
// This is an 'include file'____________________It contains 3 functions that you will call from your 'main' file.
//***************************************
//*** Functions ***
//***************************************
// FUNCTION
//*** Updates high score list in file and returns updated list ***
function UpdateHighScoresFile(player as PlayerType)
gamesplayed as PlayerType[5] //Contains high score details
//*** If high scores file exists ***
if GetFileExists("HighScores.dat") = 1
//*** Open file for reading ***
fileID = OpenToRead("HighScores.dat")
//*** Read the file’s details ***
for c = 1 to 5
gamesplayed[c].name = ReadString(fileID)
gamesplayed[c].score = ReadInteger(fileID)
next c
//*** Close the file ***
CloseFile(fileID)
endif
//----------------------------------------------------------------------
//*** If current game is added to high score list... ***
if UpdateTopScoresList(gamesplayed,player) = 1
//*** Open high score file for writing ***
fileID = OpenToWrite("HighScores.dat")
//*** Write updated list to file ***
for c = 1 to 5
WriteString(fileID, gamesplayed[c].name)
WriteInteger(fileID, gamesplayed[c].score)
next c
CloseFile(fileID)
endif
endfunction gamesplayed
// FUNCTION
//*** Adds newscore to list, if newscore contains a high score ***
//*** Returns 1 if added, 0 if not added ***
function UpdateTopScoresList(list ref as PlayerType[], newscore as PlayerType)
//*** Find insert point ***
post = 1
while post < list.length and list[post].score > newscore.score
inc post
endwhile
//*** If not a high score, return zero ***
if list[post].score > newscore.score
exitfunction 0
endif
//*** Free up cell ***
for c = list.length-1 to post step -1
list[c+1] = list[c]
next c
//*** Insert value ***
list[post] = newscore
//*** Value added to list, return 1 ***
endfunction 1
// FUNCTION
//***************************************
//*** Functions ***
//***************************************
//*** Generates a random-length string of random letters ***
function RandomString()
//*** Generate length for string ***
size = Random(1,50)
//*** start with empty string ***
result as string = ""
//*** FOR size times ***
for c = 1 to size
//*** Add new character to end of string ***
result = result + Chr(Random(65,90))
next c
//*** return the string generated ***
endfunction result
See the attachment below....(Capture 1.png)
This was taken after the program was run five times...
Notice that each time the program is run, it adds another name and score, and saves it for future use.
You can close the program completely, and the next day come back to it and the scores are still there.
This program can be incorporated into any game where you want to keep score.
Noticed that the Scores are arranged from highest on top to lowest on the bottom.
.