Class: Player

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Player

Returns a new instance of Player.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubyhop.rb', line 9

def initialize window
  @window = window
  @alive  = true
  # position
  @x = window.width/2
  @y = window.height/2
  @velocity = 0.0
  @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

#aliveObject

Returns the value of attribute alive.



8
9
10
# File 'lib/rubyhop.rb', line 8

def alive
  @alive
end

#xObject

Returns the value of attribute x.



8
9
10
# File 'lib/rubyhop.rb', line 8

def x
  @x
end

#yObject

Returns the value of attribute y.



8
9
10
# File 'lib/rubyhop.rb', line 8

def y
  @y
end

Instance Method Details

#die!Object



32
33
34
35
36
37
38
39
# File 'lib/rubyhop.rb', line 32

def die!
  if @alive
    # Set velocity to one last hop
    @velocity = 5.0
    @gameover.play
    @alive = false
  end
end

#drawObject



51
52
53
# File 'lib/rubyhop.rb', line 51

def draw
  image.draw @x - 32, @y - 32, 1000 - @x
end

#hopObject



26
27
28
29
30
31
# File 'lib/rubyhop.rb', line 26

def hop
  if @alive
    @sound.play
    @velocity += @hop
  end
end

#imageObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubyhop.rb', line 54

def image
  if @alive
    if @velocity >= 0
      @rise
    else
      @fall
    end
  else
    @dead
  end
end

#updateObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubyhop.rb', line 40

def update
  @velocity += @gravity
  @y -= @velocity
  if @alive && (@y < 32 || @y > @window.height - 32)

  end
  if @y > 5000
    # kick out to loading screen to try again?
    @window.close
  end
end