Help creating a timer

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
arr5454
Prole
Posts: 5
Joined: Tue Oct 20, 2015 9:09 pm

Help creating a timer

Post 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
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.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Help creating a timer

Post 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

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
kbmonkey
Party member
Posts: 139
Joined: Tue Sep 01, 2015 12:19 pm
Location: Sydney
Contact:

Re: Help creating a timer

Post 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

Code: Select all

pauseTimeout = 4
Attachments
pong.zip.love
(9.51 KiB) Downloaded 84 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Help creating a timer

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
arr5454
Prole
Posts: 5
Joined: Tue Oct 20, 2015 9:09 pm

Re: Help creating a timer

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 9 guests