Is there a condition that can be added to this code that will cause the x_direction to change making the ball appear to bounce if width is exceeded?Something like: if x_val > width x_direction == x_direction * -1? (likewise for the y_direction).
import pygame
BLACK = (0,0,0)
WHITE = (255,255,255)
BLUE = (50,50,255)
YELLOW = (255,255,0)
width = 64
height = 480
pygame.init()
size = (width,height)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")
done = False
clock = pygame.time.Clock()
ball_width = 20
x_val = 150
y_val = 200
x_direction = 1
y_direction = 1
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
x_val = x_val + x_direction
y_val = y_val + y_direction
screen.fill (BLACK)
# condition to go here to make ball bounce
pygame.draw.rect(screen, BLUE, (x_val,y_val,ball_width,20))
pygame.display.flip()
clock.tick(60)
pygame.quit()