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)
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}")
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}")
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}")
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}")
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}")
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}")
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}")