#Bounce bullet when it reaches corners.
def bullet_bounce(rect):
bottom_corner = False
if bottom_corner == False:
rect.y +=5
if rect.bottom == 400:
bottom_corner = True
if bottom_corner == True:
rect.y -=5
if rect.top == 0:
bottom_corner = False
While working with pygame to create Pong I want to make a rectangle bounce every time it reaches the corner of the screen. I came up with this code and it works as intented when I initialize bottom_corner
outside the game loop and then run the rest of the code inside the loop. But since it would look much cleaner I want to implement this code inside as a function. The problem is since the function is called inside the loop bottom_corner
is initialized as False every time the loop runs which leads to only the first half of the code being iterated. Do you have any recommendations on how to fix this issue?
def bullet_bounce(rect, bottom_corner):
to get this value as parameter. But it would need also to usereturn bottom_corner
and run asbottom_corner = bullet_bounce(rect, bottom_corner)