Page 1 of 1
How to create a highscore? please
Posted: Thu Mar 02, 2017 9:19 am
by Bernard51
Hi
I'm looking for a tutorial to create a A highscore with writing and reading a file
Thank you
Bernard
Re: How to create a highscore? please
Posted: Thu Mar 02, 2017 10:43 am
by xNick1
I once implemented it using "classic" to create the Score Object.
Follow Sheepolution's tutorials to learn more about "classic".
This example also uses "ser" to save and load the score from the file.
Feel free to ask questions if you don't understand something.
Code: Select all
Score = Object:extend()
-- Set the X and Y position of the score label
function Score:new(x, y)
self.x = wWidth - 100
self.y = 20
end
function Score:update(dt)
end
function Score:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.print("Score: " .. scor, self.x, self.y)
end
function Score:saveMaxScore()
local data = {}-- Make a table to store variables in.
data.maxScor = scor
-- Save the table to the "savegame.txt" file:
love.filesystem.write("savegame.txt", serialize(data))
end
function Score:loadMaxScore()
if not love.filesystem.exists("savegame.txt") then
maxScor = 0
score:saveMaxScore()
end
-- Load the data table:
local data = love.filesystem.load("savegame.txt")()
-- Copy the variables out of the table:
maxScor = data.maxScor
return maxScor
end
When the bullet hits the enemy:
Code: Select all
scor = scor + 1
if scor > maxScor then
score:saveMaxScore()
end
You should do the last check at the end of the game anyway.
In the menu:
Code: Select all
maxScor = score:loadMaxScore()
love.graphics.print("Best Score: " .. maxScor, wWidth - 150, wHeight - 30)
Re: How to create a highscore? please
Posted: Thu Mar 02, 2017 12:58 pm
by Bernard51
Thank You very much
Re: How to create a highscore? please
Posted: Thu Mar 02, 2017 7:31 pm
by xNick1
You're welcome