class HopLevel

Attributes

movement[RW]
score[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Level::new
# File lib/rubyhop/level.rb, line 49
def initialize
  super
  @music = Rubyhop.song "music.mp3"
  @music.play true
  @player = Player.new
  @hoops = 6.times.map { Hoop.new }
  init_hoops!
  @movement = 3
end

Public Instance Methods

button_down(id) click to toggle source
# File lib/rubyhop/level.rb, line 87
def button_down id
  quit!       if id == Gosu::KbEscape
  @player.hop if id == Gosu::KbSpace
end
draw() click to toggle source
# File lib/rubyhop/level.rb, line 111
def draw
  @player.draw
  @hoops.each(&:draw)
  draw_score
end
draw_score() click to toggle source
# File lib/rubyhop/level.rb, line 117
def draw_score
  Rubyhop.score_font.draw "Score: #{@score}", 700, 10, 1, 1.0, 1.0, Gosu::Color::RED
end
init_hoops!() click to toggle source
# File lib/rubyhop/level.rb, line 66
def init_hoops!
  @hoops.each do |hoop|
    hoop.y = 325
  end
  hoop_start = 400
  @hoops.each do |hoop|
    reset_hoop! hoop
    hoop_start += 200
    hoop.x = hoop_start
  end
end
reset_hoop!(hoop) click to toggle source
# File lib/rubyhop/level.rb, line 78
def reset_hoop! hoop
  idx = @hoops.index hoop
  prev = @hoops[idx - 1]
  new_y = ((prev.y-150..prev.y+125).to_a & (150..500).to_a).sample
  hoop.x += 1200
  hoop.y = new_y
  hoop.active = true
end
start!() click to toggle source
# File lib/rubyhop/level.rb, line 59
def start!
  @score = 0
  @movement = 3
  @player.start!
  init_hoops!
end
update() click to toggle source
# File lib/rubyhop/level.rb, line 92
def update
  @movement += 0.0025
  @player.update
  if @player.offscreen?
    # kick out to loading screen to try again?
    fail!
  end
  @hoops.each do |hoop|
    hoop.update @movement
    reset_hoop!(hoop) if hoop.x < -200
    @player.die! if hoop.miss @player
    # increase score and flag as inactive
    if hoop.active && @player.alive && hoop.x < @player.x
      @score += 1
      hoop.active = false
    end
  end
end