Skip to content

Commit

Permalink
Attempt to prevent ball stuck in paddle
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyoho committed Aug 15, 2011
1 parent 378dfba commit 42b5153
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion rong-elements/lib/rong/elements/ball.rb
Expand Up @@ -14,7 +14,8 @@ def initialize(start_x, start_y, angle)
super(start_x, start_y)
end

def reflect_x
def reflect_x(x_position=nil)
move_to(x_position, y) if x_position
self.x_direction *= -1
end

Expand Down
7 changes: 4 additions & 3 deletions rong-elements/lib/rong/elements/game.rb
Expand Up @@ -13,7 +13,7 @@ class Game
:score_callbacks, :hit_callbacks,
:bounce_callbacks, :win_callbacks

def initialize
def initialize(&block)
self.left_score = self.right_score = 0

self.score_callbacks = []
Expand All @@ -22,8 +22,9 @@ def initialize
self.win_callbacks = []

self.ball = Ball.new(WINDOW_CENTER_X, WINDOW_CENTER_Y, 0)
self.paddles = [Paddle.new("Player 1", LEFT_PADDLE_X, PADDLE_Y),
Paddle.new("Player 2", RIGHT_PADDLE_X, PADDLE_Y)]
self.paddles = [Paddle.new("Player 1", :left, LEFT_PADDLE_X, PADDLE_Y),
Paddle.new("Player 2", :right, RIGHT_PADDLE_X, PADDLE_Y)]
yield self
end

def update
Expand Down
16 changes: 10 additions & 6 deletions rong-elements/lib/rong/elements/paddle.rb
Expand Up @@ -6,10 +6,11 @@ class Paddle
SPEED = 10
include Entity

attr_accessor :name
attr_accessor :name, :side

def initialize(name, start_x, start_y)
def initialize(name, side, start_x, start_y)
self.name = name
self.side = side
super(start_x, start_y)
end

Expand All @@ -21,11 +22,14 @@ def move_down
self.y += SPEED
end

def left?
side == :left
end

def hit(ball)
ball.reflect_x
if ball.y < y
ball.reflect_y
end
ball_x = left? ? right : left
ball.reflect_x(ball_x)
ball.reflect_y if ball.y < y

strength = (y - ball.y).abs / (height / 2)
ball.angle = 45 * strength
Expand Down

0 comments on commit 42b5153

Please sign in to comment.