240 Chapter 10
There’s just one more condition to check. Remember that the
ball and paddle are virtual; that is, they don’t exist in the real world,
don’t have actual edges, and don’t interact like real game pieces do.
We could move our paddle through the ball even when it’s bouncing
back up from the bottom edge. We don’t want to award points when
the player has clearly missed the ball, so before awarding a point,
let’s check to make sure the ball is headed down, in addition to being
within the vertical and horizontal range of the paddle. We can tell
the ball is headed down the screen if the ball’s speed in the y direc-
tion (speedy) is greater than zero. When speedy > 0 , the ball is moving
down the screen in the positive y direction.
We now have the conditions we need to create the two if state-
ments that will check whether the ball hit the paddle:
if picy + pich >= paddley and picy + pich <= paddley + paddleh \
and speedy > 0:
if picx + picw/2 >= paddlex and picx + picw/2 <= paddlex + \
paddlew:
First, we check whether the ball is within the vertical range
to be able to touch the paddle and whether it’s heading downward
instead of upward. Then, we check whether the ball is within the
horizontal range to be able to touch the paddle.
In both of these if statements, the compound conditions made
the statement too long to fit on our screen. The backslash charac-
ter, \, allows us to continue a long line of code by wrapping around
to the next line. You can choose to type a long line of code all on a
single line, or you can wrap the code to fit the screen by ending the
first line with a backslash \, pressing enter, and continuing the
code on the next line. We have some long lines of logic in the games
in this chapter, so you’ll see the backslash in several of the code
listings. Just remember that Python will read any lines separated
by a backslash as a single line of code.
a dding a Point
Let’s build the logic to bounce the ball and award a point. To com-
plete our paddle logic, we add two more lines right after the two if
statements:
if picy + pich >= paddley and picy + pich <= paddley + paddleh \
and speedy > 0: