Python:游戏菜单选项提示出ord

2024-03-29 15:42:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在创建我的第一个“大”python项目。在游戏中,你是一个巫师,必须使用异能与怪物战斗。如果你的生命值达到0,你就输了。如果怪物的生命值达到0,你就赢了。你知道吗

我的问题是:
我创建了一个菜单,可以用1,2,3和4键操作。 代码如下:

menu = int(input('''
             Press 1 to battle!
             Press 2 to get to the shop
             Press 3 to get to the upgrades
             Press 4 to exit the game'''))
buying = int(input('''
               (1)Buy an apple!
               (2)Buy a cake!
               (3)Buy a MedPack!'''))

if menu == 1:
   print("You can use your Abilities with A, B and C")
   print(" ")
   while health_player >= 0:
       spells = input("Press a for an attack, b for a fire ball or C for a Thunder!"
       if spells == "A":
           damage_monster = random.randrange(10, 30)
           health_monster -= damage_monster
           print("The monster lost", damage_monster, "HP and still has", health_monster, "/200 left.")
           damage = random.randrange(10, 30)
           health_player -= damage
           print("You lost", damage, "HP and still have", health_player, "/200 left.")
           print(" ")

。。。然后是其他菜单选项:

elif menu == 2:
    print('''
    -----------------------
    Welcome to the shop!
    Buy an apple to get a part of your health back!
    ''')

elif menu == 3:
    pass

elif menu == 4:
    sys.exit()

但是,菜单选项选择没有按预期工作。即使我按1、3或4,商店也总是来。我怎样才能解决这个问题?你知道吗

希望你能理解和帮助我。:)

提前谢谢!你知道吗


Tags: andthetoaninputget菜单buy
1条回答
网友
1楼 · 发布于 2024-03-29 15:42:33

编辑:

答案完全重新写在新的信息和代码的光。你知道吗

如果我正确理解您的问题:
-如果我没有从菜单中选择“2”,为什么会提示我买东西?

这是因为'buying'input提示符紧跟在'menu'input提示符之后。如下所示。因为这个游戏是用脚本编写的,所以代码从上到下运行。你知道吗

menu = int(input('''
             Press 1 to battle!
             Press 2 to get to the shop
             Press 3 to get to the upgrades
             Press 4 to exit the game'''))
buying = int(input('''
               (1)Buy an apple!
               (2)Buy a cake!
               (3)Buy a MedPack!'''))

我建议您将“购买”提示移到elif menu == 2块中。你知道吗

如图所示:

if menu == 1:
    # Do battle stuff ...
elif menu == 2:
    print('''
               -
    Welcome to the shop!
    Buy an apple to get a part of your health back!
    ''')
    # Move your 'buying' prompt here:
    buying = int(input('''
                   (1)Buy an apple!
                   (2)Buy a cake!
                   (3)Buy a MedPack!'''))
elif menu == 3:
    # Do menu 3 stuff ...
elif menu == 4:
    sys.exit()

我已经测试了这个逻辑,它似乎工作正常。试一试。。。你知道吗

建议:

1)看看string formatting。这将给你一些权力和控制你的屏幕上的提示。你知道吗

2)考虑用^{}来编写游戏,或者如果你想更进一步,^{},而不是作为脚本。这将给你更多的灵活性。试试看!你知道吗

相关问题 更多 >