Answer to Question #301071 in Python for rtf

Question #301071

o develop a console or GUI turn-based battle game. The game allows player setup his/her team which made up of a numbers of units (default is 3). Each unit has a name, health point (HP), attack point (ATK), defence point (DEF), experience (EXP) and a rank. On top of that, a unit can be either a Warrior or Tanker which having different strength in different range for ATK and DEF point with a flowchart


1
Expert's answer
2022-02-22T08:58:06-0500
class Character:
    def __init__(self, hp=20, atk=5, defense=5, role='Peasant'):
        self.hp = hp
        self.atk = atk
        self.defense = defense
        self.role = role
        print('\nThe character has been created.')
        # self.getStats()

    def setRole(self, role):
        if role == 'warrior':
            self.atk = 10
            self.defense = 5
            self.role = 'Warrior'
            print('You changed role to warrior')
            self.getStats()
        elif role == 'tanker':
            self.atk = 5
            self.defense = 10
            self.role = 'Tanker'
            print('You changed role to tanker')
            self.getStats()
    def getStats(self):
            print('Character stats:')
            print(f'HP {self.hp} / ATK {self.atk} / DEF {self.defense} / ROLE {self.role}')

def commandList():
    print('''
Available commands:

1. Create a character
2. Set role character
3. View list of characters
4. Exit

(Enter number)

        ''')

characters = []
roles = ['Warrior', 'Tanker']
command = ''

while command != 'exit':
    commandList()
    command = input()
    if command == '1':
        char = Character()
        char.getStats()
        characters.append(char)
    elif command == '2':
        if len(characters) != 0:
            print('Which character do you want to set the role to?')
            for i in range(len(characters)):
                print(f'{i+1}.')
                char.getStats()
            num = input()
            if num.isdigit() and int(num) > 0 and int(num) <= len(characters):
                print('What role do you want set?')
                for role in roles:
                    print(role, end='')
                    print(' / ', end='')
                print('\n')
                role = input()
                if role.lower() == 'warrior' or role.lower() == 'tanker':
                    characters[int(num)-1].setRole(role)
                else:
                    print('error')
        else:
            print('\nCharacter list is empty')
    elif command == '3':
        counter = 1
        for c in characters:
            print(f'\n{counter}.')
            c.getStats()
            counter += 1
        if len(characters) == 0:
            print('\nCharacter list is empty')
    elif command == '4':
        break
    else:
        print('error')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog