import pygame import random def run_game(): ## Initialize the pygame submodules and set up the display window. pygame.init() width = 640 height = 480 my_win = pygame.display.set_mode((width,height)) # We will use the same radius for all balls. radius = 20 # Create a list of balls. Each ball represented as a list # of four elements: x, y, x_speed, y_speed. balls = [] for count in range(0,10): x = random.randint(0+radius, width-radius) y = random.randint(0+radius,height-radius) xspeed = float(random.randint(-100,100))/1000 yspeed = float(random.randint(-100,100))/1000 ball = [x, y, xspeed, yspeed] balls = balls + [ball] # clock clock = pygame.time.Clock() ## The game loop starts here. keepGoing = True while (keepGoing): ## Handle events. for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False ## Update game objects dt = clock.tick() # update circle position for ball in balls: ball[0] = ball[0] + ball[2] * dt ball[1] = ball[1] + ball[3] * dt if (ball[0] < radius): ball[0] = radius ball[2] = -1 * ball[2] if (ball[0] > width-radius): ball[0] = width-radius ball[2] = -1 * ball[2] if (ball[1] < radius): ball[1] = radius ball[3] = -1 * ball[3] if (ball[1] > height-radius): ball[1] = height-radius ball[3] = -1 * ball[3] ## Draw picture and update display my_win.fill(pygame.color.Color("lightblue")) # Draw all balls in the list for ball in balls: pygame.draw.circle(my_win, pygame.color.Color('darkmagenta'), (int(ball[0]),int(ball[1])),radius) pygame.display.update() ## The game loop ends here. pygame.quit() ## Call the function run_game. run_game()