0

Yes, I have looked at other posts but I am still a bit confused, some use lambda or make multiple methods and I am confused. I have this class, and I want to create multiple instances (members of the team) and before I call my function to start a fight, I want to arrange a list so that the person with the highest self.full_speed goes first, etc. (I have a full_speed and speed attributes for debuffs/buffs)


class Player:
    """Describes the main player."""
    def __init__(self, level, health, will, speed):
        """Initializes stats"""
        self.level = level
        self.health = health
        self.full_health = health
        self.will = will
        self.full_will = will
        self._cheat = "cheat" # Protected instance attribute has a leading underscore
        # Private starts with two leading underscores
        self.speed = speed
        self.full_speed = speed

    def level_up(self, skill):
        """Choose where to distribute skill points."""
        pass

    def default_attack(self, enemy):
        """Normal attack that damages enemy target."""
        damage = 0
        if random.randint(1,100) <= 95:
            damage = (self.level * 2)
            critical_hit = random.randint(1,10)
            if critical_hit == 1:
                damage += int(self.level * 0.5)
                print("Critical hit!")
        enemy.health -= damage
        print("The enemy took " + str(damage) + " damage.")

    def special_attack(self, enemy):
        """Deals more damage but uses will, more likely to miss."""
        damage = 0
        if random.randint(1,100) <= 90:
            damage = (self.level * 3)
            critical_hit = random.randint(1, 10)
            if critical_hit == 1:
                damage += self.level
                print("Critical hit!")
        enemy.health -= damage
        self.will -= 2
        print("The enemy took " + str(damage) + " damage.")

    def heal(self):
        """Heals self by 10%."""
        self.will -= 2
        recovered = int(self.full_health * 0.10)
        self.health += recovered
        if self.health > self.full_health:
            self.health = self.full_health
        print("Recovered " + str(recovered) + " HP.")

I've started here but I am unsure where to go at this point..

def team_by_speed(team):
    new_team = []
    for member in team:
        # member.full_speed
    return new_team
ewe
  • 159
  • 1
  • 7
  • 1
    Does this answer your question? [Python - How to sort a list, by an attribute from another class](https://stackoverflow.com/questions/4648608/python-how-to-sort-a-list-by-an-attribute-from-another-class) – bhristov Jul 12 '20 at 04:52
  • Use the code in the dupe with `reverse=True` for sorting by descending order. – Austin Jul 12 '20 at 04:55

1 Answers1

-2

Used Sorted() function Call sorted(iterable, key: NoneType=None) with a list of objects as iterable