Beispiel #1
0
    def start():
        pygame.init()
        pygame.display.set_caption('PyDungeon')
        size = width, height = (640, 480)
        game = Game()
        game.setup_default_components(size)
        scene = game.scene

        tilemap = Entity()
        scene.add_entity(tilemap)
        tilemap.add_component(NameComponent("tilemap"))

        tilemap.add_component(TransformComponent(Vector2(0, 0)))

        ts = TileSet()
        ts.load("assets/tileset.png", "assets/tileinfo.info")
        tm = TileMap(ts, size)
        mp = [[["floor_" + str(random.randrange(1, 8))] for _ in range(24)]
              for _ in range(24)]
        PyDungeons.build_wall_rect(mp, pygame.Rect(2, 2, 10, 10))
        tm.load_letters(mp)
        tilemap.add_component(tm)
        tilemap.add_component(
            TileMapCollider(
                tm, ["wall_mid", "wall_side_mid_right", "wall_side_mid_left"]))
        tilemap.add_component(RendererComponent(TileMapRenderer(), size))

        player = Entity()
        scene.add_entity(player)
        player.add_component(NameComponent("player"))

        key_bindings = [[pygame.K_a], [pygame.K_d], [pygame.K_w], [pygame.K_s]]

        player.add_component(MoveComponent(1, 2))
        player.add_component(KeyControlComponent(key_bindings))
        #player.add_component(ScreenBoundsCollisionHandler(pygame.Rect(0, 0, width, height)))
        player.add_component(TransformComponent(Vector2(100, 100)))
        player.add_component(BoxCollider((16 * 2, 22 * 2), Vector2(0, 12)))
        player.add_component(
            RendererComponent(TileRenderer(ts.tiles["knight_f_idle_anim"], ts),
                              (16 * 2, 28 * 2)))
        #player.add_component(RendererComponent(ImageRenderer("assets/tileset.png"), (1000, 1000)))

        game.add_component(ExitOnEscape())

        game.run()
    def start():
        key_bindings = [[pygame.K_a], [pygame.K_d], [pygame.K_w], [pygame.K_s]]

        pygame.init()

        size = width, height = (640, 480)
        game = Game(size)

        game.add_component(ExitOnEscape())

        scene = game.scene

        map1 = "G      GGG\n         G\nGGGGGGG  G\n         F"
        map2 = "GGGGGGGG G\nG         \nGGGGGGGG G\n         G"
        tiles = {" ": AirTile, "G": GrassTile, "F": FinishTile}

        tile_map = Entity()
        player = Entity()

        scene.add_entity(tile_map)
        tile_map.add_component(
            TransformComponent(pygame.Rect(0, 0, width, height)))
        tile_map.add_component(TileMap())
        tile_map.add_component(TileLoader(map1, tiles))
        tile_map.add_component(
            MapSwap(pygame.K_f, map1, map2, tiles, [tile_map, player]))
        tile_map.add_component(RendererComponent(TileRenderer()))

        scene.add_entity(player)
        player.add_component(
            TransformComponent(pygame.Rect(width // 2, height // 2, 50, 50)))
        player.add_component(MoveComponent(1, 4))
        player.add_component(
            ScreenBoundsCollisionHandler(pygame.Rect(0, 0, width, height)))
        player.add_component(
            TileMapCollisionHandler(tile_map.get_component(TileMap),
                                    {AirTile}))
        player.add_component(KeyControlComponent(key_bindings))
        player.add_component(RendererComponent(SmileRenderer()))
        player.add_component(ResetWhenSwapAndCollideComponent(tile_map))

        game.add_component(ResetGameComponent(tile_map, player))

        start_text = Entity()
        scene.add_entity(start_text)
        start_text.add_component(
            TransformComponent(
                pygame.Rect(width // 2 - 200, height // 2, 400, 50)))
        start_text.add_component(
            RendererComponent(TextRenderer("Smile Dimensions", 40)))

        controls_text = "WASD - to move, F - swap dimension, Enter - continue"
        controls = Entity()
        scene.add_entity(controls)
        controls.add_component(
            TransformComponent(
                pygame.Rect(width // 2 - 200, height // 2 + 50, 400, 20)))
        controls.add_component(
            RendererComponent(TextRenderer(controls_text, 14)))

        rules = "If you collide with wall in other dimension, game will be reset"
        rules_text = Entity()
        scene.add_entity(rules_text)
        rules_text.add_component(
            TransformComponent(
                pygame.Rect(width // 2 - 220, height // 2 + 70, 450, 20)))
        rules_text.add_component(RendererComponent(TextRenderer(rules, 14)))

        game.add_component(
            HideOnKeyComponent([start_text, controls, rules_text],
                               pygame.K_RETURN))

        game.run()