my Python code has a circle which moves from the right of the screen to the left but it stops. I would like it to bounce off the left edge and continue moving to the right and then bounce off the right edge to the left and so on. I think I'm missing a line. I have tried several things but it doesn't seem to be working. Please see code below. Any advice would be very grateful.
import pygame
pygame.init()
size = width, height = 400, 300
screen = pygame.display.set_mode(size)
x_pos = 380
y_pos = 280
r = 20
running = True
while running: # game cycle
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.draw.circle(screen, (0, 255, 0), (x_pos, y_pos), r)
if x_pos > 20: # do not let the ball roll out of the screen
x_pos -= 1
pygame.time.delay(5) # delay in milliseconds
pygame.display.flip()
pygame.quit()
I think I am expecting another IF statement which allows it to bounce off the edge. I would like to continue using the code that I have, and I'm looking for just one or two lines that can hopefully solve my problems. I don't want the code to be completely revamped.