How Do I Make A High Score System?

BlitzPlus Forums/BlitzPlus Beginners Area/How Do I Make A High Score System?

Problem:I want to create a high score system for my pong clone. I cant figure it out and there arent ANY examples on the web. I want it to record how many times you hit the ball in succesion,what do I need to add?

EDIT:
Writing and reading to/from a file is my problem, NOT variables. when I do that, all the scores are 0!!!

I'm a bit confused how you wrote a pong clone when you don't understand how to check to see if the current score is greater than each score in a list of high scores, or even a single high score stored in its own variable.

When I started programming, I didn't learn how to make a ball bounce off walls and a paddle until years after I learned how to keep score. :-)

I know how to keep score, its Reading/Writing the score to a file.

Ohhhh. :-)



	Dim Highscore_Name$(10, 2)
	Dim Highscore_Level(10, 2)
	Dim Highscore_Score(10, 2)


; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function checks the player's current score against the high score list and inserts their name into the list if they have achived a new high score.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Update_Highscores()

	Local Loop, Loop2

	Load_Highscores()

	; Loop through the highscore list from the highest score to the lowest.	
	For Loop = 0 To 9
	
		; If this player's score is greater than the highscore at this location in the list...
		If Savegame_Score(GLOBAL_Editing_Savegame, GLOBAL_Game_Mode) > Highscore_Score(Loop, GLOBAL_Game_Mode)
			
			; Move each highscore, starting at this one, down by one, pushing the last one off the list.
			
				For Loop2 = Loop To 8					
				
					Highscore_Name$(Loop2+1, GLOBAL_Game_Mode) = Highscore_Name$(Loop2, GLOBAL_Game_Mode)
					Highscore_Level(Loop2+1, GLOBAL_Game_Mode) = Highscore_Level(Loop2, GLOBAL_Game_Mode)
					Highscore_Score(Loop2+1, GLOBAL_Game_Mode) = Highscore_Score(Loop2, GLOBAL_Game_Mode)
					
				Next

			; Add this player's score to the list at this location.
			;
			; We use the score and level variables instead of the saved score because we want the score in the middle of the level to be saved, not the score
			; at the end of the last level.  This score will not have the level bonus added.  The player only gets that if they complete the level.

				Highscore_Name$(Loop, GLOBAL_Game_Mode) = Savegame_Name$(GLOBAL_Editing_Savegame)
				Highscore_Level(Loop, GLOBAL_Game_Mode) = Level
				Highscore_Score(Loop, GLOBAL_Game_Mode) = Score

			; Exit the loop early.
				Exit
			
		EndIf

	Next	
	
	Save_Highscores()
		
End Function


; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function loads the current high score data.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Load_Highscores()

	Local Filename$
	Local DataFile
	Local Loop

	; Attempt to open the data file for reading.
	; If the operation fails, no data will be loaded, but this will just result in no high scores being displayed, so no biggie.

		Filename$ = DATA_PATH$ + "highscores.dat"
		DataFile = ReadFile(Filename$)

	; Load the data.
	
		For Loop = 0 To 9
			
			Highscore_Name$(Loop, GAME_MODE_ARCADE) = ReadLine$(DataFile)
			Highscore_Level(Loop, GAME_MODE_ARCADE) = ReadLine$(DataFile)
			Highscore_Score(Loop, GAME_MODE_ARCADE) = ReadLine$(DataFile)
			
		Next

		For Loop = 0 To 9
			
			Highscore_Name$(Loop, GAME_MODE_ADVANCED) = ReadLine$(DataFile)
			Highscore_Level(Loop, GAME_MODE_ADVANCED) = ReadLine$(DataFile)
			Highscore_Score(Loop, GAME_MODE_ADVANCED) = ReadLine$(DataFile)
			
		Next
	
	; Close the data file.
		CloseFile DataFile
	
End Function


; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function deletes any high score file that exists and then saves a new one with the current high score data.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Save_Highscores()

	Local Filename$
	Local DataFile
	Local Loop

	; Attempt to open the data file for reading.

		Filename$ = DATA_PATH$ + "highscores.dat"
		If FileType(Filename$) = 1 Then DeleteFile Filename$

		DataFile = WriteFile(Filename$)

	; Write the data.
	
		For Loop = 0 To 9
			
			WriteLine(DataFile, Highscore_Name$(Loop, GAME_MODE_ARCADE))
			WriteLine(DataFile, Highscore_Level(Loop, GAME_MODE_ARCADE))
			WriteLine(DataFile, Highscore_Score(Loop, GAME_MODE_ARCADE))
			
		Next

		For Loop = 0 To 9
			
			WriteLine(DataFile, Highscore_Name$(Loop, GAME_MODE_ADVANCED))
			WriteLine(DataFile, Highscore_Level(Loop, GAME_MODE_ADVANCED))
			WriteLine(DataFile, Highscore_Score(Loop, GAME_MODE_ADVANCED))
			
		Next
	
	; Close the data file.
		CloseFile DataFile

End Function


@sswift :Thanks. Im about to try it.
EDIT:
I might have to change the code a little.
I'm recording the amount of times you hit the ball in a round, the number of rounds you won before the computer won ten,the name of the player,EDIT: and what mode, I just added endless.Do you think you could modify it a bit?
P.S. Its Rally pong,the one in the blitz showcase. Ill give you credit for the high score system if you help,please?

I think it'd be best for you to look at the code and figure out how to make those changes yourself. It's not that difficult. :-)

I'll give you a hint:

All the code is doing is writing lines of text to a text file, and then reading those lines back in. For each player it writes 3 lines, and there are 10 players. So it writes the first name, then the level, then the score, and then does the same for the next player, until it has done all players. There's nothing done different for each data type. Text and numbers are written the same way to the file. If you need to store more data for each player, just add more WriteLine and ReadLines.

Ok, ill give it a try. Im not great with the write/read routines though.
EDIT:
Erm...No savegames and levels...confused...help...:(
EDIT 2:
Fixed it to some extent,just need to display it at the title screen. could I have a hint on that(was no function for displaying in code.)
EDIT 3:
MAV.
	Dim Highscore_Name$(10)
	Dim Highscore_Score(10)
	Dim Highscore_Whacks(10)


; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function checks the player's current score against the high score list and inserts their name into the list if they have achived a new high score.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Update_Highscores()

	Local Loop, Loop2

	Load_Highscores()

	; Loop through the highscore list from the highest score to the lowest.	
	For Loop = 0 To 9
	
		; If this player's score is greater than the highscore at this location in the list...
		If highwhacks > Highscore_Score(Loop)
			
			; Move each highscore, starting at this one, down by one, pushing the last one off the list.
			
				For Loop2 = Loop To 8					
				
					Highscore_Name$(Loop2+1) = Highscore_Name$(Loop2)
					Highscore_Score(Loop2+1) = Highscore_Score(Loop2)
					Highscore_Whacks(Loop2+1) = Highscore_Whacks(Loop2)
					
				Next

			; Add this player's score to the list at this location.
			;
			; We use the score and level variables instead of the saved score because we want the score in the middle of the level to be saved, not the score
			; at the end of the last level.  This score will not have the level bonus added.  The player only gets that if they complete the level.

				Highscore_Name$(Loop) = Input$("What Is your name?")
				Highscore_Score(Loop) = player\score
				Highscore_Whacks(Loop) = whackshigh

			; Exit the loop early.
				Exit
			
		EndIf

	Next	
	
	Save_Highscores()
		
End Function


; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function loads the current high score data.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Load_Highscores()

	Local Filename$
	Local DataFile
	Local Loop

	; Attempt to open the data file for reading.
	; If the operation fails, no data will be loaded, but this will just result in no high scores being displayed, so no biggie.

		Filename$ = DATA_PATH$ + "highscores.dat"
		DataFile = ReadFile(Filename$)

	; Load the data.
	
		For Loop = 0 To 9
			
			Highscore_Name$(Loop) = ReadLine$(DataFile)
			Highscore_Score(Loop) = ReadLine$(DataFile)
			Highscore_Whacks(Loop) = ReadLine$(DataFile)
			
		Next
	
	; Close the data file.
		CloseFile DataFile
	
End Function


; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function deletes any high score file that exists and then saves a new one with the current high score data.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Save_Highscores()

	Local Filename$
	Local DataFile
	Local Loop

	; Attempt to open the data file for reading.

		Filename$ = DATA_PATH$ + "highscores.dat"
		If FileType(Filename$) = 1 Then DeleteFile Filename$

		DataFile = WriteFile(Filename$)

	; Write the data.
	
		For Loop = 0 To 9
			
			WriteLine(DataFile, Highscore_Name$(Loop))
			WriteLine(DataFile, Highscore_Score(Loop))
			WriteLine(DataFile, Highscore_Whacks(Loop))
			
		Next
	
	; Close the data file.
		CloseFile DataFile

End Function

Function DisplayHighscores()
Local textloop
Load_Highscores()

For textloop = 0 To 9

Print Highscore_Name$(textloop)
Print Highscore_Score(textloop)
Print Highscore_Whacks(textloop)

Next

End Function






http://acronyms.thefreedictionary.com/MAV

Monkey Attack Victim?

Memory access violation. :)

