O'Reilly logo

Raspberry Pi For Dummies by Mike Cook, Sean McManus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Making the Ball Move

The instructions that make the ball move also go inside the while loop, so they are all indented by four spaces, except where they need to be indented further. You need to insert them where indicated by a comment in the previous chunk of code.

We start by storing the current position of the ball in the variables oldballx and oldbally. Then we check for whether we need to bounce off the left, right, or top wall. If the ball’s X position is 1, for example, it can’t go any further left, so we change its horizontal direction to be to the right by giving ballxdir a value of 1. If the ball is in the top row, its Y direction is changed to downwards.

oldballx=ballx

oldbally=bally

if ballx==1:

ballxdir=1

if ballx==20:

ballxdir=-1

if bally==1:

ballydir=1

# uncomment below for cheat mode

# if bally==BATY-1:

# ballydir=-1

tip.eps We’ve included a cheat mode in the preceding code, which stops the ball from falling out of the bottom of the game. It’s useful for testing purposes. To use it, uncomment the two extra lines by removing the # (hash mark) at the start, and make sure they are still indented to line up with the statements above.

The next thing we need to do is check whether the ball has hit the bat, or fallen out of play. First we check whether the ball is on the row above the ball, which is BATY-1. If it is, we check whether it’s hit the bat. There are two ways ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required