Class: Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblr-game/timer.rb

Overview

TIMER CLASS. This class handles the timing aspect of the game. The initialize function accepts parameters that pass in a font, the timer location, and the amount of time the game will run for. The update function will increment the frame counter by 1 every frame until it gets to 60. It then resets to zero. The draw function renders the Timer on the game screen at the location established in initialize. The return_time function returns the time left in the game.

Instance Method Summary collapse

Constructor Details

#initialize(fontParam, pX, pY, time) ⇒ Timer

initialize function of the timer class this will create a Timer object with parameters fontParam = a Gosu font object pX = the x coordinate of where the font will display pY = the y coordinate of where the font will display the timer object also maintains a frame count and a count of the total time



16
17
18
19
20
21
22
# File 'lib/tumblr-game/timer.rb', line 16

def initialize(fontParam, pX, pY, time)
  @font = fontParam
  @frameCounter = 0
  @totalTime = time
  @posX = pX
  @posY = pY
end

Instance Method Details

#drawObject

the draw function of the timer utilizes the draw function of the font object that was passed in it will draw the font object with text being the time remaining at location (posX, posY) in the window



40
41
42
# File 'lib/tumblr-game/timer.rb', line 40

def draw
  @font.draw("Time: #{@totalTime}", @posX - 100, @posY, 0, 2, 2, 0xFFFFFFFF)
end

#return_timeObject

the return time function of this class will return the time that is remaining in the game



45
46
47
# File 'lib/tumblr-game/timer.rb', line 45

def return_time
  return @totalTime
end

#updateObject

the update function of the timer class does most of the timing job the frame counter variable is updated every 16.666666 ms (Gosu’s default refresh rate) this leads to about 60 frames per second, so every 60 frames the total time count will decrease by 1



27
28
29
30
31
32
33
34
35
36
# File 'lib/tumblr-game/timer.rb', line 27

def update
  if @frameCounter < 60 then
    @frameCounter += 1
  else
    @frameCounter = 0
    if @totalTime != 0 then
      @totalTime -= 1
    end
  end
end