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
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
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
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.
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....
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
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:
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:
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
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:
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.
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
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?