简体   繁体   中英

I don't know how to show a list of classes for user to choose from in my game?

I'm making a text-adventure game called Glaux. This game is an RPG game, but it's on text. my problem is how to show the user 3 classes that he can choose from by typing an option but I don't know how.(code provided below).

import random
class Warrior:
    def __init__(self, name, health, steel_armor, healing_potion, attack, rage):
        self.name = input(print("Enter your name, Warrior!"))
        self.health = health
        self.steel_armor = steel_armor
        self.healing_potion = healing_potion
        self.attack = attack
        self.rage = rage
class Mage:
    def __init__(self, name, health_magic, magic_armor, magic_heal, magic_attack, mana):
        self.name = input(print("Enter your name, Mage!"))
        self.health = health_magic
        self.magic_armor = magic_armor
        self.magic_heal = magic_heal
        self.magic_attack = magic_attack
        self.mana = mana
class Archer:
    def __init__(self, name, health_spirit, elevens_armor, spiritual_heal, spiritual_attack, spirit):
        self.name = input(print("Enter your name, Archer!"))
        self.health = health_spirit
        self.elevens_armor = elevens_armor
        self.spiritual_heal = spiritual_heal
        self.spiritual_attack = spiritual_attack
        self.spirit = spirit
def Classes(Warrior, Mage, Archer):
        Classes = (Warrior, Mage, Archer)
Play = input

run = True


main_menu = True
while run:
    while main_menu:
        question = input("To play enter [Play]: ")
        if question != 'Play':
                print("Please enter a valid option")
                continue
        else:
            break
    print("you were a....."), # here i want to begin the part after main_menu the game will ask the user to choose between 3 classes so how to show the user those 3 classes ?
    print("Choose your class!")
    input(Classes('Warrior', 'Mage', 'Archer'))

    break

You can do it like that:

You define a method Classes that is doing some weird stuff which I don't know what your intention was but you should change:

def Classes(Warrior, Mage, Archer):
    Classes = (Warrior, Mage, Archer)

into something like this:

classes = ["Warrior", "Mage", "Archer"]

A list with all available classes.

After that you can let the player choose his class like this:

player_class = input(classes)
    while player_class not in classes:
        player_class = input(classes)
    print("You are: {}".format(player_class))

You should probably make the output a bit more beautiful but it works with this.

Output:

To play enter [Play]: Play
Choose your class!
['Warrior', 'Mage', 'Archer']Warrior
You are: Warrior

Add the following function

def get_input(*names):
    while True:
        try:
            for i, name in enumerate(names, 1):
                print('[{:>2}] {}'.format(i, name))
            choice = int(input()) - 1
            if choice >= 0 and choice < len(names):
                return names[choice]
        except Exception:
            print('Bad choice, try again...')

Use it like this - get_input('Warrior', 'Mage', 'Archer')

Output example:

To play enter [Play]: Play
you were a.....
Choose your class!
[ 1] Warrior
[ 2] Mage
[ 3] Archer
4
[ 1] Warrior
[ 2] Mage
[ 3] Archer

Bad choice, try again...
[ 1] Warrior
[ 2] Mage
[ 3] Archer
dd
Bad choice, try again...
[ 1] Warrior
[ 2] Mage
[ 3] Archer
fdsg
Bad choice, try again...
[ 1] Warrior
[ 2] Mage
[ 3] Archer
1
Warrior

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM