Class: Player

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayer

Returns a new instance of Player.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rubyhop/player.rb', line 3

def initialize
  # position
  start!
  @gravity  = -0.25
  @hop      = 7.5
  # sounds
  @sound    = Rubyhop.sound "hop.mp3"
  @gameover = Rubyhop.sound "gameover.mp3"
  # images
  @rise = Rubyhop.image "rubyguy-rise.png"
  @fall = Rubyhop.image "rubyguy-fall.png"
  @dead = Rubyhop.image "rubyguy-dead.png"
end

Instance Attribute Details

#aliveObject

Returns the value of attribute alive.



2
3
4
# File 'lib/rubyhop/player.rb', line 2

def alive
  @alive
end

#xObject

Returns the value of attribute x.



2
3
4
# File 'lib/rubyhop/player.rb', line 2

def x
  @x
end

#yObject

Returns the value of attribute y.



2
3
4
# File 'lib/rubyhop/player.rb', line 2

def y
  @y
end

Instance Method Details

#die!Object



28
29
30
31
32
33
34
35
# File 'lib/rubyhop/player.rb', line 28

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

#drawObject



46
47
48
# File 'lib/rubyhop/player.rb', line 46

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

#hopObject



16
17
18
19
20
21
# File 'lib/rubyhop/player.rb', line 16

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

#imageObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubyhop/player.rb', line 49

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

#offscreen?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rubyhop/player.rb', line 43

def offscreen?
  @y > 1000
end

#start!Object



22
23
24
25
26
27
# File 'lib/rubyhop/player.rb', line 22

def start!
  @x = Rubyhop.width / 3
  @y = Rubyhop.height / 2
  @velocity = 0.0
  @alive = true
end

#updateObject



36
37
38
39
40
41
42
# File 'lib/rubyhop/player.rb', line 36

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