예제 #1
0
 def get_wall_map_layer(self):
     """Creates a new :py:class:`GridMapLayer` to hold walls.
     """
     return GridMapLayer(
         name="walls",
         columns=self.columns,
         rows=self.rows,
         pixel_width=self.columns * self.tile_size,
         pixel_height=self.rows * self.tile_size,
         sprite_filename=resolve_resource_path("images/box.png"),
         sprite_class=Wall,
     )
예제 #2
0
    def setup_maps(self):
        """Sets up the map.

        Uses a :py:class:`TiledMap` to load the map from a ``.tmx`` file,
        created using :doc:`Tiled <tiled:manual/introduction>`. 
        """
        super().setup_maps()
        sprite_classes = {
            "Obstacles": Wall,
            "Background": Background,
        }
        island_map = TiledMap(resolve_resource_path("images/island/island.tmx"), sprite_classes)
        self.add_map(island_map)
예제 #3
0
class IslandAdventureDiscrete(QuestGame):
    """An alternate version of Island Adventure, using discrete movement.

    To run this example::

        $ python -m quest.examples.island_discrete

    """
    player_sprite_image = resolve_resource_path("images/boy_simple.png")
    player_initial_x = 300
    player_initial_y = 300
    player_speed = 6

    def setup_maps(self):
        """Sets up the map.

        Uses a :py:class:`TiledMap` to load the map from a ``.tmx`` file,
        created using :doc:`Tiled <tiled:manual/introduction>`.
        """
        super().setup_maps()
        sprite_classes = {
            "Obstacles": Wall,
            "Background": Background,
        }
        self.add_map(
            TiledMap(resolve_resource_path("images/island/island.tmx"),
                     sprite_classes))
        layer = GridMapLayer("grid", 40, 40, 40 * 32, 40 * 32)
        self.get_current_map().add_layer(layer)

    def setup_walls(self):
        """Assigns sprites to `self.wall_list`. These sprites will function as walls, blocking
        the player from passing through them.
        """
        self.wall_list = self.get_current_map().get_layer_by_name(
            "Obstacles").sprite_list

    def setup_physics_engine(self):
        """Uses :py:class:`DiscretePhysicsEngine` instead of the standard :py:class:`ContinuousPhysicsEngine`.
        The result is that the player snaps to a grid instead of moving smoothly to any position.

        A game's physics engine is responsible for enforcing the rules of the game's reality.
        In a fancy 3d game, the physics engine is full of intense math to keep track of objects
        flying around, bouncing off of walls, and breaking into pieces.

        Quest's physics engines are simpler. They just need to make sure nobody walks through walls,
        and to check when sprites collide. There are two physics engines built-in: A continuous (the
        default) and a discrete (used here).
        """
        grid = self.get_current_map().get_layer_by_name('grid')
        self.physics_engine = DiscretePhysicsEngine(self, grid)
예제 #4
0
    def setup_npcs(self):
        """Creates and places Grandma and the vegetables.
        """
        static_npc_data = [
            [Carrots, "images/items/carrots.png", 1, 220, 640],
            [Mushroom, "images/items/mushroom.png", 1, 1028, 264],
            [Potatoes, "images/items/potatoes.png", 1, 959, 991],
            [Tomatos, "images/items/tomatos.png", 1, 323, 1055],
        ]
        self.npc_list = arcade.SpriteList()
        grandma_image = resolve_resource_path("images/people/grandma.png")
        grandma = DirectionalGrandma(grandma_image, grandma_image, grandma_image, 3)
        grandma.center_x = 400
        grandma.center_y = 400
        self.npc_list.append(grandma)
        for sprite_class, image, scale, x, y in static_npc_data:
            sprite = sprite_class(resolve_resource_path(image), scale)
            sprite.center_x = x
            sprite.center_y = y
            self.npc_list.append(sprite)

        grandma = self.npc_list[0]
        walk = RandomWalk(0.05)
        grandma.strategy = walk
예제 #5
0
    def setup_maps(self):
        """Sets up the map.

        Uses a :py:class:`TiledMap` to load the map from a ``.tmx`` file,
        created using :doc:`Tiled <tiled:manual/introduction>`.
        """
        super().setup_maps()
        sprite_classes = {
            "Obstacles": Wall,
            "Background": Background,
        }
        self.add_map(
            TiledMap(resolve_resource_path("images/island/island.tmx"),
                     sprite_classes))
        layer = GridMapLayer("grid", 40, 40, 40 * 32, 40 * 32)
        self.get_current_map().add_layer(layer)
예제 #6
0
    def setup_npcs(self):
        """Creates and places Grandma and the vegetables.
        """
        super().setup_npcs()
        npc_data = [
            [Carrots, "images/items/carrots.png", 1, 220, 640],
            [Mushroom, "images/items/mushroom.png", 1, 1028, 264],
            [Grandma, "images/people/grandma.png", 3, 400, 400],
        ]
        for sprite_class, image, scale, x, y in npc_data:
            sprite = sprite_class(resolve_resource_path(image), scale)
            sprite.center_x = x
            sprite.center_y = y
            self.npc_list.append(sprite)

        grandma = self.npc_list[-1]
        grandma.strategy = RandomWalk(0.05) 
예제 #7
0
class DirectionalGame(GrandmasSoupGame):
    """Extends the GrandmasSoupGame to have a player and grandmother than face different
    directions depending on the dirtection they are moving.
    """

    player_sprite_image = resolve_resource_path("images/people/trixie.png")
    player_scaling = 3
    def setup_player(self):
        """Creates the player sprite.

        Initializes a sprite for the player, assigns its starting position,
        and appends the player sprite to a SpriteList (Arcade likes to work
        with sprites in SpriteLists).
        """
        self.player = DirectionalPlayer(self.player_sprite_image, self.player_sprite_image, self.player_sprite_image, self.player_scaling)
        self.player.center_x = self.player_initial_x
        self.player.center_y = self.player_initial_y
        self.player.speed = self.player_speed
        self.player_list = arcade.SpriteList()
        self.player_list.append(self.player)

    def setup_npcs(self):
        """Creates and places Grandma and the vegetables.
        """
        static_npc_data = [
            [Carrots, "images/items/carrots.png", 1, 220, 640],
            [Mushroom, "images/items/mushroom.png", 1, 1028, 264],
            [Potatoes, "images/items/potatoes.png", 1, 959, 991],
            [Tomatos, "images/items/tomatos.png", 1, 323, 1055],
        ]
        self.npc_list = arcade.SpriteList()
        grandma_image = resolve_resource_path("images/people/grandma.png")
        grandma = DirectionalGrandma(grandma_image, grandma_image, grandma_image, 3)
        grandma.center_x = 400
        grandma.center_y = 400
        self.npc_list.append(grandma)
        for sprite_class, image, scale, x, y in static_npc_data:
            sprite = sprite_class(resolve_resource_path(image), scale)
            sprite.center_x = x
            sprite.center_y = y
            self.npc_list.append(sprite)

        grandma = self.npc_list[0]
        walk = RandomWalk(0.05)
        grandma.strategy = walk
예제 #8
0
class IslandAdventure(QuestGame):
    """A very simple subclass of :py:class:`QuestGame`.

    To run this example::

        $ python -m quest.examples.island

    :py:class:`IslandAdventure` shows off the basic features of the Quest 
    framework, loading a map and letting the player explore it. 
    After you play it, check out the sorce code by clicking on "source" in the
    blue bar just above.
    """

    player_sprite_image = resolve_resource_path("images/boy_simple.png")
    screen_width = 500
    screen_height = 500
    left_viewport_margin = 96                            
    right_viewport_margin = 96
    bottom_viewport_margin = 96
    top_viewport_margin = 96
    player_initial_x = 300
    player_initial_y = 300
    player_speed = 6

    def setup_maps(self):
        """Sets up the map.

        Uses a :py:class:`TiledMap` to load the map from a ``.tmx`` file,
        created using :doc:`Tiled <tiled:manual/introduction>`. 
        """
        super().setup_maps()
        sprite_classes = {
            "Obstacles": Wall,
            "Background": Background,
        }
        island_map = TiledMap(resolve_resource_path("images/island/island.tmx"), sprite_classes)
        self.add_map(island_map)

    def setup_walls(self):
        """Assigns sprites to `self.wall_list`. These sprites will function as walls, blocking
        the player from passing through them.
        """
        self.wall_list = self.get_current_map().get_layer_by_name("Obstacles").sprite_list
