Exemplo n.º 1
0
def create_minimap(world, pos, building):
    """Create a new minimap based on the building.

    Return the MinimapState object.
    """
    state = MinimapState()

    minimap_tileset = world.entity()
    minimap_tileset.add_components(
        Positionable(pos[0], pos[1], 0, 0),
        TileSpace("minimap", (1, 1))
    )

    switcher = RoomSwitcher(world, building.rooms[0])

    for room in building.rooms:
        room_widget = RoomWidget(room, building.room_size, switcher, state)
        room_entity = world.entity()
        room_entity.add_components(
            Positionable(0, 0, building.room_size[0], building.room_size[1]),
            Renderable(room_widget.draw, 1),
            TilePositionable("minimap", room.position, 1),
            Hoverable(room_widget.toggle_hover, room_widget.toggle_hover),
            Clickable(room_widget.activate, Button.LEFT)
        )

    return state
Exemplo n.º 2
0
    def __init__(self, master, **kwargs):
        super().__init__(master, **kwargs)
        Oriented.__init__(self, 'north')
        Rectangular.__init__(self, (self.winfo_reqwidth(), self.winfo_reqheight()))
        Positionable.__init__(self, (0, 0))

        self.images = {}
        self.sprites = {}
        self.key_pressed = False

        self.map = GameMap(GameMapConfig.size, self)
        self.map.place_rectangle(pos=(0, 0), dim=(500, 500), fill='green')
        self.map.draw()
Exemplo n.º 3
0
def create_character(world, base_pos, animation_name, base_direction, fps):
    character = world.entity()
    character.add_components(Positionable(0, 0, 40, 80),
                             Renderable(lambda brush: None, 2),
                             TilePositionable("ground", base_pos, 2),
                             Animable(), Activable())
    return Character(character, animation_name, base_direction, fps)
Exemplo n.º 4
0
def create_sky_effect(world, pos, size, layer):
    """Setup a sky entity and return its helper.
    """
    sky = world.entity()
    sky.add_components(
        Positionable(pos[0], pos[1], size[0], size[1]), Colorable(day_color),
        Renderable(lambda brush, color: brush.draw_rect(color, (0, 0), size),
                   layer), Animable(), Sky())

    return SkyHelper(sky)
def create_bubble(world, scenario_state):
    bubble = world.entity()
    bubble.add_components(
        Positionable(0, 0, 70, 70),
        Renderable(lambda brush: None, 20),
        TilePositionable("ground", (0, 0), 20),
        Activable(),
        Animable()
    )
    bubble.get_component(Activable).toggle()

    scenario_state["bubble"] = bubble
def create_compartment(world, scenario_state):
    compartment = world.entity()
    compartment.add_components(
        Positionable(0, 0, 50, 50),
        Renderable(
            lambda brush: brush.draw_image("compartment_open.png"),
            1
        ),
        TilePositionable("wall", (2, 0.3), 2)
    )

    scenario_state["compartment"] = compartment
def create_burglar(world, scenario_state):
    burglar = create_character(
        world,
        (1, 4),
        "burglar_lantern",
        CharacterDirection.LEFT,
        2.5
    )

    burglar.entity.get_component(Animable).add_animation(
        FlickerAnimation(6, 0.75)
    )

    burglar.entity.add_component(
        Lightable(
            Positionable(-20, 10, 60, 60),
            Positionable(-70, -40, 160, 160),
            (205, 155, 29, 64)
        )
    )

    scenario_state["burglar"] = burglar
Exemplo n.º 8
0
    def __init__(self,
                 name,
                 canvas,
                 dimension,
                 position,
                 direction='south',
                 speed=1):
        Oriented.__init__(self, direction)
        Movable.__init__(self, speed)
        Positionable.__init__(self, position)
        Rectangular.__init__(self, dimension)

        self.name = name
        self._image = create_photo_image(
            os.path.join(AssetsConfig.sprites, self.name,
                         f'{self.direction}.png'), self.dimension)

        self.canvas = canvas
        self.canvas.save_sprite(self)
        self._canvas_id = self.NO_CANVAS_ID

        self.sleep_time = 10
        self.costume_num = 0