I glanced at your code, but I don't see what might have caused a MAV.

Huh. its strange...

Someone please HELP!!!

Is that the code in the box a few posts above?

Yes. The latest one I posted gets a mem access violation

Um... Did you manually create a highscores.dat file, or call the save function before trying to load the highscores? Cause contrary to my comment in the code, trying to read lines from a nonexistent data file might be a bad idea.

No. I created a file in notepad and saved it. I didnt read from a non-existing file.(psst...I deleted all the code cuz I was annoyed. Its only on the forums now...)

umm...somebody help?

This seems pretty straight forward:

http://www.blitzbasic.com/codearcs/codearcs.php?code=171

What do I need to change in that to make it work?
I have the amount of whacks, the score, and the name.I need a function that lets me input a name, not just read it.

YAY!!! IT WORKS!!! WOOHOO!!!!
Thanks for telling me about that yeshu!

No problem.

Here Is How I Set HighScores


fileout1 = WriteFile("data1.dat")
WriteString(fileout1,"player1")
WriteByte(fileout1,0)
CloseFile(fileout1)

fileout2 = WriteFile("data2.dat")
WriteString(fileout2,"player2")
WriteByte(fileout2,0)
CloseFile(fileout2)

fileout3 = WriteFile("data3.dat")
WriteString(fileout3,"player3")
WriteByte(fileout3,0)
CloseFile(fileout3)

fileout4 = WriteFile("data4.dat")
WriteString(fileout4,"player4")
WriteByte(fileout4,0)
CloseFile(fileout4)

fileout5 = WriteFile("data5.dat")
WriteString(fileout5,"player5")
WriteByte(fileout5,0)
CloseFile(fileout5)

fileout6 = WriteFile("data6.dat")
WriteString(fileout6,"player6")
WriteByte(fileout6,0)
CloseFile(fileout6)

fileout7 = WriteFile("data7.dat")
WriteString(fileout7,"player7")
WriteByte(fileout7,0)
CloseFile(fileout7)

fileout8 = WriteFile("data8.dat")
WriteString(fileout8,"player8")
WriteByte(fileout8,0)
CloseFile(fileout8)

fileout9 = WriteFile("data9.dat")
WriteString(fileout9,"player9")
WriteByte(fileout9,0)
CloseFile(fileout9)

fileout10 = WriteFile("data10.dat")
WriteString(fileout10,"player10")
WriteByte(fileout10,0)
CloseFile(fileout10)


Read them Like This

filein1 = ReadFile("data1.dat")
place1$ = ReadString$(filein1)
score1 = ReadInt(filein1)
CloseFile(filein1)

filein2 = ReadFile("data2.dat")
place2$ = ReadString$(filein2)
score2 = ReadInt(filein2)
CloseFile(filein2)

filein3 = ReadFile("data3.dat")
place3$ = ReadString$(filein3)
score3 = ReadInt(filein3)
CloseFile(filein3)

filein4 = ReadFile("data4.dat")
place4$ = ReadString$(filein4)
score4 = ReadInt(filein4)
CloseFile(filein4)

filein5 = ReadFile("data5.dat")
place5$ = ReadString$(filein5)
score5 = ReadInt(filein5)
CloseFile(filein5)

filein6 = ReadFile("data6.dat")
place6$ = ReadString$(filein6)
score6 = ReadInt(filein6)
CloseFile(filein6)

