time limit
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Citizen
- Posts: 54
- Joined: Thu Oct 24, 2013 1:29 am
time limit
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
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
Re: time limit
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
Check out my blog on gamedev
-
- Citizen
- Posts: 54
- Joined: Thu Oct 24, 2013 1:29 am
Re: time limit
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
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
Re: time limit
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.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: time limit
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
-
- Citizen
- Posts: 54
- Joined: Thu Oct 24, 2013 1:29 am
Re: time limit
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....
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....
-
- Citizen
- Posts: 54
- Joined: Thu Oct 24, 2013 1:29 am
Re: time limit
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..
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
Re: time limit
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:
You put the above in your love.load and it would work like this:
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
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
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
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: time limit
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:
is strictly equivalent to:
Therefore:
This is fairly well discussed in Chapter 16 of PiL.
If a is a table, and f a function in this table, then, the following:
Code: Select all
a.f(a,...)
Code: Select all
a:f(...)
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
-
- Citizen
- Posts: 54
- Joined: Thu Oct 24, 2013 1:29 am
Re: time limit
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?
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?
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest