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

Drawing the Bricks

Now we have a map, and we know how to make rectangles, we can draw the start screen showing the brickwork raspberry.

Our game map is 20 rows of 20 columns, and each position on the map can have a brick in it, have nothing in it, or have the ball or half of the bat in it (because the bat occupies two spaces).

To draw a brick on screen, we need to take a brick’s position on the map and use it to calculate the brick’s position on screen. Each position on the map is a square with sides of 20 pixels, so a brick’s real coordinate on the screen (in pixels) is 20 times its coordinate in the game (measured in our map rows and columns). For example, the brick that’s in the fifth row and the fourth column of our map is 100 pixels from the left of the window edge, and 80 pixels from the top. To calculate these pixel positions for Pygame, we’re going to create two functions called realx() and realy():

def realx(x):

x=x*20

return x

def realy(y):

y=y*20

return y

remember.eps Remember that all your functions need to go at the start of your program, so Python knows about them before you try to use them.

The next step is to create a function to draw a brick. In the last chapter, you learned how to pass arguments to a function. In this chapter, we extend the idea and pass several values at once. Our function drawbrick() receives the X map coordinate, Y map coordinate, and the color value ...

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