本文介绍了使 D&D 游戏遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习 Python 一段时间了,我决定自己制作一个 D&D 类型的游戏.我正在尝试让用户输入从 3 个字符中进行选择,每个字符都有自己的生命值等...当用户角色遇到怪物并被怪物攻击时,我想从用户角色中减去生命值.我只是一个初学者,不知道如何解决这个问题,这是我的示例代码....

I have been learning Python for a while and I decided to make a D&D type game on my own.I am trying to make a user input to choose out of 3 characters each with their own hit point etc...When the Users character encounter a monster and gets attacked by the monster I want to subtract hit points from the user character. I am just a beginner and not sure how to solve this problem, here is my code for a example....

 Choose_Character = input('Choose a character:\n1.Warrior\n2.Wizard\n3.Elf\n')

if Choose_Character == '1':
  Warrior = {'HP':140,'Magic':23}
  print('Here are you character stats:\nHit Points: ' + str(Warrior['HP'])+ 'nMagic Points: ' + str(Warrior['Magic']))




if Choose_Character == '2':
  Wizard = {'HP':123,'Magic':12}
  print('Here are your character stats:\nHit Points: ' + str(Wizard['HP'])+ '\nMagic Points: ' + str(Wizard['Magic']))


if Choose_Character == '3':
  Elf = {'HP':123,'Magic':12}
  print('Here are your character stats:\nHit Points: ' + str(Elf['HP'])+ '\nMagic Points: ' + str(Elf['Magic']))




fox = {'HP':123,'Magic':12}

print('You encounter a fox he bites you. you lode 20 Hit Points')

new_stats = int(Choose_Character['HP'] - 20)
print(f'your Hit Points are now:{new_stats}')

我收到一个错误:类型错误:字符串索引必须是整数我希望能够获取用户输入的字符,并在游戏中的任何时候使用该输入在字符字典中添加或减去点数.

I get a error: TypeError: string indices must be integersI want to be able to take the user input character and at any point in the game use that input to add or subtract points from the character dictionary.

推荐答案

您遇到了问题,因为您正在尝试访问输入字符串的 'HP'th 索引.Python 不理解这个命令,因为它没有意义.

You are running into issues because you are trying to access the 'HP'th index of your input string. Python doesn't understand this command because it doesn't make sense.

考虑的替代方案:

Choose_Character = input('Choose a character:\n1.Warrior\n2.Wizard\n3.Elf\n')

if Choose_Character == '1':
    character = {'Class': 'Warrior', 'HP': 140, 'Magic': 23}
elif Choose_Character == '2':
    character = {'Class': 'Wizard', 'HP': 123, 'Magic': 12}
elif Choose_Character == '3':
    character = {'Class': 'Elf', 'HP': 123, 'Magic': 12}

print('Here are your character stats:\nHit Points: ' + str(character['HP']) + '\nMagic Points: ' + str(character['Magic']))

fox = {'HP': 123, 'Magic': 12}

print('You encounter a fox he bites you. you lode 20 Hit Points')

new_stats = int(character['HP'] - 20)
print(f'your Hit Points are now:{new_stats}')

作为额外的建议,我建议将你的角色、怪物和物品放入一个类系统中(就像在编程类"而不是 RPG类"中一样).这将允许您创建它们的副本,这使您可以多次与同一类型的怪物战斗,而无需手动重置一堆东西.它还允许您为怪物实现有趣的行为.例如,您可以有一个类函数 on_die,它可以在怪物的 HP 达到零时指定自定义行为.对于史莱姆,此功能可用于生成更多史莱姆.总之,切换到类结构将使您的项目受益匪浅.

As an additional suggestion, I would recommend putting your characters, monsters, and items into a class system (as in the programming 'class' and not the RPG 'class'). This will allow you to create copies of them, which allows you to fight the same type of monster more than once without manually resetting a bunch of stuff. It also allows you to implement interesting behavior for monsters. For instance, you could have a class function on_die that could specify custom behavior when a monster's HP reaches zero. For slimes, this function could be used to spawn more slimes.In summary, switching to a class structure would benefit your project a lot.

这篇关于使 D&D 游戏遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:39