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

No credit card required

Positioning the Bat

The game uses the variable batx to refer to the X coordinate of the bat in the game map. The player can’t move the bat up and down, so we’ve used a constant called BATY to represent the bat’s row. The variable batx is assigned a random value at the start, so the bat starts in a random position. When setting up the bat, we also set the player’s score to zero:

#initialize bat and score

batx=random.randint(1,19)

BATY=20

drawbat(batx)

score=0

The function drawbat() does exactly what it says on the function name. It takes the bat’s X position as an argument and draws the bat there. Like the bricks, we’ve compiled the bat from several rectangles so it looks a bit more interesting. Here’s the routine to draw the bat:

def drawbat(x):

pygame.draw.rect (gameSurface,LIGHTPURPLE,(realx(x),Ærealy(BATY),40,4))

pygame.draw.rect (gameSurface,PURPLE,(realx(x),realy Æ(BATY)+4,40,6))