Making the Bat Move
Now we have all our functions in place, our starting screen drawn, and all our variables set up. It’s time to make the game loop, which repeats until the game is won, lost, or abandoned by the player closing the window.
Each run through the loop checks for keypresses, moves the bat if required, moves the ball (including changing its direction if it should bounce), knocks out bricks if necessary, updates the screen, checks if the player has won, and then pauses briefly to match your desired game speed.
Here’s the shell of the main loop, with all the bat controls included, but with a gap left for the ball movement instructions to be added later:
#main game loop
for event in pygame.event.get():
if event.type == QUIT:
if batx==0 or batx==20:
# ball movement goes here
while True sets the loop up to run forever and is a construct that’s often used for loops you want to repeat indefinitely. The first thing that happens inside the
while loop is that there is a
for loop that works through Pygame’s list of events, which are things that happened like key presses or clicks on the window’s Close button. Each time around the
for loop, the program checks the next event in the list.
First, we ...