def create_building(world, scenario_state, sound_system):
    up_door = world.entity()
    up_door.add_components(
        Positionable(0, 0, 50, 100),
        Renderable(
            lambda brush: brush.draw_image("door2_t.png"),
            3
        ),
        TilePositionable("wall", (8, 1), 1),
        Activable(False)
    )

    left_door = world.entity()
    left_door.add_components(
        Positionable(0, 0, 50, 100),
        Renderable(
            lambda brush: brush.draw_image("door2_l.png"),
            3
        ),
        TilePositionable("wall", (0, 6), 3),
        Activable(False)
    )

    down_door = world.entity()
    down_door.add_components(
        Positionable(0, 0, 50, 100),
        Renderable(
            lambda brush: brush.draw_image("door2_b.png"),
            3
        ),
        TilePositionable("wall", (8, 10), 3),
        Activable(False)
    )

    right_door = world.entity()
    right_door.add_components(
        Positionable(0, 0, 100, 50),
        Renderable(
            lambda brush: brush.draw_image("door2_r.png"),
            3
        ),
        TilePositionable("wall", (12, 6), 3),
        Activable(False)
    )

    up_window = world.entity()
    up_window.add_components(
        Positionable(0, 0, 100, 100),
        Renderable(
            lambda brush: brush.draw_image("window_t.png"),
            1
        ),
        TilePositionable("wall", (7, 1), 1),
        Activable(False)
    )

    left_window = world.entity()
    left_window.add_components(
        Positionable(0, 0, 100, 100),
        Renderable(
            lambda brush: brush.draw_image("window_l.png"),
            1
        ),
        TilePositionable("wall", (0, 3), 1),
        Activable(False)
    )

    down_window_renderable = Renderable(
        lambda brush: brush.draw_image("window_b.png"),
        1
    )

    down_window_toggled = [False]

    def toggle_down_window():
        down_window_renderable.render_image(
            "window_b.png" if down_window_toggled[0] else "window_semiopen_b.png",
            (0, 0) if down_window_toggled[0] else (-24, 0)
        )
        down_window_toggled[0] = not down_window_toggled[0]
        sound_system.play("window")

    down_window = world.entity()
    down_window.add_components(
        Positionable(0, 0, 100, 100),
        down_window_renderable,
        TilePositionable("wall", (4, 10), 1),
        Clickable(
            toggle_down_window,
            Button.LEFT
        ),
        Activable(False)
    )
    add_cursor_change_hoverable(down_window)

    def is_activated(entity):
        return entity.get_component(Activable).activated

    scenario_state["has_window"] = lambda: is_activated(up_window)
    scenario_state["has_down_door"] = lambda: is_activated(down_door)
    scenario_state["has_up_door"] = lambda: is_activated(up_door)
    scenario_state["has_right_door"] = lambda: is_activated(right_door)

    scenario_state["window"] = up_window

    building = Building(
        [
            Room((0, 0), [left_door, right_door, down_door, left_window]),
            Room((0, 30), [up_door, left_window]),
            Room((30, 0), [left_door, down_door, up_window]),
            Room((30, 30), [up_door, down_window])
        ],
        (30, 30)
    )

    scenario_state["minimap"] = create_minimap(world, (700, 50), building)

    for i in range(2, 5):
        bookshelf = world.entity()
        bookshelf.add_components(
            Positionable(0, 0, 50, 100),
            Renderable(
                lambda brush: brush.draw_image("bookshelf.png"),
                1
            ),
            TilePositionable("ground", (i, 1), 1)
        )

        if i == 2:
            def bookshelf_move(animable, scenario_state, direction, duration):
                def move():
                    animable.add_animation(
                        TileMoveAnimation(direction, duration)
                    )
                    scenario_state["bookshelf_moved"] =\
                        not scenario_state["bookshelf_moved"]
                    sound_system.play("furniture")

                return move

            animable = Animable()
            scenario_state["bookshelf_moved"] = False
            scenario_state["bookshelf_move_left"] =\
                bookshelf_move(animable, scenario_state, (-2, 0), 1)
            scenario_state["bookshelf_move_right"] =\
                bookshelf_move(animable, scenario_state, (2, 0), 1)
            scenario_state["bookshelf_can_move"] = True

            def toggle_bookshelf(bookshelf):
                def toggle():
                    if scenario_state["bookshelf_can_move"]:
                        if scenario_state["bookshelf_moved"]:
                            scenario_state["bookshelf_move_right"]()
                        else:
                            scenario_state["bookshelf_move_left"]()

                return toggle

            bookshelf.add_components(
                animable,
                Clickable(
                    toggle_bookshelf(bookshelf),
                    Button.LEFT
                ),
                Frightening()
            )

            add_cursor_change_hoverable(bookshelf)

    fireplace_anim = Animable()
    fireplace_anim.add_animation(
        FlickerAnimation(6, 0.3)
    )

    fireplace = world.entity()
    fireplace.add_components(
        Positionable(0, 0, 100, 100),
        Renderable(
            lambda brush: brush.draw_image("fireplace.png"),
            1
        ),
        TilePositionable("ground", (8, 1), 1),
        Lightable(
            Positionable(-130, 60, 360, 120),
            Positionable(-230, 30, 560, 200),
            (205, 155, 29, 64)
        ),
        fireplace_anim
    )

    scenario_state["fireplace"] = fireplace
Exemplo n.º 10
0
def get_text_positionable(text, font_size, x=0, y=0, font_type=None):
    """ Use pygame to compute the width and height of a text entity """
    font = pygame.font.Font(font_type, font_size)
    pygame_rect = font.render(text, False, (255, 0, 0)).get_rect().move(x, y)
    return Positionable(pygame_rect.left, pygame_rect.top, pygame_rect.width,
                        pygame_rect.height)
