Class: RubyhopGame

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

Constant Summary collapse

VERSION =
"1.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(width = 800, height = 600, fullscreen = false) ⇒ RubyhopGame

Returns a new instance of RubyhopGame.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rubyhop.rb', line 96

def initialize width=800, height=600, fullscreen=false
  super
  self.caption = "Ruby Hop"
  @music = Gosu::Song.new self, get_my_file("music.mp3")
  @music.play true
  @background = Gosu::Image.new self, get_my_file("background.png")
  @player = Player.new self
  @hoops = 6.times.map { Hoop.new self }
  init_hoops!
  @score = 0
end

Instance Method Details

#button_down(id) ⇒ Object



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

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

#drawObject



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

def draw
  @background.draw 0, 0, 0
  @player.draw
  @hoops.each &:draw
end

#init_hoops!Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rubyhop.rb', line 108

def init_hoops!
  @hoops.each do |hoop|
    hoop.y = 325
  end
  hoop_start = 600
  @hoops.each do |hoop|
    reset_hoop! hoop
    hoop_start += 200
    hoop.x = hoop_start
  end
end

#reset_hoop!(hoop) ⇒ Object



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

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

#updateObject



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

def update
  @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
      self.caption = "Ruby Hop: #{@score}"
    end
  end
end