Class: Player

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb

Overview

#

Constant Summary collapse

GRAVITY =
#

GRAVITY

#
100
JUMP_TIME =
#

JUMP_TIME

#
0.3
JUMP_POWER =
#

JUMP_POWER

#
-250
INCREASE_GRAVITY =
#

INCREASE_GRAVITY

#
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Player

#

initialize

#


57
58
59
60
61
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 57

def initialize(window)
  @player_image = load_image(window)
  @window = window
  reset
end

Instance Attribute Details

#deadObject

Returns the value of attribute dead.



31
32
33
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 31

def dead
  @dead
end

#xObject

Returns the value of attribute x.



29
30
31
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 29

def x
  @x
end

#yObject

Returns the value of attribute y.



30
31
32
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 30

def y
  @y
end

Instance Method Details

#bird_fallObject

#

bird_fall

Bird fall to the ground

#


170
171
172
173
174
175
176
177
178
179
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 170

def bird_fall
  if @y < @window.ground_y - @player_image.height/2
    @gravity += INCREASE_GRAVITY
    @a += 25
    @a = [@a,90].min
    @y += @gravity * @window.delta
  else
    @y = @window.ground_y - @player_image.height/2
  end
end

#bird_flyObject

#

bird_fly

Bird fly when all is done

#


186
187
188
189
190
191
192
193
194
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 186

def bird_fly
  move
  margin = 20
  @y += @velocityY * @window.delta if @y > @velocityY * @window.delta + margin
  @gravity += INCREASE_GRAVITY
  @a += 0.5
  @a = [@a,90].min
  @y += @gravity * @window.delta
end

#collision?(other) ⇒ Boolean

Does the bird touch anything

Returns:

  • (Boolean)


128
129
130
131
132
133
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 128

def collision?(other)
  @y + @player_image.height/2 > other.y &&
  @y - @player_image.height/2 < other.y + other.height &&
  @x + @player_image.width/2  > other.x &&
  @x - @player_image.width/2  < other.x + other.width
end

#dead_if_touch_groundObject

The bird is dead if he touch the ground



118
119
120
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 118

def dead_if_touch_ground
  @dead = true if @y >= @window.ground_y - @player_image.height/2
end

#drawObject

#

draw

Draw player image.

#


91
92
93
94
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 91

def draw
  @player_image = load_image(@window)
  @player_image.draw_rot(@x, @y, 1, @a)
end

#jump_startObject

Set @jump_start with Gosu seconds



143
144
145
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 143

def jump_start
  @jump_start = Gosu::milliseconds / 1000.0
end

#killed?Boolean

Is bird is dead ?

Returns:

  • (Boolean)


113
114
115
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 113

def killed?
  (@y == @window.ground_y - @player_image.height/2) ? true : false
end

#load_image(window) ⇒ Object

#

load_image

#


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 36

def load_image(window)
  image = ''
  images = [
    GamesAndRpgParadise.image_directory?+'flappy_bird/flappy-1.png',
    GamesAndRpgParadise.image_directory?+'flappy_bird/flappy-2.png',
    GamesAndRpgParadise.image_directory?+'flappy_bird/flappy-3.png'
  ]
  sec = (Gosu::milliseconds / 1000).to_s.split(//).last
  if ['0', '3' , '6', '9'].include? sec
    image = images[0]
  elsif ['1', '4', '7'].include? sec
    image = images[1]
  else
    image = images[2]
  end
  @player_image = Gosu::Image.new(image)
end

#moveObject

Move bird sprite when user hit space



104
105
106
107
108
109
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 104

def move
  @space = user_hit_space?
  jump_start if @space && !@space_before
  update_jump_params if @jump_start > 0
  @space_before = @space
end

#resetObject

#

reset

Reset position of the bird

#


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 68

def reset
  @x = @window.width / 4
  @y = @window.height / 2
  @a = 0
  @velocityY = 0
  @gravity = 50
  @delta = 0.25

  @space = false
  @space_before = false

  @jump = false
  @jump_max = 0.3
  @jump_start = 0

  @dead = false
end

#through_wall?(other) ⇒ Boolean

Is the bird hit a wall ?

Returns:

  • (Boolean)


123
124
125
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 123

def through_wall?(other)
  @x > other.x + (other.width/2)
end

#updateObject

Update position, acceleration and gravity of the bird



97
98
99
100
101
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 97

def update
  dead_if_touch_ground
  bird_fall if @dead
  bird_fly if !@dead
end

#update_jump_paramsObject

#

update_jump_params

Stop jump if its duration > JUMP_TIME. Else update acceleration used during jump activation

#


153
154
155
156
157
158
159
160
161
162
163
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 153

def update_jump_params
  @gravity = GRAVITY
  if ((Gosu.milliseconds / 1000.0) - @jump_start) > JUMP_TIME
    @jump_start = 0
  else
    @velocityY = JUMP_POWER
    @a = -45
    @a = -22 if ((Gosu.milliseconds / 1000.0) - @jump_start) > JUMP_TIME / 3
    @a = 0 if ((Gosu.milliseconds / 1000.0) - @jump_start) > (JUMP_TIME*2 / 3)
  end
end

#user_hit_space?Boolean

Does the use hit space key

Returns:

  • (Boolean)


137
138
139
140
# File 'lib/games_and_rpg_paradise/games/flappy_bird/gosu/player.rb', line 137

def user_hit_space?
  return true if @window.button_down?(Gosu::KbSpace)
  return false if !@window.button_down?(Gosu::KbSpace)
end