예제 #1
0
    def _left_click(self, event):
        # Invariant: (event.x, event.y) == self._target_position
        #  => Due to mouse move setting target position to cursor
        x, y = self._target_position
        mobs = self._world.get_mobs(x, y, 2.0)
        for mob in mobs:
            if mob.get_id() is "friendly_sheep":
                print('Dropped block, wool')
                physical = DroppedItem(create_item("wool"))

                # this is so bleh
                x0 = x - BLOCK_SIZE // 2 + 5 + 11 + random.randint(0, 2)
                y0 = y - BLOCK_SIZE // 2 + 5 + 11 + random.randint(0, 2)

                self._world.add_item(physical, x0, y0)
            elif mob.get_id() is "foe_bee":
                print(f"{self._player} attack a bee,damage 1 hit")
                mob.attack(True)
                self._player.change_health(-1)
                if self._player.get_health() <= 0:
                    self._restart()
                if mob.is_dead:
                    print("A bee is deaded")
                    self._world.remove_mob(mob)

        target = self._world.get_thing(x, y)
        if not target:
            return

        if self._target_in_range:
            block = self._world.get_block(x, y)
            if block:
                self.mine_block(block, x, y)
예제 #2
0
    def _handle_player_collide_item(self, player: Player,
                                    dropped_item: DroppedItem, data,
                                    arbiter: pymunk.Arbiter):
        """Callback to handle collision between the player and a (dropped) item. If the player has sufficient space in
        their to pick up the item, the item will be removed from the game world.

        Parameters:
            player (Player): The player that was involved in the collision
            dropped_item (DroppedItem): The (dropped) item that the player collided with
            data (dict): data that was added with this collision handler (see data parameter in
                         World.add_collision_handler)
            arbiter (pymunk.Arbiter): Data about a collision
                                      (see http://www.pymunk.org/en/latest/pymunk.html#pymunk.Arbiter)
                                      NOTE: you probably won't need this
        Return:
             bool: False (always ignore this type of collision)
                   (more generally, collision callbacks return True iff the collision should be considered valid; i.e.
                   returning False makes the world ignore the collision)
        """

        item = dropped_item.get_item()

        if self._hot_bar.add_item(item):
            print(f"Added 1 {item!r} to the hotbar")
        elif self._inventory.add_item(item):
            print(f"Added 1 {item!r} to the inventory")
        else:
            print(f"Found 1 {item!r}, but both hotbar & inventory are full")
            return True

        self._world.remove_item(dropped_item)
        return False
