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 # starting position and speed for the ball x = random.randint(0+radius, width-radius) y = random.randint(0+radius,height-radius) xspeed = float(random.randint(-10,10))/10 yspeed = float(random.randint(-10,10))/10 ball = [x, y, xspeed, yspeed] ## 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 circle position ball[0] = ball[0] + ball[2] ball[1] = ball[1] + ball[3] if (ball[0] < radius): ball[2] = -1 * ball[2] if (ball[0] > width-radius): ball[2] = -1 * ball[2] if (ball[1] < radius): ball[3] = -1 * ball[3] if (ball[1] > height-radius): ball[3] = -1 * ball[3] ## Draw picture and update display my_win.fill(pygame.color.Color("lightblue")) 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()