def __init__(self, n_actor_count: int): Actor.__init__(self, n_actor_count) self.name += "_Wizard" self.magic = randint(15, self.unit_max()) self.has_staff = True self.will = randint(1, 10) self.intelligence = randint(10, self.unit_max()) self.image_path = 'images/wizard.png'
def __init__(self, n_actor_count: int): Actor.__init__(self, n_actor_count) self.name += "_Hobbit" self.stealth = randint(1, self.unit_max()) self.has_dagger = False self.dependence = randint(1, self.unit_max()) self.fear = randint(1, self.unit_max()) self.runs = 0 self.image_path = 'images/hobbit.png'
def __init__(self, actor_json, battle_object, portrait=None): Actor.__init__(self, object_json=actor_json) self.battle_object = battle_object if not portrait: self.portrait = pygame.image.load(os.path.join("..", "assets", "portraits", "ball_2.gif")).convert_alpha() else: self.portrait = portrait
def __init__(self, n_actor_count: int): Actor.__init__(self, n_actor_count) self.name += "_Orc" self.anger = randint(1, 50) self.size = randint(5, 50) self.location = [ randint(500, self.battlefield_max()), randint(0, self.battlefield_max()) ] self.image_path = 'images/orc.png'
def test_get_distance_between_actors(): actor0 = Actor(0) actor0.location[0] = 0 actor0.location[1] = 2 actor1 = Actor(1) actor1.location[0] = 10 actor1.location[1] = 2 assert actor0.get_distance_from(actor1) == 10
def seppuku(self): if self.location[0] > self.battlefield_max(): Actor.take_damage(self, self.unit_max()) elif self.location[0] < 0: Actor.take_damage(self, self.unit_max()) if self.location[1] > self.battlefield_max(): Actor.take_damage(self, self.unit_max()) elif self.location[1] < 0: Actor.take_damage(self, self.unit_max())
def fight(self, enemy) -> int: damage = 0 if enemy.location == self.location: if enemy.strength > self.strength or enemy.health_points > self.health_points: damage = (self.strength + self.human_pride) else: damage = Actor.fight(self, enemy) return damage
def build_map(self): for layer in self.map_data["layers"]: if layer["visible"]: if layer["type"] == "tilelayer": pos = 0 t_width = self.map_data["tilesets"][0]["tilewidth"] t_height = self.map_data["tilesets"][0]["tileheight"] for i in layer["data"]: if i != 0: y_cord = int(pos / layer["width"]) x_cord = int(pos % layer["width"]) if layer["name"] == "passmap": self.passmap.append( pygame.Rect(x_cord * t_width, y_cord * t_height, t_width, t_height)) else: tile_x = (i - 1) % ( self.map_data["tilesets"][0]["imagewidth"] / t_width) tile_x = tile_x * t_width tile_y = (i - 1) / ( self.map_data["tilesets"][0]["imagewidth"] / t_height) # need to figure out what the -4 is from tile_y = tile_y * t_height - 4 if "properties" in layer \ and "upper" in layer["properties"] \ and layer["properties"]["upper"]: map_to_draw = self.upper_map else: map_to_draw = self.lower_map map_to_draw.blit( self.map_tileset, (x_cord * t_width, y_cord * t_height), area=pygame.Rect( tile_x, tile_y, self.map_data["tilesets"][0] ["tilewidth"], self.map_data["tilesets"][0] ["tileheight"])) pos += 1 elif layer["type"] == "objectgroup": for o in layer["objects"]: if "door" in o["properties"]: self.door_list.append(Door(o)) elif "start" in o["properties"]: self.starting_location = (o['x'], o['y']) elif "ACTOR" in o["properties"]: self.actor_list.append(Actor(o)) else: self.object_list.append( MapObject(o, self.tileset_data))
def move_actor(self, enemy): if self.runs >= self.max_runs( ): # Limit how often a hobbit can run away self.runs = 0 Actor.move_actor(self, enemy) elif self.fear > enemy.strength and self.strength < enemy.health_points and self.dependence > 50: # X Movement if enemy.location[0] > self.location[0]: self.location[0] = self.location[0] - self.speed elif enemy.location[0] < self.location[0]: self.location[0] = self.location[0] + self.speed # Y Movement if enemy.location[1] > self.location[1]: self.location[1] = self.location[1] - self.speed elif enemy.location[1] < self.location[1]: self.location[1] = self.location[1] + self.speed self.runs += 1 else: Actor.move_actor(self, enemy) # Actor.keep_in(self) # If a hobbit runs out off the battlefield they are considered dead to us self.seppuku() enemy.take_damage(Actor.fight(self, enemy))
def test_fight(): actor = Actor(0) actor.strength = 25 enemy = Actor(1) enemy.health_points = enemy.unit_max() actor.location[0] = 50 actor.location[1] = 50 enemy.location[0] = 50 enemy.location[1] = 50 assert actor.fight(enemy) == 25
def test_actor_health_is_within_50_to_100(): actor = Actor(0) assert 50 <= actor.health_points <= actor.unit_max()
def __init__(self, n_actor_count: int): Actor.__init__(self, n_actor_count) self.name += "_Elf" self.bow_strength = randint(15, self.unit_max()) self.range = randint(10, self.unit_max()) self.image_path = 'images/elf.png'
def to_string(self) -> str: return Actor.to_string(self) + "\nBow Strength : " + str( self.bow_strength) + "\nMagic : " + "\nRange : " + str(self.range)
def take_damage(self, damage: int): if damage >= self.armor: Actor.take_damage(self, damage)
def test_actor_location_xy_is_within_0_to_100(): actor = Actor(0) assert 0 <= actor.location[0] <= actor.battlefield_max() assert 0 <= actor.location[1] <= actor.battlefield_max()
def test_actor_speed_is_within_1_to_100(): actor = Actor(0) assert 1 <= actor.speed <= actor.unit_max()
def test_actor_strength_is_within_1_to_100(): actor = Actor(0) assert 1 <= actor.strength <= actor.unit_max()
def test_take_damage(): actor = Actor(0) actor.health_points = 50 actor.take_damage(40) assert actor.health_points == 10
def to_string(self) -> str: return Actor.to_string(self) + "\nStealth : " + str( self.stealth) + "\nHas Dagger : " + str( self.has_dagger) + "\nDependence : " + str( self.dependence) + "\nFear : " + str(self.fear)
def test_fight_from_distance(): actor = Actor(0) enemy = Actor(1) enemy.health_points = enemy.unit_max() actor.location[0] = 50 actor.location[1] = 40 enemy.location[0] = 50 enemy.location[1] = 50 weapon_range = 10 weapon = 25 assert actor.fight_from_distance(enemy, weapon_range, weapon) == 25 weapon_range = 1 assert actor.fight_from_distance(enemy, weapon_range, weapon) == 0
def to_string(self): return Actor.to_string(self) + "\nArmor : " + str( self.armor) + "\nAxe Strength : " + str(self.axe_strength)
def test_to_string(): actor = Actor(25) assert actor.to_string().__contains__("Name : Unit_25")
def __init__(self, n_actor_count: int): Actor.__init__(self, n_actor_count) self.name += "_Dwarf" self.armor = randint(1, 50) self.axe_strength = randint(1, 50) self.image_path = 'images/dwarf.png'
def test_actor_name(): actor = Actor(5) assert actor.name == "Unit_5"
def fight(self, enemy) -> int: damage = Actor.fight_from_distance(self, enemy, self.range, self.bow_strength) return damage
def take_damage(self, damage): if damage >= self.size: Actor.take_damage(self, damage)
def to_string(self): return Actor.to_string( self) + "\nHuman Pride : " + str(self.human_pride)
def test_keep_in(): actor = Actor(0) actor.location[0] = -1 actor.location[1] = actor.battlefield_max() + 1 actor.keep_in() assert actor.location[0] == 0 assert actor.location[1] == actor.battlefield_max() actor.location[0] = actor.battlefield_max() + 1 actor.location[1] = -1 actor.keep_in() assert actor.location[0] == actor.battlefield_max() assert actor.location[1] == 0 actor.location[0] = 50 actor.location[1] = 50 actor.keep_in() assert actor.location[0] == 50 assert actor.location[1] == 50
def __init__(self, n_actor_count: int): Actor.__init__(self, n_actor_count) self.name += "_Human" self.human_pride = randint(1, 50) self.image_path = 'images/human.png'
def test_move_actor(): actor = Actor(0) enemy = Actor(1) actor.location[0] = 20 actor.location[1] = 10 enemy.location[0] = 10 enemy.location[1] = 50 actor.speed = 30 actor.move_actor(enemy) assert actor.location[ 0] == 10 # 20 - 30 = -10 but enemy is at 10 so stop at 10 assert actor.location[1] == 40 # 10 + 30 = 40 actor.location[0] = 100 actor.location[1] = 100 actor.move_actor(enemy) assert actor.location[0] == 70 assert actor.location[1] == 70