예제 #3
0
    def mine_block(self, block, x, y):
        luck = random.random()

        active_item, effective_item = self.get_holding()

        was_item_suitable, was_attack_successful = block.mine(
            effective_item, active_item, luck)

        effective_item.attack(was_attack_successful)

        if block.is_mined():
            # Task 1.2 Mouse Controls: Reduce the player's food/health appropriately
            if self._player.get_food() > 0:
                self._player.change_health(0)
                self._player.change_food(-2)
                self._status_view.set_food_value(self._player.get_food())

            elif self._player.get_food() == 0:
                self._player.change_food(0)
                self._player.change_health(-2)
                self._status_view.set_health_value(self._player.get_health())

            if self._player.is_dead():
                result = tk.messagebox.askquestion(
                    title="You are dead!", message="Do you want to try again?")
                if (result == "yes"):
                    self._reset()
                else:
                    self._master.destroy()

            # ...

            # Task 1.2 Mouse Controls: Remove the block from the world & get its drops
            # ...
            self._world.remove_block(block)
            drops = block.get_drops(luck, was_item_suitable)

            if not drops:
                return

            x0, y0 = block.get_position()

            for i, (drop_category, drop_types) in enumerate(drops):
                print(f'Dropped {drop_category}, {drop_types}')

                if drop_category == "item":
                    physical = DroppedItem(create_item(*drop_types))

                    # this is so bleh
                    x = x0 - BLOCK_SIZE // 2 + 5 + (
                        i % 3) * 11 + random.randint(0, 2)
                    y = y0 - BLOCK_SIZE // 2 + 5 + (
                        (i // 3) % 3) * 11 + random.randint(0, 2)

                    self._world.add_item(physical, x, y)
                elif drop_category == "block":
                    self._world.add_block(create_block(*drop_types), x, y)
                else:
                    raise KeyError(f"Unknown drop category {drop_category}")
예제 #4
0
    def mine_block(self, block, x, y):
        luck = random.random()

        active_item, effective_item = self.get_holding()

        was_item_suitable, was_attack_successful = block.mine(
            effective_item, active_item, luck)

        effective_item.attack(was_attack_successful)

        if block.is_mined():
            # Task 1.2 Mouse Controls: Reduce the player's food/health appropriately
            # ...
            if self._player.get_food() > 0:
                self._player.change_food(-0.9)
            else:
                self._player.change_health(-0.5)

            self._world.remove_block(block)

            if block.get_id() == 'hive':
                block_x, block_y = block.get_position()
                for i in range(5):
                    self._world.add_mob(Bee(f"killer_bee{i}", (10, 10)),
                                        block_x, block_y)

            # Task 1.2 Mouse Controls: Get what the block drops.
            # ...
            drops = block.get_drops(luck, was_item_suitable)

            if not drops:
                return

            x0, y0 = block.get_position()

            for i, (drop_category, drop_types) in enumerate(drops):
                print(f'Dropped {drop_category}, {drop_types}')

                if drop_category == "item":
                    physical = DroppedItem(create_item(*drop_types))

                    # this is so bleh
                    x = x0 - BLOCK_SIZE // 2 + 5 + (
                        i % 3) * 11 + random.randint(0, 2)
                    y = y0 - BLOCK_SIZE // 2 + 5 + (
                        (i // 3) % 3) * 11 + random.randint(0, 2)

                    self._world.add_item(physical, x, y)
                elif drop_category == "block":
                    self._world.add_block(create_block(*drop_types), x, y)
                else:
                    raise KeyError(f"Unknown drop category {drop_category}")
예제 #5
0
    def mine_block(self, block, x, y):
        luck = random.random()

        active_item, effective_item = self.get_holding()

        was_item_suitable, was_attack_successful = block.mine(
            effective_item, active_item, luck)

        effective_item.attack(was_attack_successful)
        # if the block has been mined

        if block.is_mined():
            # Task 1.2 Mouse Controls: Reduce the player's food/health appropriately
            # ...
            if self._player.get_food() > 0:
                self._player.change_food(-0.5)
            else:
                self._player.change_health(-2.5)

            # Task 1.2 Mouse Controls: Remove the block from the world & get its drops
            # ...
            self._world.remove_item(block)
            if luck < 1:
                drops = block.get_drops(luck, was_item_suitable)
            # Have a look at the World class for removing
            # Have a look at the Block class for getting the drops

            if not drops:
                return

            x0, y0 = block.get_position()

            for i, (drop_category, drop_types) in enumerate(drops):
                print(f'Dropped {drop_category}, {drop_types}')

                if drop_category == "item":
                    physical = DroppedItem(create_item(*drop_types))

                    # this is so bleh
                    x = x0 - BLOCK_SIZE // 2 + 5 + (
                        i % 3) * 11 + random.randint(0, 2)
                    y = y0 - BLOCK_SIZE // 2 + 5 + (
                        (i // 3) % 3) * 11 + random.randint(0, 2)

                    self._world.add_item(physical, x, y)
                elif drop_category == "block":
                    self._world.add_block(create_block(*drop_types), x, y)
                else:
                    raise KeyError(f"Unknown drop category {drop_category}")
예제 #6
0
    def mine_block(self, block, x, y):
        luck = random.random()

        active_item, effective_item = self.get_holding()

        was_item_suitable, was_attack_successful = block.mine(
            effective_item, active_item, luck)

        effective_item.attack(was_attack_successful)

        if block.is_mined():
            print(block)
            # Task 1.2 Mouse Controls: Reduce the player's food/health appropriately
            if self._player.get_food() > 0:
                self._player.change_food(-1)
            else:
                self._player.change_health(-1)
                if self._player.get_health() <= 0:
                    self._restart()

            # Task 1.2 Mouse Controls: Remove the block from the world & get its drops
            self._world.remove_block(block)

            drops = block.get_drops(random.random(), True)
            if not drops:
                return

            x0, y0 = block.get_position()

            for i, (drop_category, drop_types) in enumerate(drops):
                print(f'Dropped {drop_category}, {drop_types}')

                if drop_category == "item":
                    physical = DroppedItem(create_item(*drop_types))

                    # this is so bleh
                    x = x0 - BLOCK_SIZE // 2 + 5 + (
                        i % 3) * 11 + random.randint(0, 2)
                    y = y0 - BLOCK_SIZE // 2 + 5 + (
                        (i // 3) % 3) * 11 + random.randint(0, 2)

                    self._world.add_item(physical, x, y)
                elif drop_category == "block":
                    self._world.add_block(create_block(*drop_types), x, y)
                elif drop_category == "mob":
                    self._world.add_mob(Bee("foe_bee", (8, 8)), x, y)
                else:
                    raise KeyError(f"Unknown drop category {drop_category}")
예제 #7
0
    def mine_block(self, block, x, y):
        """Mines a block, removing it from the game and dropping one or more items"""

        luck = random.random()

        active_item, effective_item = self.get_holding()

        was_item_suitable, was_attack_successful = block.mine(
            effective_item, active_item, luck)

        effective_item.attack(was_attack_successful)

        if block.is_mined():
            # Task 1.2 Mouse Controls: Reduce the player's food/health appropriately
            if self._player.get_food() > 0:
                self._player.change_food(-1)
                print("Food now at", self._player.get_food())
            else:
                self._player.change_health(-0.5)
                print("Health now at", self._player.get_health)

            # Task 1.2 Mouse Controls: Remove the block from the world & get its drops
            drops = block.get_drops(luck, was_item_suitable)
            self._world.remove_block(block)

            if not drops:
                return

            x0, y0 = block.get_position()

            for i, (drop_category, drop_types) in enumerate(drops):
                print(f'Dropped {drop_category}, {drop_types}')

                if drop_category == "item":
                    physical = DroppedItem(create_item(*drop_types))

                    # this is so bleh
                    x = x0 - BLOCK_SIZE // 2 + 5 + (
                        i % 3) * 11 + random.randint(0, 2)
                    y = y0 - BLOCK_SIZE // 2 + 5 + (
                        (i // 3) % 3) * 11 + random.randint(0, 2)

                    self._world.add_item(physical, x, y)
                elif drop_category == "block":
                    self._world.add_block(create_block(*drop_types), x, y)
                else:
                    raise KeyError(f"Unknown drop category {drop_category}")
예제 #8
0
    def mine_block(self, block, x, y):
        luck = random.random()

        active_item, effective_item = self.get_holding()

        was_item_suitable, was_attack_successful = block.mine(
            effective_item, active_item, luck)

        effective_item.attack(was_attack_successful)

        if block.is_mined():
            # Task 1.2 Mouse Controls: Reduce the player's food/health appropriately
            #          You may select what you believe is an appropriate amount by
            #          which to reduce the food or health.
            # ...

            self._world.remove_block(block)
            # Task 1.2 Mouse Controls: Get what the block drops.
            # ...

            if not block.get_drops(random.randrange(0, 10, 1) / 10, True):
                # luck, break_result
                return

            x0, y0 = block.get_position()

            for i, (drop_category, drop_types) in enumerate(
                    block.get_drops(random.randrange(0, 10, 1) / 10, True)):
                print(f'Dropped {drop_category}, {drop_types}')

                if drop_category == "item":
                    physical = DroppedItem(create_item(*drop_types))

                    # TODO: this is so bleh
                    x = x0 - BLOCK_SIZE // 2 + 5 + (
                        i % 3) * 11 + random.randint(0, 2)
                    y = y0 - BLOCK_SIZE // 2 + 5 + (
                        (i // 3) % 3) * 11 + random.randint(0, 2)

                    self._world.add_item(physical, x, y)
                elif drop_category == "block":
                    self._world.add_block(create_block(*drop_types), x, y)
                else:
                    raise KeyError(f"Unknown drop category {drop_category}")
예제 #9
0
    def attack(self, mob, x, y):
        """Method to attack mobs encountered in the world"""
        luck = random.random()

        active_item, effective_item = self.get_holding()

        was_attack_successful = mob.attacked(effective_item, luck)

        if was_attack_successful:

            # Get damage from TooLItem
            damage = effective_item.attack(True)
            if not damage:  # HandItem returns None, so make that 0 damage
                damage = 0

            # Deal damage to the mob, will return any dropped items
            drops = mob.take_damage(luck, damage)

            if not drops:
                return

            x0, y0 = mob.get_position()

            for i, (drop_category, drop_types) in enumerate(drops):
                print(f'Dropped {drop_category}, {drop_types}')

                if drop_category == "item":
                    physical = DroppedItem(create_item(*drop_types))

                    # this is so bleh
                    x = x0 - BLOCK_SIZE // 2 + 5 + (
                        i % 3) * 11 + random.randint(0, 2)
                    y = y0 - BLOCK_SIZE // 2 + 5 + (
                        (i // 3) % 3) * 11 + random.randint(0, 2)

                    self._world.add_item(physical, x, y)
                elif drop_category == "block":
                    self._world.add_block(create_block(*drop_types), x, y)
                else:
                    raise KeyError(f"Unknown drop category {drop_category}")