import pygame def run_game(): ## Initialize the pygame submodules and set up the display window. pygame.init() width = 640 height = 480 game_window = pygame.display.set_mode((width,height)) # load the balloon picture and convert it into Pygame's internal # image format balloon = pygame.image.load("balloon.gif") balloon = balloon.convert() balloon_w = balloon.get_width() balloon_h = balloon.get_height() balloon_x = 100 balloon_y = 100 balloon_vx = 0.5 balloon_vy = 0.5 ## The game loop starts here. keepGoing = True while (keepGoing): ## Handle events. for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False balloon_x += balloon_vx balloon_y += balloon_vy if balloon_x > width - balloon_w: balloon_x = width - balloon_w balloon_vx = -balloon_vx elif balloon_x < 0: balloon_x = 0 balloon_vx = -balloon_vx if balloon_y > height - balloon_h: balloon_y = height - balloon_h balloon_vy = -balloon_vy elif balloon_y < 0: balloon_y = 0 balloon_vy = -balloon_vy ## Draw picture and update display game_window.fill(pygame.color.Color("darkblue")) game_window.blit(balloon, (balloon_x,balloon_y)) pygame.display.update() ## The game loop ends here. pygame.quit() ## Call the function run_game. run_game()