Exemplo n.º 1
0
 def test_tile_at_withOneTileDefined_returnsThatTileAtOnlyThatLocation(self):
     tile_map = tiles.TileMap(self.red_tile)
     tile_map.place_tile(self.blue_tile, 1, 1)
     result = tile_map.tile_at(1, 1)
     r, g, b, alpha = result.get_at((0, 0))
     self.assertEqual(BLUE, (r, g, b))
     self.assertEqual(self.blue_tile, result)
     self.assertEqual(self.red_tile, tile_map.tile_at(0, 1))
Exemplo n.º 2
0
 def test_render_withRenderToPartOfSurface_onlyRendersSpecified(self):
     tile_map = tiles.TileMap(self.red_tile)
     surface = pygame.Surface((3, 3))
     surface.fill(BLUE)
     tile_map.render(surface, 1, 0, 2, 2)
     # Expect to get this pattern:
     #   brr
     #   brr
     #   bbb
     for y in range(3):
         for x in range(3):
             expected = BLUE if y == 2 or x == 0 else RED
             colour = surface.get_at((x, y))
             self.assertEqual(expected, colour)
Exemplo n.º 3
0
 def test_render_withOneTileDefined_rendersToCorrectPlace(self):
     tile_map = tiles.TileMap(self.red_tile)
     tile_map.place_tile(self.blue_tile, 1, 1)
     surface = pygame.Surface((3, 3))
     tile_map.render(surface, 0, 0, 3, 3)
     # Expect to get this pattern:
     #   rrr
     #   rbr
     #   rrr
     for y in range(3):
         for x in range(3):
             expected = BLUE if y == 1 and x == 1 else RED
             colour = surface.get_at((x, y))
             self.assertEqual(expected, colour)
Exemplo n.º 4
0
 def test_tile_at_withNoTiles_returnsDefaultTile(self):
     tile_map = tiles.TileMap(self.red_tile)
     result = tile_map.tile_at(1, 1)
     self.assertEqual(self.red_tile, result)
Exemplo n.º 5
0
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)