Class: Gtk::Pong::Paddle

Inherits:
CenteredRect show all
Defined in:
lib/games_paradise/pong/gtk/cairo_pong.rb

Constant Summary

Constants inherited from CenteredItem

CenteredItem::NAMESPACE

Instance Attribute Summary

Attributes inherited from CenteredItem

#height, #width, #x, #y

Instance Method Summary collapse

Methods inherited from CenteredRect

#draw

Methods inherited from CenteredItem

#max_x, #max_y, #min_x, #min_y

Constructor Details

#initialize(field, x, y) ⇒ Paddle

initialize



110
111
112
113
# File 'lib/games_paradise/pong/gtk/cairo_pong.rb', line 110

def initialize(field, x, y)
  super(x, y, 0.05, 0.3)
  @field = field
end

Instance Method Details

#ball_hit?(ball) ⇒ Boolean

ball_hit?

Returns:

  • (Boolean)


125
126
127
# File 'lib/games_paradise/pong/gtk/cairo_pong.rb', line 125

def ball_hit?(ball)
  ball.y > min_y and ball.y < max_y
end

#update(ball) ⇒ Object

update



115
116
117
118
119
120
121
122
# File 'lib/games_paradise/pong/gtk/cairo_pong.rb', line 115

def update(ball)
  # is the ball coming towards us?
  if (ball.x < @x and ball.dx > 0) or
      (ball.x > @x and ball.dx < 0)
    # move to intercept it
    @y = ball.y
  end
end

#update_ball(ball) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/games_paradise/pong/gtk/cairo_pong.rb', line 129

def update_ball(ball)
  if ball_hit?(ball)
    if ball.min_x < @x and ball.max_x > min_x # hit our left side
      ball.x -= (ball.max_x - min_x)
      ball.dx = -ball.dx
    elsif ball.max_x > @x and ball.min_x < max_x # hit our right side
      ball.x += (max_x - ball.min_x)
      ball.dx = -ball.dx
    end
  end
end