Class: GamesAndRpgParadise::Gtk::Pong::Paddle

Inherits:
CenteredRect 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

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



123
124
125
126
# File 'lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb', line 123

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)


138
139
140
# File 'lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb', line 138

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

#update(ball) ⇒ Object

update



128
129
130
131
132
133
134
135
# File 'lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb', line 128

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



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/games_and_rpg_paradise/games/pong/gtk/cairo_pong.rb', line 142

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