Class: HopLevel

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ HopLevel

Returns a new instance of HopLevel.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rubyhop.rb', line 99

def initialize window
  @window = window
  @music = Gosu::Song.new @window, get_my_file("music.mp3")
  @music.play true
  @player = Player.new self
  @hoops = 6.times.map { Hoop.new self }
  init_hoops!
  @font = Gosu::Font.new @window, Gosu::default_font_name, 20
  @movement = 3

  # Add callback holders
  @fail_callbacks = []
  @quit_callbacks = []
end

Instance Attribute Details

#movementObject

Returns the value of attribute movement.



98
99
100
# File 'lib/rubyhop.rb', line 98

def movement
  @movement
end

#scoreObject

Returns the value of attribute score.



98
99
100
# File 'lib/rubyhop.rb', line 98

def score
  @score
end

#windowObject

Returns the value of attribute window.



98
99
100
# File 'lib/rubyhop.rb', line 98

def window
  @window
end

Instance Method Details

#button_down(id) ⇒ Object



158
159
160
161
# File 'lib/rubyhop.rb', line 158

def button_down id
  quit!       if id == Gosu::KbEscape
  @player.hop if id == Gosu::KbSpace
end

#drawObject



178
179
180
181
182
# File 'lib/rubyhop.rb', line 178

def draw
  @player.draw
  @hoops.each &:draw
  @font.draw "Score: #{@score}", 700, 10, 1, 1.0, 1.0, Gosu::Color::RED
end

#fail!Object



129
130
131
# File 'lib/rubyhop.rb', line 129

def fail!
  @fail_callbacks.each { |c| c.call }
end

#init_hoops!Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rubyhop.rb', line 137

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

#on_fail(&block) ⇒ Object



114
115
116
# File 'lib/rubyhop.rb', line 114

def on_fail &block
  @fail_callbacks << block
end

#on_quit(&block) ⇒ Object



118
119
120
# File 'lib/rubyhop.rb', line 118

def on_quit &block
  @quit_callbacks << block
end

#quit!Object



133
134
135
# File 'lib/rubyhop.rb', line 133

def quit!
  @quit_callbacks.each { |c| c.call }
end

#reset_hoop!(hoop) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/rubyhop.rb', line 149

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!Object



122
123
124
125
126
127
# File 'lib/rubyhop.rb', line 122

def start!
  @score = 0
  @movement = 3
  @player.start!
  init_hoops!
end

#updateObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rubyhop.rb', line 163

def update
  @movement += 0.0025
  @player.update
  @hoops.each do |hoop|
    hoop.update
    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