def tick(): """Update global engine state, advance one frame. Call this function once per frame. This function redraws the screen, gets input, moves sprites, calls the sprite behavior functions, and animates sprites. Call flip() after every tick(). You may draw to the screen after tick() draws all the sprites before calling flip(). """ global _debug_fps clock.tick(fps) #event.handle_events() #sprite.update_sprites() if background_color: graphics.draw_background(background_color) #sprite.draw_sprites() #flip() _debug_fps += 1 if _debug_fps > 50: #every 50 frames if SHOW_FPS: print clock.get_fps(), "FPS" _debug_fps = 0
def game_loop(): global BORDER_LOWER, BORDER_LEFT, BORDER_RIGHT global TICKS_PER_SECOND global SHOT_SPEED,SHIP_SPEED enemies = [] generate_enemy_wave(enemies) screen = pygame.display.get_surface() shoot_sound = sound.load_sound('psh.ogg') enemy_explosion_sound = sound.load_sound('uddh.ogg') position = [(SCREEN_SIZE[0]) / 2, BORDER_LOWER-10] ship_sprite = PlayerShip.PlayerShip(position) shot_sprite = Shot.Shot([0,0]) enemy_shot_sprite = Shot.Shot([0,0]) ship_sprite.size = ship_sprite.image.get_size() # shotX, shotY = shot_sprite.image.get_size() # enemy_shotX, enemy_shotY = shotX, shotY ship_sprite.coorX = ship_sprite.position[0] ship_sprite.coorY = ship_sprite.position[1] # shot_coorX = 0.0 # shot_coorY = 0.0 ship_sprite.coorX = (SCREEN_SIZE[0] - ship_sprite.size[0]) / 2 ship_sprite.coorY = BORDER_LOWER - ship_sprite.size[1] shot_exists = False enemy_shot_exists = False score = 0 clock = pygame.time.Clock() ticks = 0 done = False # Main game loop while not done: clock.tick(TICKS_PER_SECOND) ticks += 1 if (ticks % TICKS_PER_SECOND) == 0: fps = clock.get_fps() # print fps ##################################################################################### # read keyboard and move player ship events = pygame.event.get() keystate = pygame.key.get_pressed() if keystate[K_ESCAPE] == 1: done = True break if keystate[K_a] == 1: ship_sprite.move_left() if keystate[K_d] == 1: ship_sprite.move_right() if keystate[K_SPACE] == 1: if not shot_exists: shoot_sound.play() shot_exists = True shot_sprite.position[0], shot_sprite.position[1] = ship_sprite.position[0] + (ship_sprite.size[0] - shot_sprite.size[0]) / 2, ship_sprite.position[1] - shot_sprite.size[1] #generate shot near top middle of gun if keystate[K_e] == 1: if not enemy_shot_exists: #shoot_sound.play() enemy_shot_exists = True enemy_shot_sprite.position[0] = 400.0 #random.randint(BORDER_LEFT, BORDER_RIGHT) enemy_shot_sprite.position[1] = 400.0 #shot_coorX, shot_coorY = ship_sprite.coorX + (ship_sprite.size[0] - shotX) / 2, ship_sprite.coorY - shotY #generate shot near top middle of gun for event in events: if event.type == QUIT: done = True break ####################################################################################### # collision detection shot <-> enemies dying_enemies = [] # empty list that gets filled as enemies get shot for i in range(len(enemies)): if shot_exists: if collidesWith(shot_sprite,enemies[i]): # Collision! score += enemies[i].get_score() dying_enemies.append(i) shot_exists = False enemy_explosion_sound.play() # TODO: enemy explosion graphics explosion.create(enemies[i].position) #print "zerstort" # remove all enemies that were hit. delta = 0 for i in range(len(dying_enemies)): del enemies[dying_enemies[i + delta]] delta += 1 # each time we remove one, the index of all the others must be reduced. This assumes that the list of dying enemies is sorted # collision detection enemy_shot <-> PlayerShip #dying_enemies = [] # empty list that gets filled as enemies get shot if enemy_shot_exists: if collidesWith(enemy_shot_sprite,ship_sprite): # Collision! enemy_shot_exists = False enemy_explosion_sound.play() # TODO: ship explosion graphics # remove all enemies that were hit. # detect end of wave if len(enemies) == 0: generate_enemy_wave(enemies) ############################################################################################ if shot_exists: #move shot shot_sprite.position[1] -= SHOT_SPEED if shot_sprite.position[1] <= BORDER_UPPER: shot_exists = False if enemy_shot_exists: #move shot enemy_shot_sprite.position[1] += SHOT_SPEED if enemy_shot_sprite.position[1] >= BORDER_LOWER: enemy_shot_exists = False ############################################################################################ graphics.draw_background(screen) text.draw_text(screen, score) if shot_exists: # draw shot screen.blit (shot_sprite.image, (shot_sprite.position[0], shot_sprite.position[1])) if enemy_shot_exists: # draw shot screen.blit (shot_sprite.image, (enemy_shot_sprite.position[0], enemy_shot_sprite.position[1])) for i in range(len(enemies)): enemies[i].draw(screen) explosion.draw(screen) # draw player ship ship_sprite.draw(screen) # swap back and front buffers pygame.display.flip()