How to create a highscore? please

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Bernard51
Prole
Posts: 46
Joined: Fri Jan 20, 2017 5:51 pm

How to create a highscore? please

Post by Bernard51 »

Hi

I'm looking for a tutorial to create a A highscore with writing and reading a file

Thank you

Bernard
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: How to create a highscore? please

Post 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)
Bernard51
Prole
Posts: 46
Joined: Fri Jan 20, 2017 5:51 pm

Re: How to create a highscore? please

Post by Bernard51 »

Thank You very much
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: How to create a highscore? please

Post by xNick1 »

You're welcome :D
Post Reply

Who is online

Users browsing this forum: No registered users and 168 guests