import pygame 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)) pop_sound = pygame.mixer.Sound("pop.wav") # starting position for the circle x = 320 y = 240 radius = 50 xspeed = -1 yspeed = -1 ## The game loop starts here. keepGoing = True while (keepGoing): ## Handle events. for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False elif event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() if (mouse_x - x)**2 + (mouse_y - y)**2 < radius**2: pop_sound.play() # update circle position x = x + xspeed y = y + yspeed if (x < radius): xspeed = -1 * xspeed if (x > width-radius): xspeed = -1 * xspeed if (y < radius): yspeed = -1 * yspeed if (y > height-radius): yspeed = -1 * yspeed ## Draw picture and update display my_win.fill(pygame.color.Color("lightblue")) pygame.draw.circle(my_win, pygame.color.Color("navy"), (x,y),radius) pygame.display.update() ## The game loop ends here. pygame.quit() ## Call the function run_game. run_game()