Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ball meets Paddle.

Similar presentations


Presentation on theme: "Ball meets Paddle."— Presentation transcript:

1 Ball meets Paddle

2 Telling the ball when it has hit the paddle
If we don’t do anything, the ball will go right through the paddle (oh no!) We need to tell the paddle when it has been hit. We also need to tell the ball when it has hit a wall. We will make code easier to manage by chunking it into smaller pieces and keeping our functions simple.

3 Change the Ball class __init__ function (1)
Pass the paddle object into our ball __init__ function Change the parameters to: def __init__(self, canvas, paddle, color): And Add the following (just below the self.canvas = canvas): self.paddle = paddle

4 Change the Ball class __init__ function (2)
We also need to pass the paddle object into the ball object At the bottom of the program, just before the main loop, change  ball = Ball(canvas, paddle, ‘red’)

5 hit_paddle Function (1)
Adding a code to see of the ball has struck the paddle is more complicated than seeing if the ball has hit a wall, so we will write a new function called hit_paddle to the draw function of the Ball class. First, let’s update the draw function to allow for our new hit_paddle, which we are about to create: Add this just below the (if pos[3] >= seld.canvas_height…) lines: if self.hit_paddle(pos) == True: self.y = -3

6 hit_paddle function (2)
def hit_paddle(self, pos): paddle_pos = self.canvas.coords(self.paddle.id) if pos[2] >= paddle_pos[0] and pos[0] < paddle_pos[2]: if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]: return True return False


Download ppt "Ball meets Paddle."

Similar presentations


Ads by Google