Class: GamesAndRpgParadise::Gtk::Pong::Ball

Inherits:
CenteredCircle show all
Defined in:
lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb

Constant Summary

Constants inherited from CenteredItem

CenteredItem::NAMESPACE

Instance Attribute Summary collapse

Attributes inherited from CenteredItem

#height, #width, #x, #y

Instance Method Summary collapse

Methods inherited from CenteredCircle

#draw

Methods inherited from CenteredItem

#max_x, #max_y, #min_x, #min_y

Constructor Details

#initialize(dx = 0.02, dy = 0.02) ⇒ Ball

initialize



89
90
91
92
93
# File 'lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb', line 89

def initialize(dx=0.02, dy=0.02)
  super(0.8, 0.5, 0.04, 0.04)
  @dx = dx
  @dy = dy
end

Instance Attribute Details

#dxObject

Returns the value of attribute dx.



86
87
88
# File 'lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb', line 86

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



86
87
88
# File 'lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb', line 86

def dy
  @dy
end

Instance Method Details

#updateObject

update



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb', line 96

def update
  @x += @dx
  @y += @dy

  # ball bouncing
  if max_y > 1
    @y = 1 - (max_y - 1)
    @dy *= -1
  elsif min_y < 0
    @y -= min_y
    @dy *= -1
  end
    
  if max_x > 1
    @x = 1 - (max_x - 1)
    @dx *= -1
  elsif min_x < 0
    @x -= min_x
    @dx *= -1
  end
end