#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    5
    Rep 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    5
    Rep 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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    5
    Rep 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

IMN logo majestic logo threadwatch logo seochat tools logo