基本的蟒蛇角色扮演游戏战斗



嘿,我是python/编程的新手,一直在做一些在线课程。我决定休息一天做笔记之类的,并尝试用一些随机的while循环来挑战自己,这些循环模拟了dnd中的不同事物。我目前被困在我的模拟战斗中,我知道我走在正确的轨道上,但显然它不起作用。一些指示将不胜感激。

import random

monster_hp = 25
strength = 5
ac = 17
hp = 25
combat = True
current_hp = monster_hp
dmg = 0
hit = False

def roll_to_hit(hit, ac):
hit = random.randint(1, 20)
return hit
if hit >= ac:
return True
# print(roll_to_hit(hit))

def roll_4_dmg(dmg, strength):
dmg = random.randint(1, 10) + strength
return dmg
print(f"You hit and did {dmg} damage.")

# print(roll_4_dmg())

def damage(current_hp, monster_hp, dmg):
current_hp = monster_hp - dmg
return current_hp

# print(damage())

while combat:
if current_hp > 0:
inp = input(r"A monster has appeared what will you do? ").lower()
if inp == "attack":
roll_to_hit(hit, ac)
if True:
roll_4_dmg(dmg, strength)
damage(current_hp, monster_hp, dmg)
else:
print("You missed!")
inp = input(
r"A monster has appeared what will you do? ").lower()
else:
print("You have slain the beast!")
break

好的,所以你的代码有很多问题,我会尝试一一检查它们。

在你的第一个函数中——

def roll_to_hit(hit, ac):
hit = random.randint(1, 20)
return hit
if hit >= ac:
return True

当您返回hit该功能将结束并且不会检查对AC的命中时。而且您不需要将hit作为参数传递。我认为你要找的是更像这样的东西——

def roll_to_hit(ac):
hit = random.randint(1, 20)
if hit >= ac:
return True
return False

您的第二个函数是一个类似的问题,您返回一个值,然后尝试执行另一个命令。它还需要一个不必要的参数dmg.它应该看起来更像这样——

def roll_4_dmg(strength):
dmg = random.randint(1, 10) + strength
print(f"You hit and did {dmg} damage.")
return dmg

除了未使用的参数之外,你的第三个函数很好current_hp它可以简单地是 -

def damage(monster_hp, dmg):
current_hp = monster_hp - dmg
return current_hp

现在在你的战斗循环中有很多错误,主要是因为你似乎没有存储/使用从你的函数返回的值(我强烈建议你阅读函数如何在python中工作(根据我对你在这里试图实现的目标的理解,我认为你想要的是——

while current_hp > 0:
inp = input(r"A monster has appeared what will you do? ").lower()
if(inp == "attack"):
if(roll_to_hit(ac)):
damage_done = roll_4_dmg(strength)
current_hp = damage(current_hp, damage_done)
else:
print("You missed!")
print("You have slain the beast!")

坦率地说,对于刚接触编程的人来说,这是一项相当不错的努力,您需要阅读存储在变量中的内容实际上如何变化(鉴于您在函数中的额外参数,我感觉那里有一点误解(

*另外,您的roll_to_hit功能只会像现在这样"命中"15%的时间,因此可能需要几次"攻击"来测试程序

最新更新