time limit

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.
elsalvador
Citizen
Posts: 54
Joined: Thu Oct 24, 2013 1:29 am

time limit

Post by elsalvador »

okay can someone
explain to me how time works?
I want to use time limit to a game so i want to start from
1:00 min til 0:00
i try with update(dt)
but it goes really fast :(
read the wiki but to many stuff cant get it ?
can someone be so nice and give me the simple code?
from 1:00 and as soon hits 0:00 to say FAIL! as print
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: time limit

Post by micha »

You need a variable that records the time left (in seconds)

Code: Select all

function love.load()
  timeLeft = 60
end

function love.draw()
  if timeLeft >= 0 then
    love.graphics.print(timeLeft,100,100)
  else
    love.graphics.print('Fail',100,100)
  end
end

function love.update(dt)
  timeLeft = timeLeft - dt
end
elsalvador
Citizen
Posts: 54
Joined: Thu Oct 24, 2013 1:29 am

Re: time limit

Post by elsalvador »

oH i see But is there a way to make it like the real time going down with out all them number to the right???
i did tried like that but to many number to the right side? i will need to display it on the screen this could work behind scenes but what i need its to be able to display like them real games with time limits.. the DT is a problem for me its to fast .. for 1:00 going down to 0:00 i will need it to go like real time mins( dt )gives me only like millisecs... to fast
Teraku
Prole
Posts: 27
Joined: Mon Jun 24, 2013 10:01 am
Location: The Netherlands

Re: time limit

Post by Teraku »

Try this code (Untested):

Code: Select all

function love.load()
  timeLeft = 60
end

function love.draw()
love.graphics.print(round(timeLeft), 0, 0)
end

function love.update(dt)
timeLeft = timeLeft - dt
end

function round(num)
    return math.floor(num+0.5)
end
Last edited by Teraku on Sun Nov 17, 2013 9:25 pm, edited 1 time in total.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: time limit

Post by DaedalusYoung »

And to round it down to integers, you use something like math.floor().

Code: Select all

function love.draw()
    love.graphics.print(math.floor(timeLeft), 32, 32)
end
elsalvador
Citizen
Posts: 54
Joined: Thu Oct 24, 2013 1:29 am

Re: time limit

Post by elsalvador »

Thank you both this is what i was looking for interesting that under wiki you don't have something like this
for beginners because the DT under update its difficult to use for cases like this..

Hope someone could add ' time world clock ' in a simple way using the update(dt)<-----
but for now this is perfect thank you both!

but if there is another easier way hope someone could add it here....
elsalvador
Citizen
Posts: 54
Joined: Thu Oct 24, 2013 1:29 am

Re: time limit

Post by elsalvador »

okay from what you gave me I did this
IDK i feel the code its messy can someone share a shorter way to do this?
I feel to many variables i used.. :(

Code: Select all

function love.load()
    timeLeft = 10
	mins = 2
	stop = true
	p = false
end

function love.draw()
	love.graphics.setColor(255,255,255,255)
	love.graphics.print(mins..' :'..round(timeLeft), 50, 50)
	if p== true then
		love.graphics.print('YOU LOST', 100, 100)
	end
end

function love.update(dt)
	if stop== true then
		timeLeft = timeLeft - dt
	end	
	
	if timeLeft <= 0 then
		timeLeft = 10
		mins =mins-1
	end	
	if mins== 0 then
		mins =2
		stop = false
		p=true
	end
end

function round(num)	
	return math.floor(num+0.5)	
end
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: time limit

Post by Plu »

If you don't mind upping the complexity a bit, you can use a table to handle most of this stuff and keep it fairly clean. I'm going to assume you understand the basic idea of tables and can fill in the gaps because I can't test the code from here, but something along the lines of this:

Code: Select all

clock = {}
clock.time = 120 -- in seconds
clock.x, clock.y = 50, 50 -- where you want the clock to be
clock.update = function( self, dt ) if self.time > 0 then self.time = self.time - dt end end
clock.draw = function( self ) love.graphics.print(math.floor( self.time / 60 ) ..' :'..round(self.time % 60 ), self.x, self.y )
-- the above uses the clock.time to calculate the number of minutes and seconds on the fly whenever they are printed
clock.timeUp = function( self ) return self.time <= 0 end
You put the above in your love.load and it would work like this:

Code: Select all

function love.update( dt )
  clock.update( clock, dt )
end

function love.draw()
  clock.draw( clock )
  if clock.timeUp( clock ) then
    -- print "you lose" or whatever you like here
  end
end
It could be a lot cleaner but it'll only get more complicated for a beginning programmer, so once you understand how this work, feel free to come in and ask for more improvements :)
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: time limit

Post by Roland_Yonaba »

Just to add that if you keep the same function definition (as proposed by the Great Plu), you can have a shorter syntax, OOP style, using colon for method call.

If a is a table, and f a function in this table, then, the following:

Code: Select all

a.f(a,...)
is strictly equivalent to:

Code: Select all

a:f(...)
Therefore:

Code: Select all

    function love.update( dt )
      clock:update( dt )
    end

    function love.draw()
      clock:draw()
      if clock:timeUp() then
        -- print "you lose" or whatever you like here
      end
    end
This is fairly well discussed in Chapter 16 of PiL.
elsalvador
Citizen
Posts: 54
Joined: Thu Oct 24, 2013 1:29 am

Re: time limit

Post by elsalvador »

Guys you all are great i will learn a lot from you now if i make the table ( i know basic) when you say function does it means i create a new function somewhere else and just add it to the table?
ill get this examples you guys wrote and test it til i get it :cool:

but for now thank you so much ....

But at the end the way i did it before is it valid? will it still work?
i know its messy but just wondering?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest