Great example IanM! But, how would I implement your code into mine? And how to make the input stop after the returnkey has been pressed?
Here's the code that I used to read and write the Highscores (it's a code snippet I found on the forum):
Score = 0
DIM HighScore(9)
DIM Name$(9)
global ScrWid : ScrWid = screen width() : global ScrHgt : ScrHgt = screen height()
if file exist("test.txt") = 0
gosub SetupHighScores
else
gosub LoadHighScores
endif
KeyDelay = timer() + 100
repeat
gosub ShowHighScores
gosub GetInput
ink rgb(255,255,255),0 : ` white
center text 100,10,"SCORE: " + str$(score)
text 100,ScrHgt - 120,"Press '1' to increase score by 2000."
text 100,ScrHgt - 100,"Press 'S' to save the HighScores."
text 100,ScrHgt - 80,"Press 'A' to add the HighScore to the table."
text 100,ScrHgt - 60,"Press [Spacebar] to quit."
until spacekey() = 1
end
GetInput:
` gets a keypress from the user
if scancode() > 0 and timer() > KeyDelay : ` slow it down so it doesn't update too fast
I$ = upper$(INKEY$()) : ` use Upper$() to make sure the input is always uppercase
select i$
case "1"
inc SCORE,2000
endcase
case "S"
cls
ink rgb(0,0,255),0
center text ScrWid * .5,ScrHgt * .5,"Saving file....."
sync
gosub SaveHighScores
wait 1200
cls
center text ScrWid * .5,ScrHgt * .5,"High Score table saved."
sync
wait 1500
endcase
case "A"
gosub OverWriteHighScores
endcase
endselect
KeyDelay = timer() + 100 : ` update the key delay
endif
return
ShowHighScores:
cls : ` clear the screen
ink rgb(255,255,0),0 : ` yellow
for s = 0 to 9 : ` print out the different names and high scores
text 100,(s * 20) + 50,Name$(s)
text 200,(s * 20) + 50,str$(HighScore(s))
next s
return
SaveHighScores:
` check to see if there is an existing file - if so, delete it
if file exist("test.txt") = 1 then delete file "test.txt"
Open to Write 1,"test.txt"
For N = 0 To 9
Write long 1,HighScore(N)
Write String 1,Name$(N)
Next N
Close File 1
return
LoadHighScores:
if file exist("test.txt") = 1
open to read 1,"test.txt"
for n = 0 to 9
read long 1,HighScore(n)
read string 1,Name$(n)
next n
close file 1
endif
return
OverwriteHighScores:
cls
maxx = 10
ink rgb(255,255,255),0
center text ScrWid * .5,ScrHgt * .5,"Updating high score table."
sync
wait 1200
pos = -1
for hs = 9 to 0 step -1
if score > HighScore(hs) then pos = hs
next hs
if pos >= 0 and pos < 9
` move the HighScores that are lower than score down by one
for hs = 8 to pos step -1
Name$(hs + 1) = Name$(hs)
HighScore(hs + 1) = HighScore(hs)
next hs
endif
if pos = 9 then HighScore(9) = score
if pos >= 0
text 10,ScrHgt - 200,"Congratulations! Your score is one of the top ten!"
set cursor 10,ScrHgt - 180
INPUT "Enter your name: ",Name$(pos) : sync
HighScore(pos) = score
endif
return
SetupHighScores:
` gets here if there is no saved file and puts values into the arrays
restore HighScoreData
for s = 0 to 9
read HighScore(s),Name$(s)
next s
return
HighScoreData:
` score, name$
data 9999999,"Bob1",8888888,"Bob2",7777777,"Bob3",6666666,"Bob4",5555555,"Bob5"
data 4444444,"Bob6",3333333,"Bob7",2222222,"Bob8",1111111,"Bob9",0000000,"Bobx"
Thanks
Cheers
Slayer rules!!! Yeaaah, man!