Exemplo n.º 11
0
def create_room(
        world,
        sound_system,
        outer_positionable=Positionable(0, 50, 600, 500),
        inner_positionable=Positionable(100, 100, 500, 400),
        outer_resolution=(50, 50),
        inner_resolution=(50, 50),
        ground_sprite = "basic_ground_tile.png",
        wall_sprite = "wall_tile",
        corner_sprite = "corner_tile"
):
    """ Create the entities of the room
    outer_positionable: describes the area of the room, walls included
    inner_positionable: describes the area of the ground
    outer_resolution: tile resolution of the walls
    inner_resolution: tile resolution of the ground
    """

    # ground
    tile_ground = world.entity()
    tile_ground.add_components(
        inner_positionable,
        TileSpace("ground", inner_resolution)
    )

    w, h = inner_resolution

    for i in range(inner_positionable.width / w):
        for j in range(inner_positionable.height / h):
            e = world.entity()
            e.add_components(
                Positionable(0, 0, w, h),
                Renderable(
                    lambda brush: brush.draw_image(ground_sprite, (0, 50)),
                    0
                ),
                TilePositionable("ground", (i, j), 0)
            )

    # wall
    tile_wall = world.entity()
    tile_wall.add_components(
        outer_positionable,
        TileSpace("wall", outer_resolution)
    )
    w, h = outer_resolution
    w_max = outer_positionable.width / w
    h_max = outer_positionable.height / h
    # corners
    tl = world.entity()
    tl.add_components(
        Positionable(0, 0, 2*w, 2*h),
        Renderable(
            lambda brush: brush.draw_image(corner_sprite+"_tl.png"),
            0
        ),
        TilePositionable("wall", (0, 1), -10)
    )
    tr = world.entity()
    tr.add_components(
        Positionable(0, 0, 2*w, 2*h),
        Renderable(
            lambda brush: brush.draw_image(corner_sprite+"_tr.png"),
            0
        ),
        TilePositionable("wall", (w_max, 1), -10)
    )
    bl = world.entity()
    bl.add_components(
        Positionable(0, 0, 2*w, 2*h),
        Renderable(
            lambda brush: brush.draw_image(corner_sprite+"_bl.png"),
            0
        ),
        TilePositionable("wall", (0, h_max), -10)
    )
    br = world.entity()
    br.add_components(
        Positionable(0, 0, 2*w, 2*h),
        Renderable(
            lambda brush: brush.draw_image(corner_sprite+"_br.png"),
            0
        ),
        TilePositionable("wall", (w_max, h_max), -10)
    )

    # walls
    for i in range(2, w_max):
        t = world.entity()
        t.add_components(
            Positionable(0, 0, w, 2*h),
            Renderable(
                lambda brush: brush.draw_image(wall_sprite+"_t.png"),
                0
            ),
            TilePositionable("wall", (i, 1), -10)
        )
        b = world.entity()
        b.add_components(
            Positionable(0, 0, w, 2*h),
            Renderable(
                lambda brush: brush.draw_image(wall_sprite+"_b.png"),
                0
            ),
            TilePositionable("wall", (i, h_max), -10)
        )

    for j in range(2, h_max):
        l = world.entity()
        l.add_components(
            Positionable(0, 0, 2*w, h),
            Renderable(
                lambda brush: brush.draw_image(wall_sprite+"_l.png"),
                0
            ),
            TilePositionable("wall", (0, j), -10)
        )
        r = world.entity()
        r.add_components(
            Positionable(0, 0, 2*w, h),
            Renderable(
                lambda brush: brush.draw_image(wall_sprite+"_r.png"),
                0
            ),
            TilePositionable("wall", (w_max, j), -10)
        )

    # furniture
    table = world.entity()
    table.add_components(
        Positionable(0, 0, 150, 100),
        Renderable(
            lambda brush: brush.draw_image("table_textured.png"),
            1
        ),
        TilePositionable("ground", (1, 5), 1)
    )

    stool_animable = Animable()
    stool_toggled = [False]

    def toggle_stool():
        if not stool_animable.animations:
            stool_animable.add_animation(
                TileMoveAnimation(
                    (0, 0.5) if stool_toggled[0] else (0, -0.5),
                    0.2
                )
            )
            stool_toggled[0] = not stool_toggled[0]
            sound_system.play("furniture-short")

    stool = world.entity()
    stool.add_components(
        Positionable(0, 0, 40, 40),
        Renderable(
            lambda brush: brush.draw_image("stool.png"),
            1
        ),
        TilePositionable("ground", (2, 6), 1),
        stool_animable,
        Clickable(
            toggle_stool,
            Button.LEFT
        )
    )
    add_cursor_change_hoverable(stool)