Class: Gtk::Pong::Ball

Inherits:
CenteredCircle show all
Defined in:
lib/games_paradise/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



76
77
78
79
80
# File 'lib/games_paradise/pong/gtk/cairo_pong.rb', line 76

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.



73
74
75
# File 'lib/games_paradise/pong/gtk/cairo_pong.rb', line 73

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



73
74
75
# File 'lib/games_paradise/pong/gtk/cairo_pong.rb', line 73

def dy
  @dy
end

Instance Method Details

#updateObject

update



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/games_paradise/pong/gtk/cairo_pong.rb', line 83

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