Page 1 of 1
Help creating a timer
Posted: Tue Dec 08, 2015 2:45 am
by arr5454
Hey everyone,
I'm fairly new to coding and love2d and I'm working on a simple Pong game to practice developing. I got the basic game to work and I would like to implement a feature where each time the ball spawns in the center of the screen after each point, it will stay there for 3 seconds before moving. I've read different ways to make a timer but I'm not sure where to put it (in the drawBall() function in ball.lua, or just in main.lua?) or how to make it countdown in real seconds. I would also like to make the countdown appear in the center of the screen in a "3, 2, 1 GO!" sort of way.
Thank you very much in advance!
EDIT: Forgot to upload game file
Re: Help creating a timer
Posted: Tue Dec 08, 2015 4:34 am
by Beelz
I would go with something like this:
Code: Select all
canMove = false
moveTime = 3
love.update(dt)
if moveTime > 0 then
moveTime = moveTime - 1 * dt
canMove = false
else
canMove = true
end
if canMove then
-- Move
end
end
Re: Help creating a timer
Posted: Wed Dec 09, 2015 9:05 am
by kbmonkey
This is a very nice looking pong clone!
Similar to what Beelz said, I would functionalize the pause code:
Code: Select all
function updatePauseTimeout(dt)
pauseTimeout = pauseTimeout and (pauseTimeout - dt) or 4
return pauseTimeout < 1
end
function drawPauseTimeout()
if pauseTimeout > 3 then
love.graphics.printf("3", 0, 100, love.window.getWidth(), "center")
elseif pauseTimeout > 2 then
love.graphics.printf("2", 0, 100, love.window.getWidth(), "center")
elseif pauseTimeout > 1 then
love.graphics.printf("1", 0, 100, love.window.getWidth(), "center")
elseif pauseTimeout > 0 then
love.graphics.printf("GO!", 0, 100, love.window.getWidth(), "center")
end
end
Then just call updatePauseTimeout(dt) and drawPauseTimeout() from love.update and love.draw.
When the ball is lost, simply set
Re: Help creating a timer
Posted: Wed Dec 09, 2015 9:28 am
by s-ol
arr5454 wrote:Hey everyone,
I'm fairly new to coding and love2d and I'm working on a simple Pong game to practice developing. I got the basic game to work and I would like to implement a feature where each time the ball spawns in the center of the screen after each point, it will stay there for 3 seconds before moving. I've read different ways to make a timer but I'm not sure where to put it (in the drawBall() function in ball.lua, or just in main.lua?) or how to make it countdown in real seconds. I would also like to make the countdown appear in the center of the screen in a "3, 2, 1 GO!" sort of way.
Thank you very much in advance!
EDIT: Forgot to upload game file
Where you put the timer is up to you really, I would put it in main.lua probably since it is not really ball-related.
To count seconds, you can use dt:
Code: Select all
local countdown = 3 -- do sth after 3 seconds
function love.update(dt)
if countdown and countdown > 0 then -- only if countdown is running
countdown = countdown - dt
end
if countdown < 0 then
countdown = nil
ball.paused = false -- for example
end
end
-- somewhere else
if countdown and countdown >= 1 then
love.graphics.print( math.floor(countdown), 20, 20 ) -- draw countdown on screen
end
Re: Help creating a timer
Posted: Wed Dec 09, 2015 2:35 pm
by arr5454
This is really helpful, thank you all very much.
The rumors I've heard of the love community being one of the best are true!