class Player

Attributes

alive[RW]
x[RW]
y[RW]

Public Class Methods

new() click to toggle source
# File lib/rubyhop/player.rb, line 3
def initialize
  # position
  start!
  @gravity  = -0.25
  @hop      = 7.5
  # sounds
  @sound    = Rubyhop.sound "hop.mp3"
  @gameover = Rubyhop.sound "gameover.mp3"
  # images
  @rise = Rubyhop.image "rubyguy-rise.png"
  @fall = Rubyhop.image "rubyguy-fall.png"
  @dead = Rubyhop.image "rubyguy-dead.png"
end

Public Instance Methods

die!() click to toggle source
# File lib/rubyhop/player.rb, line 28
def die!
  if @alive
    # Set velocity to one last hop
    @velocity = 5.0
    @gameover.play
    @alive = false
  end
end
draw() click to toggle source
# File lib/rubyhop/player.rb, line 46
def draw
  image.draw @x - 32, @y - 32, 1000 - @x
end
hop() click to toggle source
# File lib/rubyhop/player.rb, line 16
def hop
  if @alive
    @sound.play
    @velocity += @hop
  end
end
image() click to toggle source
# File lib/rubyhop/player.rb, line 49
def image
  if @alive
    if @velocity >= 0
      @rise
    else
      @fall
    end
  else
    @dead
  end
end
offscreen?() click to toggle source
# File lib/rubyhop/player.rb, line 43
def offscreen?
  @y > 1000
end
start!() click to toggle source
# File lib/rubyhop/player.rb, line 22
def start!
  @x = Rubyhop.width / 3
  @y = Rubyhop.height / 2
  @velocity = 0.0
  @alive = true
end
update() click to toggle source
# File lib/rubyhop/player.rb, line 36
def update
  @velocity += @gravity
  @y -= @velocity
  if @alive && (@y < 32 || @y > Rubyhop.height - 32)
    die!
  end
end