예제 #9
0
class MazeGame(QuestGame):
    """Get all the stars as fast as you can! My record is 45 seconds.

    :py:class:`MazeGame` is an example of how you can make a fairly complex 
    game without making too many changes. Because :py:class:`MazeGame` is a 
    subclass of :py:class:`QuestGame`, we just need to change the parts we 
    want to work differently. We need to set some of the :py:class:`MazeGame` 
    properties and override a few of the class methods. 

    To run this example::

        $ python -m quest.examples.maze

    After you play it, check out the sorce code by clicking on "source" in the
    blue bar just above.


    Attributes:
        tile_size=32: Each square tile in the map is 32 pixels across.
        grid_columns=33: The map will have 33 columns of tiles.
        grid_rows=33: The map will have 33 rows of tiles.
        player_sprite_image="images/boy_simple.png": The sprite's image file.
        player_scaling=0.5: The image is too big, so we scale it down. (We could
            also just resize the image itself.)
        player_initial_x=1.5 * tile_size: By starting the player at 1.5 times `tile_size`, 
            the player will initially be positioned at the center of tile (1, 1).
            The outer edge of the map is walls.
        player_initial_y=1.5 * tile_size: Again, centering the player at (1, 1)
        score=0: Keeps track of how much loot has been collected.
        max_score=25: Total amount of loot to be distributed through the maze.
        game_over=False: Keeps track of whether the game has ended.
    """
    tile_size = 32
    grid_columns = 33
    grid_rows = 33
    player_sprite_image = resolve_resource_path("images/boy_simple.png")
    player_scaling = 0.5
    player_speed = 5
    player_initial_x = 1.5 * tile_size
    player_initial_y = 1.5 * tile_size
    score = 0
    max_score = 25
    game_over = False

    def __init__(self):
        super().__init__()
        self.level_start = datetime.now()

    def setup_maps(self):
        """Creates a :py:class:`MazeMap` (see below) and adds it to game's list of maps.
        """
        super().setup_maps()
        maze_map = MazeMap(self.grid_columns, self.grid_rows, self.tile_size,
                           self.max_score)
        self.add_map(maze_map)

    def setup_walls(self):
        """Assigns `self.wall_list` to be all the sprites in the map's "walls" layer.
        """
        self.wall_list = self.get_current_map().get_layer_by_name(
            "walls").sprite_list

    def setup_npcs(self):
        """Assigns `self.npc_list` to be all the sprites in the map's "loot" layer.
        """
        self.npc_list = self.get_current_map().get_layer_by_name(
            "loot").sprite_list

    def on_loot_collected(self, collector):
        """A method to be called whenever loot is collected.

        See the :py:class:`Loot` NPC sprite below. It calls this method whenever there is
        a collision with the player.
        """
        self.score += 1
        if self.score == self.max_score:
            self.game_over = True
            self.final_elapsed_time = (datetime.now() -
                                       self.level_start).seconds

    def message(self):
        """Returns a string like "Time 12 (12/25)"
        """
        if self.game_over:
            return "You won in {} seconds!".format(self.final_elapsed_time)
        else:
            elapsed_time = datetime.now() - self.level_start
            return "Time {} ({}/{})".format(elapsed_time.seconds, self.score,
                                            self.max_score)
예제 #10
0
 def __init__(self):
     super().__init__()
     self.dialogue = Dialogue.from_ink(resolve_resource_path("grandma.ink"))
     self.modal = DialogueModal(self, self.dialogue)
     self.items = []
