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
113
# File 'lib/rubyhop.rb', line 99

def initialize window
  @window = window
  @window.caption = "Ruby Hop"
  @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



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

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

#drawObject



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

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

#fail!Object



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

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

#init_hoops!Object



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

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



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

def on_fail &block
  @fail_callbacks << block
end

#on_quit(&block) ⇒ Object



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

def on_quit &block
  @quit_callbacks << block
end

#quit!Object



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

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

#reset_hoop!(hoop) ⇒ Object



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

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



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

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

#updateObject



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

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