Skip to content

Commit

Permalink
Implement high score for second player.
Browse files Browse the repository at this point in the history
  • Loading branch information
dulsi committed Feb 7, 2023
1 parent e239786 commit a0b0e39
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 54 deletions.
118 changes: 91 additions & 27 deletions states/GameOverState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,107 @@ GameOverState = Class{__includes = BaseState}


function GameOverState:init()
self.name = ''
end

function GameOverState:textinput(text)
if #self.name < 3 then
self.name = self.name .. text
end
self.name = { }
self.position = { }
self.keys = {
{left = "left", right = "right", up = "up", down = "down" },
{left = "a", right = "d", up = "w", down = "s" }
}
self.delay = { 0, 0 }
end

function GameOverState:update(dt)
if love.keyboard.keyPressed['return'] then
if #self.name ~= 0 then
gStateMachine:change('highscore', {
new = {['score'] = self.score, ['name'] = self.name}
})
end
end
if love.keyboard.keyPressed['backspace'] then
if #self.name ~= 0 then
self.name = string.sub(self.name, 1, #self.name - 1)
end
end
for i, _ in pairs(self.score) do
self.delay[i] = self.delay[i] + dt
if self.delay[i] > 0.3 then
if love.keyboard.isDown(self.keys[i].left) then
if self.position[i] ~= 4 and self.position[i] > 1 then
self.position[i] = self.position[i] - 1
self.name[i] = string.sub(self.name[i], 1, self.position[i])
end
self.delay[i] = 0
elseif love.keyboard.isDown(self.keys[i].right) then
if self.position[i] ~= 4 then
self.position[i] = self.position[i] + 1
if self.position[i] ~= 4 then
self.name[i] = self.name[i] .. " "
else
done = true
local new = {}
for j, e in pairs(self.position) do
table.insert(new, {['score'] = self.score[j], ['name'] = self.name[j]})
if e ~= 4 then
done = false
end
end
if done then
gStateMachine:change('highscore', {
new = new
})
end
end
end
self.delay[i] = 0
elseif love.keyboard.isDown(self.keys[i].up) then
if self.position[i] ~= 4 then
lastchar = string.sub(self.name[i], #self.name[i])
if #self.name[i] == 1 then
self.name[i] = ''
else
self.name[i] = string.sub(self.name[i], 1, #self.name[i] - 1)
end
self.name[i] = self.name[i] .. ('ABCDEFGHIJKLMNOPQRSTUVWXYZ A'):match(lastchar..'(.)')
end
self.delay[i] = 0
elseif love.keyboard.isDown(self.keys[i].down) then
if self.position[i] ~= 4 then
lastchar = string.sub(self.name[i], #self.name[i])
if #self.name[i] == 1 then
self.name[i] = ''
else
self.name[i] = string.sub(self.name[i], 1, #self.name[i] - 1)
end
self.name[i] = self.name[i] .. (' ZYXWUVTSRQPONMLKJIHGFEDCBA '):match(lastchar..'(.)')
end
self.delay[i] = 0
end
end
end
end

function GameOverState:draw()
w, h = VIRTUAL_WIDTH, VIRTUAL_HEIGHT
love.graphics.setFont(largeFont)
love.graphics.printf({{1, 0, 0}, "New high score!"}, 0, h / 3, w, "center")
-- love.graphics.setFont(smallFont)
love.graphics.printf({{1, 1, 1}, "Your Score: ", {0, 1, 0}, self.score}, 0, h - h / 4 - 50, w, "center")
love.graphics.printf({{1, 1, 0}, "Enter Your Initials"}, 0, h - h / 4, w, "center")
love.graphics.printf(self.name, 0, h - (h / 4) + 10, w, "center")
w, h = VIRTUAL_WIDTH, VIRTUAL_HEIGHT / table.getn(self.score)
base = 0
for i, e in pairs(self.score) do
love.graphics.setFont(largeFont)
if (not highscores[10]) or (e > highscores[10][1]) then
love.graphics.printf({{1, 0, 0}, "New high score!"}, 0, base + h / 3, w, "center")
end
-- love.graphics.setFont(smallFont)
love.graphics.printf({{1, 1, 1}, "Player ", {1, 1, 1}, i, {1, 1, 1}, " Score: ", {0, 1, 0}, e}, 0, base + h - h / 4 - 50 / table.getn(self.score), w, "center")
if (not highscores[10]) or (e > highscores[10][1]) then
love.graphics.printf({{1, 1, 0}, "Enter Your Initials"}, 0, base + h - h / 4, w, "center")
if self.position[i] ~= 4 then
love.graphics.printf(self.name[i] .. "_", 0, base + h - (h / 4) + 10, w, "center")
else
love.graphics.printf(self.name[i], 0, base + h - (h / 4) + 10, w, "center")
end
end
base = base + h
end
end

function GameOverState:enter(params)
self.score = params.score
for i, e in pairs(self.score) do
table.insert(self.name, ' ')
if (not highscores[10]) or (e > highscores[10][1]) then
table.insert(self.position, 1)
else
table.insert(self.position, 4)
end
end
end

function GameOverState:exit()
end
end
48 changes: 25 additions & 23 deletions states/HighScoreState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,31 @@ function HighScoreState:saveHighScores()
end

function HighScoreState:newHighScore(new)
score = new['score']
name = new['name']

for i=1,10 do
if (not self.highscores[i]) then
self.highscores[i] = {score, name}
break
end
if score > self.highscores[i][1] then
if i == 10 then
self.highscores[i] = {score, name}
else
for j=9,i,-1 do
self.highscores[j+1] = self.highscores[j]
end
self.highscores[i] = {score, name}
end
break
end
end

self:saveHighScores()
self:getHighScores()
for _, next in pairs(new) do
score = next['score']
name = next['name']

for i=1,10 do
if (not self.highscores[i]) then
self.highscores[i] = {score, name}
break
end
if score > self.highscores[i][1] then
if i == 10 then
self.highscores[i] = {score, name}
else
for j=9,i,-1 do
self.highscores[j+1] = self.highscores[j]
end
self.highscores[i] = {score, name}
end
break
end
end

self:saveHighScores()
self:getHighScores()
end
end

function HighScoreState:update(dt)
Expand Down
19 changes: 15 additions & 4 deletions states/PlayState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,28 @@ function PlayState:enter(params)

changed = false

if (not highscores[10]) or (self.players[1].score > highscores[10][1]) then
changed = true
elseif (table.getn(self.player) == 2) and (self.players[2].score > highscores[10][1]) then
changed = true
end
for i=1,10 do
if (not highscores[i]) or (self.players[1].score > highscores[i][1]) then
gStateMachine:change("gameover", {
score = self.players[1].score
})
changed = true
break
end
end

if not changed then gStateMachine:change('title') end
if not changed then gStateMachine:change('title')
else
local score = {}
for _, e in pairs(self.players) do
table.insert(score, e.score)
end
gStateMachine:change("gameover", {
score = score
})
end
end

self.entityManager.player = {}
Expand Down

0 comments on commit a0b0e39

Please sign in to comment.