Text doesn't get updated [SOLVED]
Posted: Mon May 27, 2024 3:23 pm
Solution:
Instead of checking if the ball is below 0. I added a margin. 7 seemed to be the sweet spot.
Definitely not the best solution, but a solution nonetheless.
New to love2d
Making a pong game to get the feel for it. However cannot get my score updated even though I think I am doing everything right.
The initial score (0 0) gets on the screen but after a goal happens the score is not updated on the screen. I have also tried with removing dt in arguments in the scoreUpdate function.
Score = {}
function Score:load()
self.x = love.graphics.getWidth() / 2 - 100
self.y = love.graphics.getHeight() - love.graphics.getHeight() + 50
self.xPlayerScore = love.graphics.getWidth() / 2
self.yPlayerScore = love.graphics.getHeight() - love.graphics.getHeight() + 50
self.playerScore = 0
self.xAiScore = love.graphics.getWidth() / 2 + 40
self.yAiScore = love.graphics.getHeight() - love.graphics.getHeight() + 50
self.aiScore = 0
end
function Score:update(dt)
self:scoreUpdate(dt)
end
function Score:scoreUpdate(dt)
if Ball.x < 0 then
self.aiScore = self.aiScore + 1 * dt
end
if Ball.x + Ball.width > love.graphics.getWidth() then
self.playerScore = self.playerScore + 1 * dt
end
end
function Score:draw()
love.graphics.print("SCORE:", self.x, self.y)
love.graphics.print(tostring(self.playerScore), self.xPlayerScore, self.yPlayerScore)
love.graphics.print(tostring(self.aiScore), self.xAiScore, self.yAiScore)
end
Instead of checking if the ball is below 0. I added a margin. 7 seemed to be the sweet spot.
Definitely not the best solution, but a solution nonetheless.
New to love2d
Making a pong game to get the feel for it. However cannot get my score updated even though I think I am doing everything right.
The initial score (0 0) gets on the screen but after a goal happens the score is not updated on the screen. I have also tried with removing dt in arguments in the scoreUpdate function.
Score = {}
function Score:load()
self.x = love.graphics.getWidth() / 2 - 100
self.y = love.graphics.getHeight() - love.graphics.getHeight() + 50
self.xPlayerScore = love.graphics.getWidth() / 2
self.yPlayerScore = love.graphics.getHeight() - love.graphics.getHeight() + 50
self.playerScore = 0
self.xAiScore = love.graphics.getWidth() / 2 + 40
self.yAiScore = love.graphics.getHeight() - love.graphics.getHeight() + 50
self.aiScore = 0
end
function Score:update(dt)
self:scoreUpdate(dt)
end
function Score:scoreUpdate(dt)
if Ball.x < 0 then
self.aiScore = self.aiScore + 1 * dt
end
if Ball.x + Ball.width > love.graphics.getWidth() then
self.playerScore = self.playerScore + 1 * dt
end
end
function Score:draw()
love.graphics.print("SCORE:", self.x, self.y)
love.graphics.print(tostring(self.playerScore), self.xPlayerScore, self.yPlayerScore)
love.graphics.print(tostring(self.aiScore), self.xAiScore, self.yAiScore)
end