def create_humans( map_tiles): humans = pygame.sprite.Group() invalid_tile_chars = ("f", "g", "h") # Get all map tiles where humans can be put valid_map_tiles = [x for x in map_tiles if x.tile_char not in invalid_tile_chars] for x in map_tiles: if x.tile_char == "g": landing_zone_rect = x.rect break human_animations = [] humans_path = os.path.join( os.getcwd(), "graphics", "humans") human_animations.append( helpers.load_images( os.path.join( humans_path, "human1.png"))) human_animations.append( helpers.load_images( os.path.join( humans_path, "human2.png"))) human_animations.append( helpers.load_images( os.path.join( humans_path, "human3.png"))) for x in range(settings.N_HUMANS): random_map_tile = random.choice( valid_map_tiles) random_x = random.randint( random_map_tile.rect.left, random_map_tile.rect.right) random_y = random.randint( random_map_tile.rect.top, random_map_tile.rect.bottom) humans.add( Human( Animation(random.choice(human_animations)), 1, random_x, random_y, landing_zone_rect)) return humans
def create_bird(): speed = random.randint(3, 7) start_x = random.choice(( random.randint( -100, -50), random.randint( settings.WINDOW_WIDTH + 50, settings.WINDOW_WIDTH + 100))) start_y = random.choice(( random.randint( -100, -50), random.randint( settings.WINDOW_HEIGHT + 50, settings.WINDOW_HEIGHT + 100))) if start_y < 0: direction = random.randint(100, 250) else: direction = random.randint(290, 430) if direction >= 360: direction -= 360 bird_animation = Animation( helpers.load_images( os.path.join( os.getcwd(), "graphics", "bird")), settings.FPS) bird = Bird( copy.copy(bird_animation), settings.FPS, start_x, start_y, speed, direction) return bird
def create_helicopter(_hud): helicopter_surfaces = helpers.load_images( os.path.join( os.getcwd(), "graphics", "helicopter")) helicopter = pygame.sprite.GroupSingle( Helicopter( Animation( helicopter_surfaces, 0), settings.WINDOW_WIDTH // 2, settings.WINDOW_HEIGHT // 2, _hud)) return helicopter
def main(window): #window = pygame.display.set_mode( # (settings.WINDOW_WIDTH, # settings.WINDOW_HEIGHT), # pygame.FULLSCREEN) blue = pygame.Color( 50, 50, 250) window.fill(blue) clock = pygame.time.Clock() path = os.path.join( os.getcwd(), "graphics") hud = create_HUD() helicopter = create_helicopter(hud) map = create_map() humans = create_humans(map.update_group) clouds = pygame.sprite.Group() birds = pygame.sprite.Group() cloud_animations = [] cloud_animations.append( Animation( helpers.load_images( os.path.join( os.getcwd(), "graphics", "clouds", "cloud1.png")), 100)) cloud_animations.append( Animation( helpers.load_images( os.path.join( os.getcwd(), "graphics", "clouds", "cloud2.png")), 100)) cloud_animations.append( Animation( helpers.load_images( os.path.join( os.getcwd(), "graphics", "clouds", "cloud3.png")), 100)) cloud_animations.append( Animation( helpers.load_images( os.path.join( os.getcwd(), "graphics", "clouds", "cloud4.png")), 100)) running = True while running: if random.randint(1, 50) == 25: if len(clouds) <= 60: clouds.add( create_cloud( cloud_animations)) if random.randint(1, 15) == 7: if len(birds) <= 100: birds.add( create_bird()) events = pygame.event.get() for e in events: if e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False # Clouds always move. for cloud in clouds: cloud.move_by_itself() # Birds always move. for bird in birds: bird.move_by_itself() if map.update( helicopter.sprite.speed_x, helicopter.sprite.speed_y, helicopter): # Map was moved, so can other game objects. # TODO: Add humans, birds, clouds, ... here. humans.update( helicopter.sprite.speed_x, helicopter.sprite.speed_y, helicopter) clouds.update( helicopter.sprite.speed_x, helicopter.sprite.speed_y, helicopter, map.update_group) birds.update( helicopter.sprite.speed_x, helicopter.sprite.speed_y, helicopter, map.update_group) if hud.update() == True: # Game over. Either win or lose. # Present player with time and reason for game over. if hud.humans_rescued >= 50: msg1 = "GAME OVER" msg2 = "You have saved all 50 human lives! Well done." msg3 = "It took you %d seconds." % (hud.stopwatch) game_over_screen(window, msg1, msg2, msg3) else: msg1 = "GAME OVER" msg2 = "You did not save all 50 lives. This is a dark day for humanity." msg3 = "You played for %d seconds, before either crashing or being out of energy." % (hud.stopwatch) game_over_screen(window, msg1, msg2, msg3) running = False continue helicopter.update(events) window.fill(blue) map.update_draw_group.draw(window) humans.draw(window) birds.draw(window) helicopter.draw(window) clouds.draw(window) window.blit( hud.helicopter_hitpoints_sprite.image, hud.helicopter_hitpoints_sprite.rect) window.blit( hud.helicopter_energy_sprite.image, hud.helicopter_energy_sprite.rect) window.blit( hud.humans_rescued_sprite.image, hud.humans_rescued_sprite.rect) window.blit( hud.humans_carrying_sprite.image, hud.humans_carrying_sprite.rect) window.blit( hud.stopwatch_sprite.image, hud.stopwatch_sprite.rect) clock.tick( settings.FPS) pygame.display.update()
def __init__(self, _layout): # Layout of the map. super(Map, self).__init__() self.layout = _layout self.landing_zone = None self.update_group = pygame.sprite.Group() self.update_draw_group = pygame.sprite.Group() island_center_1_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_center1.png")) island_center_2_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_center2.png")) island_water_right_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_right.png")) island_water_right_image2 = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_right2.png")) island_water_left_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_left.png")) island_water_left_image2 = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_left2.png")) island_water_top_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_top.png")) island_water_top_image2 = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_top2.png")) island_water_bottom_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_bottom.png")) island_water_bottom_image2 = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_bottom2.png")) island_water_top_left_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_top_left.png")) island_water_top_right_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_top_right.png")) island_water_bottom_right_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_bottom_right.png")) island_water_bottom_left_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_water_bottom_left.png")) island_bar = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "island_bar.png")) water_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "water.png")) landing_zone_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "landing_zone.png")) city_image = helpers.load_images( os.path.join(os.getcwd(), "graphics", "map", "city.png")) # Create map here (iterate rows, columns) cnt = 0 left = top = 0 w = len(self.layout.readlines()[0].strip()) * settings.MAP_TILE_WIDTH self.layout.seek(0) for row in self.layout: for column in row.strip(): # Create animation. if column == "1": animation = Animation(island_center_1_image) if column == "2": animation = Animation(island_center_2_image) if column == "3": animation = Animation(island_water_right_image) if column == "4": animation = Animation(island_water_left_image) if column == "5": animation = Animation(island_water_top_image) if column == "6": animation = Animation(island_water_bottom_image) if column == "7": animation = Animation(island_water_top_left_image) if column == "8": animation = Animation(island_water_top_right_image) if column == "9": animation = Animation(island_water_bottom_right_image) if column == "0": animation = Animation(island_water_bottom_left_image) if column == "a": animation = Animation(island_water_top_image2) if column == "b": animation = Animation(island_water_bottom_image2) if column == "c": animation = Animation(island_water_left_image2) if column == "d": animation = Animation(island_water_right_image2) if column == "e": animation = Animation(island_bar) if column == "f": animation = Animation(water_image) if column == "g": animation = Animation(landing_zone_image) if column == "h": animation = Animation(city_image) # Create maptile, passing the animation instance to it. map_tile = MapTile(animation, 1, left, top, column) if map_tile.tile_char == "g": self.landing_zone = map_tile self.update_group.add(map_tile) left += settings.MAP_TILE_WIDTH left = 0 top += settings.MAP_TILE_HEIGHT h = top h -= settings.MAP_TILE_HEIGHT * 4 w -= settings.MAP_TILE_WIDTH * 6 self.image = pygame.Surface((w, h)) self.rect = self.image.get_rect() self.rect.top = settings.MAP_TILE_HEIGHT * 2 self.rect.left = settings.MAP_TILE_WIDTH * 3 # Set starting pos start_x = -(w / 2) - settings.MAP_TILE_WIDTH start_y = -h self.rect.move_ip(start_x, start_y) self.update_group.update(start_x, start_y, self.update_draw_group)