예제 #11
0
class GrandmasSoupGame(QuestGame):
    """Help Grandma find all the ingredients for her soup.

    :py:class:`GrandmasSoupGame` shows off the full range of features in the Quest
    framework, loading a map and letting the player explore it. Grandma and the Vegetables
    are subclasses of :py:class:`NPC`. A :py:class:`quest.modal.DialogueModal` is used for the conversations
    with Grandma. To run this example, run::

        $ python -m quest.examples.grandmas_soup

    After you play it, check out the sorce code by clicking on "source" in the
    blue bar just above.

    Attributes:
        dialogue: A :py:class:`Dialogue` containing the game's conversation. 
        modal: A :py:class:`DialogueModal` to show the dialogue.
        items: A list to keep track of which items the player has collected.
    """

    player_sprite_image = resolve_resource_path("images/boy_simple.png")
    screen_width = 500
    screen_height = 500
    left_viewport_margin = 96
    right_viewport_margin = 96
    bottom_viewport_margin = 96
    top_viewport_margin = 96
    player_initial_x = 300
    player_initial_y = 300
    player_speed = 8

    def __init__(self):
        super().__init__()
        self.dialogue = Dialogue.from_ink(resolve_resource_path("grandma.ink"))
        self.modal = DialogueModal(self, self.dialogue)
        self.items = []

    def setup_maps(self):
        """Sets up the standard island map.
        """
        super().setup_maps()
        sprite_classes = {
            "Obstacles": Wall,
            "Background": QuestSprite,
        }
        self.add_map(
            TiledMap(resolve_resource_path("images/island/island.tmx"),
                     sprite_classes))

    def setup_walls(self):
        """As in other examples, assigns all sprites in the "Obstacles" layer to be walls.
        """
        self.wall_list = self.get_current_map().get_layer_by_name(
            "Obstacles").sprite_list

    def setup_npcs(self):
        """Creates and places Grandma and the vegetables.
        """
        npc_data = [
            [Grandma, "images/people/grandma.png", 3, 400, 400],
            [Carrots, "images/items/carrots.png", 1, 220, 640],
            [Mushroom, "images/items/mushroom.png", 1, 1028, 264],
            [Potatoes, "images/items/potatoes.png", 1, 959, 991],
            [Tomatos, "images/items/tomatos.png", 1, 323, 1055],
        ]
        self.npc_list = arcade.SpriteList()
        for sprite_class, image, scale, x, y in npc_data:
            sprite = sprite_class(resolve_resource_path(image), scale)
            sprite.center_x = x
            sprite.center_y = y
            self.npc_list.append(sprite)

        grandma = self.npc_list[0]
        walk = RandomWalk(0.05)
        grandma.strategy = walk

    def talk_with_grandma(self):
        """Opens the dialogue modal to show conversation with Grandma. This is called
        when the player collides with Grandma.
        """
        self.open_modal(self.modal)

    def got_item(self, description):
        """Adds the item's description to the items list. This is called when the 
        player collides with a vegetable.

        Arguments:
            description: The item description.
        """
        self.items.append(description.upper())
        if len(self.items) < 4:
            self.dialogue.run(self.items[-1])
        else:
            self.dialogue.run("SOUP")
예제 #12
0
 def __init__(self):
     super().__init__(resolve_resource_path("images/items/coin.png"), .1)
