[英]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.我正在制作一款名为 Glaux 的文字冒险游戏。 This game is an RPG game, but it's on text.
这个游戏是一个RPG游戏,但它是文字。 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).
我的问题是如何向用户显示他可以通过键入一个选项来选择的 3 个类,但我不知道如何。(下面提供了代码)。
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:你定义了一个方法 Classes ,它正在做一些奇怪的事情,我不知道你的意图是什么,但你应该改变:
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.您可能应该使输出更漂亮一点,但它适用于此。
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')
像这样使用它 -
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.