# Homework 34 solution, problem 2: two-ball bounce. # Run with: python exercises/34/homework/solutions/02-two-balls.py import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Two Balls") clock = pygame.time.Clock() ax, ay, adx, ady = 200, 150, 4, 3 bx, by, bdx, bdy = 600, 400, -3, 5 radius = 20 W, H = 800, 600 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False ax += adx ay += ady if ax - radius < 0 or ax + radius > W: adx = -adx if ay - radius < 0 or ay + radius > H: ady = -ady bx += bdx by += bdy if bx - radius < 0 or bx + radius > W: bdx = -bdx if by - radius < 0 or by + radius > H: bdy = -bdy screen.fill((10, 10, 10)) pygame.draw.circle(screen, (255, 80, 80), (ax, ay), radius) pygame.draw.circle(screen, (80, 180, 255), (bx, by), radius) pygame.display.flip() clock.tick(60) pygame.quit()