예제 #13
0
class GrandmasSoupWithInventory(InventoryMixin, QuestGame):
    """Help Grandma find all the ingredients for her soup.

    This version has an inventory, which you can access by pressing 'i'.
    """

    player_sprite_image = resolve_resource_path("images/boy_simple.png")
    player_initial_x = 300
    player_initial_y = 300
    player_speed = 8

    def __init__(self):
        super().__init__()
        self.dialogue = Dialogue.from_ink(resolve_resource_path("grandma.ink"))
        self.modal = DialogueModal(self, self.dialogue)
        self.items = []

    def setup_maps(self):
        """Sets up the standard island map.
        """
        super().setup_maps()
        sprite_classes = {
            "Obstacles": Wall,
            "Background": QuestSprite,
        }
        self.add_map(TiledMap(resolve_resource_path("images/island/island.tmx"), sprite_classes))

    def setup_walls(self):
        """As in other examples, assigns all sprites in the "Obstacles" layer to be walls.
        """
        self.wall_list = self.get_current_map().get_layer_by_name("Obstacles").sprite_list

    def setup_npcs(self):
        """Creates and places Grandma and the vegetables.
        """
        npc_data = [
            [Grandma, "images/people/grandma.png", 3, 400, 400],
            [Carrots, "images/items/carrots.png", 1, 220, 640],
            [Mushroom, "images/items/mushroom.png", 1, 1028, 264],
            [Potatoes, "images/items/potatoes.png", 1, 959, 991],
            [Tomatos, "images/items/tomatos.png", 1, 323, 1055],
        ]
        self.npc_list = arcade.SpriteList()
        for sprite_class, image, scale, x, y in npc_data:
            sprite = sprite_class(resolve_resource_path(image), scale)
            sprite.center_x = x
            sprite.center_y = y
            self.npc_list.append(sprite)

        grandma = self.npc_list[0]
        grandma.strategy = RandomWalk(0.05) 

    def talk_with_grandma(self):
        """Opens the dialogue modal to show conversation with Grandma. This is called
        when the player collides with Grandma.
        """
        item_count = len(self.inventory())
        if item_count == 1:
            self.dialogue.run(self.inventory()[0].description.upper())
        elif item_count == 2:
            self.dialogue.run("TWO")
        elif item_count == 3:
            self.dialogue.run("THREE")
        elif item_count == 4:
            self.dialogue.run("SOUP")
        self.open_modal(self.modal)
예제 #14
0
class GrandmasSoupWithShop(InventoryMixin, ShopMixin, CoinMixin, QuestGame):
    """Help Grandma find all the ingredients for her soup.

    This game demonstrates how to use a shop attribute as part of a game.
    """

    player_sprite_image = resolve_resource_path("images/boy_simple.png")
    player_initial_x = 300
    player_initial_y = 300
    player_speed = 8
    total_coins = 12
    map_dimensions = (1000, 1000)
    money = 0

    def __init__(self):
        super().__init__()
        self.welcome = AlertModal(self, "Press i for inventory and z to enter the shop.")
        self.dialogue = Dialogue.from_ink(resolve_resource_path("grandma.ink"))
        self.modal = DialogueModal(self, self.dialogue)
        self.shop_inventory().append(Tomatos(resolve_resource_path("images/items/tomatos.png"), 1))
        self.shop_inventory().append(Potatoes(resolve_resource_path("images/items/potatoes.png"), 1))
        #self.open_modal(self.welcome)

    def setup_maps(self):
        """Sets up the standard island map.
        """
        super().setup_maps()
        sprite_classes = {
            "Obstacles": Wall,
            "Background": QuestSprite,
        }
        self.add_map(TiledMap(resolve_resource_path("images/island/island.tmx"), sprite_classes))

    def setup_walls(self):
        """As in other examples, assigns all sprites in the "Obstacles" layer to be walls.
        """
        self.wall_list = self.get_current_map().get_layer_by_name("Obstacles").sprite_list

    def setup_npcs(self):
        """Creates and places Grandma and the vegetables.
        """
        super().setup_npcs()
        npc_data = [
            [Carrots, "images/items/carrots.png", 1, 220, 640],
            [Mushroom, "images/items/mushroom.png", 1, 1028, 264],
            [Grandma, "images/people/grandma.png", 3, 400, 400],
        ]
        for sprite_class, image, scale, x, y in npc_data:
            sprite = sprite_class(resolve_resource_path(image), scale)
            sprite.center_x = x
            sprite.center_y = y
            self.npc_list.append(sprite)

        grandma = self.npc_list[-1]
        grandma.strategy = RandomWalk(0.05) 

    def talk_with_grandma(self):
        """Opens the dialogue modal to show conversation with Grandma. This is called
        when the player collides with Grandma.
        """
        item_count = len(self.inventory())
        if item_count == 1:
            self.dialogue.run(self.inventory()[0].description.upper())
        elif item_count == 2:
            self.dialogue.run("TWO")
        elif item_count == 3:
            self.dialogue.run("THREE")
        elif item_count == 4:
            self.dialogue.run("SOUP")
        self.open_modal(self.modal)

    def got_coin(self):
        self.money += 1

    def message(self):
        return "${}".format(self.money)