import pygame def initialize_constants(): """ This function sets some global constants (i.e., the values of these global variables never change). """ global name name = raw_input("Please type in your name: ") pygame.init() # background pic global sky sky = pygame.image.load("sky_scaled.jpg") # set up the display window global width, height, screen width = sky.get_width() ##640 height = sky.get_height() ##480 screen = pygame.display.set_mode((width,height)) sky = sky.convert() # can only convert after the video mode is set # load the ballon picture global balloon balloon = pygame.image.load("red_balloon.gif") balloon = balloon.convert() global balloon2 balloon2 = pygame.image.load("green_balloon.gif") balloon2 = balloon2.convert() # load sound global pop_sound pop_sound = pygame.mixer.Sound("pop.wav") pygame.mixer.music.load("mario_bros_trimmed.ogg") # load font global myFont myFont = pygame.font.Font(None,30) def move_and_bounce_object(dt, x, y, speed_x, speed_y, obj_width, obj_height): """ This function calculates an object's new position based on its old position, the time that has passed since the last update, and its speed. It also makes sure that the object bounces off the edge of the screen. """ x = x + dt * speed_x if (x < 0): x = 0 speed_x = - speed_x elif (x > width - obj_width): x = width - obj_width speed_x = - speed_x y = y + dt * speed_y if (y < 0): y = 0 speed_y = - speed_y elif (y > height - obj_height): y = height - obj_height speed_y = - speed_y # Here you see that in Python we can return more than one value. # This is a specialy of Python. return x, y, speed_x, speed_y def clicked_image(mouse_x, mouse_y, x, y, w, h): if mouse_x >= x and mouse_x <= x+w and mouse_y >= y and mouse_y <= y+h: return True else: return False def run_game(): # initialize the balloons' positions and speeds b_x = 100 b_y = 100 b_speed_x = 0.2 b_speed_y = 0.2 b_w = balloon.get_width() b_h = balloon.get_height() b2_x = 300 b2_y = 300 b2_speed_x = 0.1 b2_speed_y = 0.2 # initialize the score score = 0 clock = pygame.time.Clock() dead = False keepGoing = True pygame.mixer.music.play(-1) while (keepGoing): dt = clock.tick() # handle events for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False # If the player clicks into the red balloon, he gets a # point. The game also plays a popping sound and the # balloon starts moving a bit faster. # If the player clicks outside the red balloon, he dies. elif not dead and event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() if clicked_image(mouse_x, mouse_y, b_x, b_y, b_w, b_h): pop_sound.play() score = score + 1 b_speed_x = 1.1 * b_speed_x b_speed_y = 1.1 * b_speed_y else: dead = True # Update the positions and speeds of the balloons. if not dead: b_x, b_y, b_speed_x, b_speed_y = move_and_bounce_object(dt, b_x, b_y, b_speed_x, b_speed_y, balloon.get_width(),balloon.get_height()) b2_x, b2_y, b2_speed_x, b2_speed_y = move_and_bounce_object(dt, b2_x, b2_y, b2_speed_x, b2_speed_y, balloon2.get_width(),balloon2.get_height()) # draw everything #screen.fill(pygame.color.Color("darkblue")) screen.blit(sky, (0,0)) # score x = 10 y = 10 label = myFont.render("Your score: "+str(score), True, pygame.color.Color("magenta")) screen.blit(label, (x,y)) # balloon images screen.blit(balloon,(b_x,b_y)) screen.blit(balloon2,(b2_x,b2_y)) # game over message if dead: label = myFont.render("Sorry, "+name+"! Game over!", True, pygame.color.Color("magenta")) screen.blit(label, ((width-label.get_width())/2,height/2-50)) label = myFont.render("You didn't click on the red balloon.", True, pygame.color.Color("magenta")) screen.blit(label, ((width-label.get_width())/2,height/2-25)) pygame.display.update() def done(): global myFont myFont = None pygame.quit() def main(): initialize_constants() run_game() done() main()