Slide 7
Slide 7 text
78 def move_ball
79 # If ball exits right, +1 for the left player
80 if (@ball_view.center.x > self.view.frame.size.width)
81 increment_left_score
82 @direction_x *= -1
83 self.reset_ball
84 end
85
86 # If ball exits left, +1 for the right player
87 if (@ball_view.center.x < 0)
88 increment_right_score
89 @direction_x *= -1
90 self.reset_ball
91 end
92
93 # If ball hits top or bottom wall, bounce y in the opposite direction. x-direction remains unchanged.
94 if ((@ball_view.center.y + @ball_view.frame.size.height >
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! self.view.frame.size.height) || @ball_view.frame.origin.y < 0)
95 @direction_y *= -1
96 end
97
98 # If the ball didn't hit anything, keep on moving...
99 @ball_view.center = [@ball_view.center.x + @direction_x, @ball_view.center.y + @direction_y]
100
101 # If ball collides with a paddle, change direction.
102 check_paddle_collision
103
104 end