def make_monster(monster_type: MonsterType, x, y, world: SpriteGroups, sleeping=False): if type(monster_type) is str: monster_type = MonsterType[monster_type] monster = Character(monster_type) monster_sprite = Monster(x, y, monster, world) if move_to_nearest_empty_space(monster_sprite, world, 500): monster.sleeping = sleeping world.all.add(monster_sprite) world.solid.add(monster_sprite) world.monster.add(monster_sprite) return monster_sprite return None
def test_generator_withOneGenerator_produceMonsters(self): world = make_empty_sprite_groups() player_character = Character(PlayerCharacterType.TOBY) player = Player(500, 500, player_character, world) generator = actors.make_monster(MonsterType.ZOMBIE_GENERATOR, 0, 0, world) generator.targeted_enemy = player self.assertEqual(1, len(world.solid)) generator.do_actions(world) self.assertEqual(2, len(world.solid))
def test_generator_withTwoGenerators_produceTwoMonsters(self): world = make_empty_sprite_groups() player_character = Character(PlayerCharacterType.TOBY) player = Player(500, 500, player_character, world) generators = [ actors.make_monster(MonsterType.ZOMBIE_GENERATOR, 0, 0, world), actors.make_monster(MonsterType.ZOMBIE_GENERATOR, 100, 100, world) ] self.assertEqual(2, len(world.solid)) for g in generators: g.targeted_enemy = player g.do_actions(world) self.assertEqual(4, len(world.solid))
def test_generator_withTime_producesMonstersAtRateOfFire(self): pygame.time.Clock() world = make_empty_sprite_groups() player_character = Character(PlayerCharacterType.TOBY) player = Player(500, 500, player_character, world) generators = [ actors.make_monster(MonsterType.ZOMBIE_GENERATOR, 0, 0, world), actors.make_monster(MonsterType.ZOMBIE_GENERATOR, 100, 100, world) ] self.assertEqual(2, len(world.solid)) for g in generators: g.targeted_enemy = player g.actions[0].rate_of_fire = 10 g.do_actions(world) self.assertEqual(4, len(world.solid)) t = pygame.time.get_ticks() while t + 150 > pygame.time.get_ticks(): for g in generators: g.do_actions(world) self.assertEqual(6, len(world.solid))
def test_character_withMonsterTemplate(self): zombie = Character(MonsterType.ZOMBIE) self.assertEqual(10, zombie.vitality) self.assertEqual(10, zombie.template.treasure)
def test_character_withPlayerCharacterTemplate(self): player = Character(PlayerCharacterType.TOBY) self.assertEqual(15, player.vitality) self.assertEqual(1.5, player.rate_of_fire)
def play(): pygame.mixer.pre_init(frequency=44100) pygame.init() pygame.mixer.init(frequency=44100) screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT)) #, pygame.FULLSCREEN) background = screen.copy() intro.play(screen) clock = pygame.time.Clock() tile_manager = tiles.TileManager() tile_manager.import_tiles("terrain.png", 8, 16) game_map = tiles.TileMap(tile_manager.tiles[7]) world = interfaces.SpriteGroups() floorplan.create_objects(0, world, game_map) game_map.render(background, 0, 0, SCREEN_WIDTH // game_map.tile_width, SCREEN_HEIGHT // game_map.tile_height) screen.blit(background, (0, 0)) player_character = Character(PlayerCharacterType.TOBY) player = Player(500, 500, player_character, world) move_to_nearest_empty_space(player, world, 100) world.all.add(player) world.solid.add(player) make_monster(MonsterType.ZOMBIE_GENERATOR, 200, randint(0, screen.get_height()), world) make_monster(MonsterType.ZOMBIE_GENERATOR, 800, randint(0, screen.get_height()), world) heart_sheet = SpriteSheet(pygame.image.load(image_file("heart.png")), 1, 1) health_bar = ScoreBar(30, 30, heart_sheet, 100, 10, scale=0.1) world.hud.add(health_bar) player.add_observer(health_bar, "vitality") while True: world.all.clear(screen, background) world.hud.clear(screen, background) world.all.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: return if event.type == pygame.KEYDOWN and event.key == pygame.K_F12: debug_filmstrips(screen, world.all) screen.blit(background, (0, 0)) kb = pygame.key.get_pressed() player.handle_keyboard(kb) if kb[pygame.K_RETURN]: player.shoot() player.move(world.solid) for monster in world.monster: monster.notify(player) monster.move(world.solid) monster.do_actions(world) for missile in world.missile: missile.move(world.solid) if out_of_bounds(missile): missile.kill() missile_hits = pygame.sprite.groupcollide(world.solid, world.missile, False, False) for hit, missiles in missile_hits.items(): for m in missiles: m.on_impact(hit, world) pick_ups = pygame.sprite.spritecollide( player, world.items, dokill=True, collided=pygame.sprite.collide_mask) for item in pick_ups: item.on_pick_up(player) world.all.draw(screen) world.hud.draw(screen) pygame.display.flip() clock.tick(20)