HOME >> Tutorials >> Big Game >> How To Setup A High Score Function

How To Setup A High Score Function
BASIC programmers for the TI-83 and TI-83 Plus have had a problem when it comes to saving high scores and records of their BASIC games. You cannot just leave the number in a user variable because it could get overwritten in another program. To combat this problem, programmers have found a way to manipulate lists to save high scores or records. You can save the number into a user-created list and it's far less likely to be messed with. For the sake of this example the program will be called GAME, so the user-created list will also be called GAME. Don't worry, everything that's not explained in the commented section will be explained at the end. User variable H is your score in the game. Read the stuff below the code before running the program.

: For AShell, SOS, and TI-Explorer
Lbl 1
ClrHome Clears the home screen
1STOREdim(LGAME) Makes 1 the dimension of LGame.
Menu("CLEAR HIGH SCORE","NO",Z,"YES",BB) A menu that asks if you want to clear the high score
Lbl BB
{0}STORELGAME Clears the high score
Lbl Z
Input "YOUR SCORE? ",H Stores your score into user variable H
If H>LGAME(1):Then If the your score is more than the present high score, then
HSTORELGAME(1) Makes your score the new high score and
Output(4,2,"NEW HIGH SCORE") Displays "NEW HIGH SCORE"
End End If-Then statement
Output(6,1,"HIGH SCORE:") Displays "HIGH SCORE:" on line 6
Output(6,13,LGAME(1)) Displays the high score on the same line
Pause Suspends program and waits of user to press ENTER
ClrHome Clears the home screen
Menu("AGAIN?","YES",1,"NO",A) A menu that asks if you want to do this again
Lbl A
Stop End the entire program

Further Explanation:
First of all, run the program; choose not to clear the score, and when it asks you for your score enter a low number like 50. It'll say that there's a new high score and it'll display the high score at the bottom. Run it again and this time enter 60. It should say the same thing except that the high score is now 60. Now enter 30. It shouldn't say that there's a new high score and the high score should still read 60. If all of the tests work, then your high score function works!! Now let me show you some stuff in the code. When the calculator executes -- :1STOREdim(LGAME) -- it does one of two things depending on the condition. This is what really makes this high score function work. You see, when you first ran the program, there was no list called GAME. So when the calculator ran that little piece of code above, it actually created a list called GAME and gave it a dimension of one. If there is already a list called GAME, it doesn't do anything. Well, really it just re-makes the dimension of the list, one. You definitely need this piece of code because it's the only way to test if the game had been played before. At first, when I tried to make a high score function with a list, it wouldn't work because I wouldn't be able to create the new list in the game. Then when I figured out how to store values into lists I did this: {0}STORELGAME(1). The problem with that code is that it makes the high score zero every time you run the program, which defeats the whole purpose. Finally, I figured out how to manipulate the list so that it could work for me. Note: If you want to store into a specific entry in a list, compare a specific entry in a list with something else, or display a specific entry in a list you must put () (parenthesis) around the list entry number. If you don't put parenthesis and the list entry number, what you want to happen, won't. I'll explain one more thing, the code only works for High Scores. If you want to do the same thing for a Record, you have to slightly change the code. I consider a Record different from a High Score. For example, if you had a guessing game and you wanted to keep the record of the number of tries, the lowest would be the best, right? That's what I consider to be a record. The only code you'll have to change is the code on line 7: {0}STORELGAME. You would have to change the 0 inside the {} to an exorbitant value like 9999. You do this so that the record is "cleared". It's pretty hard for someone to have more than 9,999 tries. So when the game is played, the number of tries it took you will be the new record. You should be able to understand everything else because of the comments next to them. Wow, this was meant to only be a little footnote with a code, but it ended up being like a tutorial! If you got to this example of High Score from The Big Game, click here to go back there.

If you do not understand a particular part in this lesson, have suggestions, or find any problems please contact me.





Problems with this page?
Contact the Webmaster.