Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 16th, 2009, 07:03 PM
Allu_Akbar Allu_Akbar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 5 Allu_Akbar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 26 sec
Reputation Power: 0
Need help with a timer for my game

def seconds_minutes_string(seconds_elapsed)
return '0:00'


end

#It has to have total elapsed time starting at 0:00.

Reply With Quote
  #2  
Old November 17th, 2009, 09:16 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
Code:
def seconds_minutes_string(seconds_elapsed)
   return '0.00'
end
Has the same effect as
Code:
SMS = '0.00'

On top of that, it seems (from the absolutely minimal amount of information you provide) that you wish to manage time through string manipulation. Ruby has a very rich set of time functionalities wrapped up in the Time object. Consider the following:
Code:
t = Time.now  # => Tue Nov 17 10:11:18 -0500 2009
# wait a bit
u = Time.now  # => Tue Nov 17 10:11:54 -0500 2009
u - t  # => 35.672565
(u - t).to_s  # => "35.672565"
You can use TIme objects as 'first-class' objects so to speak; they can be added, subtracted, etc...
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #3  
Old November 17th, 2009, 11:17 AM
Allu_Akbar Allu_Akbar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 5 Allu_Akbar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 26 sec
Reputation Power: 0
Not exactly what i am looking for

Thanks for the help but i am trying to make a timer that starts at zero and until the game ends the timer will stop.

Here is a little more information.
I need positive integer of seconds, returns a digital clock string showing the minutes and seconds. Ex.78 seconds will return 1:18

I have no clue on how to do it.

Reply With Quote
  #4  
Old November 17th, 2009, 12:47 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
Code:
#!/usr/bin/env ruby

def show_timer(seconds)
    start = Time.now
    while seconds > 0
        elapsed  = (Time.now - start).to_i
        $stderr.puts "Elapsed: %02d:%02d" % [elapsed/60,elapsed%60]
        $stderr.puts "Remain : %02d:%02d" % [seconds/60,seconds%60]
        sleep 1
        seconds -= 1
    end
end

show_timer 20

That should give you a starting point on managing time. It's minimal in that it only handles minutes and seconds; it can double report the times due to truncation to Integer; it is blocking because it sleeps in between reports; and so on.
The output of the above is something like:
Code:
Elapsed: 00:00
Remain : 00:20
Elapsed: 00:01
Remain : 00:19
...
Elapsed: 00:18
Remain : 00:02
Elapsed: 00:19
Remain : 00:01

Reply With Quote
  #5  
Old November 17th, 2009, 04:48 PM
Allu_Akbar Allu_Akbar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 5 Allu_Akbar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 26 sec
Reputation Power: 0
Found a new way thanks for the jump start

def seconds_minutes_string(seconds_elapsed)
if seconds_elapsed >= 60
minutes = (seconds_elapsed / 60).to_s
return minutes
else
return seconds_elapsed.to_s
end
end

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Need help with a timer for my game

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap