import pygame ### initialization width = 640 height = 480 screen = pygame.display.set_mode((width,height)) # loading the balloon picture; gif file needs to be in same # folder as this file (one_balloon.py) balloon = pygame.image.load("red_balloon.gif") # get the dimensions of the balloon image b_width = balloon.get_width() b_height = balloon.get_height() # convert graphics format (in this case gif) to Pygame's internal # format balloon = balloon.convert() # initial x and y coordinates of balloon b_x = 100.0 b_y = 100.0 # speed of balloon in x and y direction speed_x = 0.2 speed_y = 0.1 # set up clock clock = pygame.time.Clock() ### the game loop keepGoing = True while (keepGoing): # deal with events for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False # background color screen.fill(pygame.color.Color("darkblue")) # number of milliseconds since last time dt = clock.tick() # update position based on speed and time passed b_x = b_x + speed_x * dt b_y = b_y + speed_y * dt # bounce if going beyond edge of screen if (b_x < 0): b_x = 0 speed_x = -1 * speed_x elif (b_x > width - b_width): b_x = width - b_width speed_x = -1 * speed_x if (b_y < 0): b_y = 0 speed_y = -1 * speed_y elif (b_y > height - b_height): b_y = height - b_height speed_y = -1 * speed_y # draw picture screen.blit(balloon, (b_x,b_y)) # update display pygame.display.update() ### quit pygame.quit()