Another way to make your game more exciting is to keep score. High Scores: a quick way to Save/Show High Scores Saving the last High Score The following code goes in where the game is over, to ask the player's name and record his/her high score
Showing all high scores Have a button to show the high scores to date
A. High Scores: an example where the scores are in a variable - myScore The Code: The following code will add your name ("theName") and your score ("myScore") at the end of the high Scores file ("scores.txt") Put the code where the game ends. Comments are in green: This uses the variables: theName, myScore, theFile Without Comments: ask "What is your name?" put it into theName put specialFolderPath("Documents") & "/scores.txt" into theFile put URL ("file:" & theFile ) into theData put theName & tab & myScore & return after theData put theData into URL ( "File:" & theFile) With Comments: // get the player's name ask "What is your name?" put it into theName // save the path and file name into the variable "theFile". //LiveCode keeps track of where the documents go on your system in a specialFolderPath put specialFolderPath("Documents") & "/scores.txt" into theFile //get the current scores from the file and put it into a variable - "theData" (you could put his into a field also) put URL ("file:" & theFile ) into theData // add the player's name and score to the end of the file ("after") put theName & tab & myScore & return after theData // and save it back to the hard drive put theData into URL ( "File:" & theFile) B. Low Times: an example where the time is kept in a field - yourTime (the files are available below if you want to see the whole game) The Code: The following code will add your name ("theName") and your time ("yourTime") at the end of the best times file ("BestTimes.txt") Put the code where the game ends. Comments are in green: i This uses the variables: theName, field "yourTime", thePath Without Comments: ask "What is your name?" put it into theName put specialFolderPath("Documents") & "/BestTimes.txt" into thePath put URL ("file:" & thePath ) into theTimes put theName & tab & field "yourTime" & return after theTimes put theTimes into URL ( "File:" & thePath) With Comments: // Save the player's name and time // get the player's name ask "What is your name?" put it into theName // get the path of your "Documents" folder. // (LiveCode keeps track of where the documents go on your system in a specialFolderPath) put specialFolderPath("Documents") & "/BestTimes.txt" into thePath // get the previous times from the file, add yours to the bottom and save it back // put the contents of the file into the variable (theTimes) put URL ("file:" & thePath ) into theTimes // add the player's name and score to the end of the file ("after") put theName & tab & field "yourTime" & return after theTimes // and save it back to the hard drive put theTimes into URL ( "File:" & thePath) The Explanation: 1. Keeping Track of Scores or TimesTo keep track of a score, we need a text field on the screen and a "Reset" button to set the score back to zero. (If you have a "Start" button, then you can use that to set the score back to zero.) Then anywhere that you need to, you change the score by adding or subtracting to/from it. Some places that you may put code to change it are intersects (when your player touches a "diamond' you add 10 to the score, or when your player runs into a "bomb" you subtract 10 points, etc) Adding the Score Keeper
On the card script, add the code to set the score to zero when the card is first opened put 0 into field "Score" end openCard or add the code to the button "Start" or the button "Reset" on mouseUp put 0 into field "Score" end mouseUp
2. Saving Scores You can save scores, data and other information in files for the next game. To do this, you need to use a variable or create a field to collect the information, then write that information out to a file on your computer/device. Once you have the data together in one place, you just write that out to a file on your computer. 1. Using a Variable If I use a variable called mydata, I can assemble the data and write it out to a file when I am done. This will have the player's name and score and today's date and time: ask "What is your name?" put it into tName put (tName && timer && return) after mydata [ You could also add the date and time to the score ] [ put (tName && timer && date() && time() & return) after field "mydata" ] put mydata into URL ( "file:" & "MyScoresFile.txt" ) 2. Using a Text Field Suppose I have a text entry field on my card called "mydata". To save my scores, I would have code: put (tName && timer && date() && time() & return) after field "mydata" put field "mydata" into URL ( "file:" & "MyScoresFile.txt" ) notes: return =carriage return (Enter) - this puts every item on new line The first item we put "into" the field, this erases what was there before The others we put "after" which adds them to the end - "after" what is already there If you allow the user to choose where to save the data, you would code: ask file "Save to...?" put it into myScoresFile put mydata into URL( "file:" & myScoresFile ) If you are on a network or your computer is set to Strong Security, you may not be able to save it just anywhere. LiveCode keeps track of where your documents are with "specialfolderpath("documents") (It is different on every system, so this is the best way if you are going to run your program on different platforms (Mac, PC, Linux...) put specialfolderpath("documents") & "/HighScore.txt" into myScoresFile put mydata into url( "file:" & myScoresFile ) Saving Scores - How to save scores and other info. to a file and between games. Retrieving Scores You would use code like this: put URL ( "file:" & myFile ) into field "mydata" or put URL ( "file:" & myFile ) into x |
Home >
