Class: Player
- Inherits:
-
Object
- Object
- Player
- Defined in:
- lib/rubyhop.rb
Instance Attribute Summary collapse
-
#alive ⇒ Object
Returns the value of attribute alive.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #die! ⇒ Object
- #draw ⇒ Object
- #hop ⇒ Object
- #image ⇒ Object
-
#initialize(level) ⇒ Player
constructor
A new instance of Player.
- #start! ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(level) ⇒ Player
Returns a new instance of Player.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rubyhop.rb', line 9 def initialize level @level = level @window = @level.window # position start! @gravity = -0.25 @hop = 7.5 # sounds @sound = Gosu::Sample.new @window, get_my_file("hop.mp3") @gameover = Gosu::Sample.new @window, get_my_file("gameover.mp3") # images @rise = Gosu::Image.new @window, get_my_file("rubyguy-rise.png") @fall = Gosu::Image.new @window, get_my_file("rubyguy-fall.png") @dead = Gosu::Image.new @window, get_my_file("rubyguy-dead.png") end |
Instance Attribute Details
#alive ⇒ Object
Returns the value of attribute alive.
8 9 10 |
# File 'lib/rubyhop.rb', line 8 def alive @alive end |
#x ⇒ Object
Returns the value of attribute x.
8 9 10 |
# File 'lib/rubyhop.rb', line 8 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
8 9 10 |
# File 'lib/rubyhop.rb', line 8 def y @y end |
Instance Method Details
#die! ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/rubyhop.rb', line 36 def die! if @alive # Set velocity to one last hop @velocity = 5.0 @gameover.play @alive = false end end |
#draw ⇒ Object
55 56 57 |
# File 'lib/rubyhop.rb', line 55 def draw image.draw @x - 32, @y - 32, 1000 - @x end |
#hop ⇒ Object
24 25 26 27 28 29 |
# File 'lib/rubyhop.rb', line 24 def hop if @alive @sound.play @velocity += @hop end end |
#image ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rubyhop.rb', line 58 def image if @alive if @velocity >= 0 @rise else @fall end else @dead end end |
#start! ⇒ Object
30 31 32 33 34 35 |
# File 'lib/rubyhop.rb', line 30 def start! @x = @window.width/3 @y = @window.height/2 @velocity = 0.0 @alive = true end |
#update ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rubyhop.rb', line 44 def update @velocity += @gravity @y -= @velocity if @alive && (@y < 32 || @y > @window.height - 32) die! end if @y > 1000 # kick out to loading screen to try again? @level.fail! end end |