filein7 = ReadFile("data7.dat")
place7$ = ReadString$(filein7)
score7 = ReadInt(filein7)
CloseFile(filein7)

filein8 = ReadFile("data8.dat")
place8$ = ReadString$(filein8)
score8 = ReadInt(filein8)
CloseFile(filein8)

filein9 = ReadFile("data9.dat")
place9$ = ReadString$(filein9)
score9 = ReadInt(filein9)
CloseFile(filein9)

filein10 = ReadFile("data10.dat")
place10$ = ReadString$(filein10)
score10 = ReadInt(filein10)
CloseFile(filein10)

and you have a HighScore System!!!

Here Is How I Set HighScores


fileout1 = WriteFile("data1.dat")
WriteString(fileout1,"player1")
WriteByte(fileout1,0)
CloseFile(fileout1)

fileout2 = WriteFile("data2.dat")
WriteString(fileout2,"player2")
WriteByte(fileout2,0)
CloseFile(fileout2)

fileout3 = WriteFile("data3.dat")
WriteString(fileout3,"player3")
WriteByte(fileout3,0)
CloseFile(fileout3)

fileout4 = WriteFile("data4.dat")
WriteString(fileout4,"player4")
WriteByte(fileout4,0)
CloseFile(fileout4)

fileout5 = WriteFile("data5.dat")
WriteString(fileout5,"player5")
WriteByte(fileout5,0)
CloseFile(fileout5)

fileout6 = WriteFile("data6.dat")
WriteString(fileout6,"player6")
WriteByte(fileout6,0)
CloseFile(fileout6)

fileout7 = WriteFile("data7.dat")
WriteString(fileout7,"player7")
WriteByte(fileout7,0)
CloseFile(fileout7)

fileout8 = WriteFile("data8.dat")
WriteString(fileout8,"player8")
WriteByte(fileout8,0)
CloseFile(fileout8)

fileout9 = WriteFile("data9.dat")
WriteString(fileout9,"player9")
WriteByte(fileout9,0)
CloseFile(fileout9)

fileout10 = WriteFile("data10.dat")
WriteString(fileout10,"player10")
WriteByte(fileout10,0)
CloseFile(fileout10)


Read them Like This

filein1 = ReadFile("data1.dat")
place1$ = ReadString$(filein1)
score1 = ReadInt(filein1)
CloseFile(filein1)

filein2 = ReadFile("data2.dat")
place2$ = ReadString$(filein2)
score2 = ReadInt(filein2)
CloseFile(filein2)

filein3 = ReadFile("data3.dat")
place3$ = ReadString$(filein3)
score3 = ReadInt(filein3)
CloseFile(filein3)

filein4 = ReadFile("data4.dat")
place4$ = ReadString$(filein4)
score4 = ReadInt(filein4)
CloseFile(filein4)

filein5 = ReadFile("data5.dat")
place5$ = ReadString$(filein5)
score5 = ReadInt(filein5)
CloseFile(filein5)

filein6 = ReadFile("data6.dat")
place6$ = ReadString$(filein6)
score6 = ReadInt(filein6)
CloseFile(filein6)

filein7 = ReadFile("data7.dat")
place7$ = ReadString$(filein7)
score7 = ReadInt(filein7)
CloseFile(filein7)

filein8 = ReadFile("data8.dat")
place8$ = ReadString$(filein8)
score8 = ReadInt(filein8)
CloseFile(filein8)

filein9 = ReadFile("data9.dat")
place9$ = ReadString$(filein9)
score9 = ReadInt(filein9)
CloseFile(filein9)

filein10 = ReadFile("data10.dat")
place10$ = ReadString$(filein10)
score10 = ReadInt(filein10)
CloseFile(filein10)

and you have a HighScore System!!!
You realise that if you learn about arrays/types, your code will be 90% shorter? Why the hell would you want ten separate files for a high score system when each file is going to allocate a minimum of 4096 bytes (4k) or hard drive space? Its awfully inefficient and a horrid way of doing it.

I appreciate that you're new and no problem with trying to help people, but you probably shouldn't dig up a 3 month old thread to post a solution to a problem that A) has already been solved, and B) your solution is a really, really bad solution.