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
Help creating a timer
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Help creating a timer
- Attachments
-
- PongFinished.love
- (8.77 KiB) Downloaded 99 times
Last edited by arr5454 on Tue Dec 08, 2015 8:10 pm, edited 1 time in total.
Re: Help creating a timer
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
This is a very nice looking pong clone!
Similar to what Beelz said, I would functionalize the pause code:
Then just call updatePauseTimeout(dt) and drawPauseTimeout() from love.update and love.draw.
When the ball is lost, simply set
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
When the ball is lost, simply set
Code: Select all
pauseTimeout = 4
- Attachments
-
- pong.zip.love
- (9.51 KiB) Downloaded 84 times
Re: Help creating a timer
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.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
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
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!
The rumors I've heard of the love community being one of the best are true!
Who is online
Users browsing this forum: Bing [Bot], Semrush [Bot] and 9 guests