英文:
Basic Python RPG combat
问题
嗨,大家好,我是全新接触Python/编程的,一直在参加一些在线课程。我决定今天放下笔记,挑战一下自己,尝试使用一些随机的while循环来模拟龙与地下城中的不同情境。我目前卡在了模拟战斗部分,我知道我走在正确的道路上,但显然还不起作用。欢迎提供一些建议。
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
def roll_4_dmg(dmg, strength):
dmg = random.randint(1, 10) + strength
return dmg
print(f"你击中了,造成了{dmg}点伤害。")
def damage(current_hp, monster_hp, dmg):
current_hp = monster_hp - dmg
return current_hp
while combat:
if current_hp > 0:
inp = input("一个怪物出现了,你要做什么?").lower()
if inp == "攻击":
roll_to_hit(hit, ac)
if True:
roll_4_dmg(dmg, strength)
damage(current_hp, monster_hp, dmg)
else:
print("你没有击中!")
inp = input("一个怪物出现了,你要做什么?").lower()
else:
print("你杀死了野兽!")
break
英文:
Hey ya'll I'm brand new to python/programming and have been doing some online courses. I decided to take the day off of taking notes and such and try to challenge myself with some random while loops that simulate different things in dnd. I'm currently stuck on my simulated combat, and I know I'm on the right track but obviously it's not working. Some pointers would be much appreciated.
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
答案1
得分: 1
在你的代码中有很多问题,我会逐一进行说明。
在你的第一个函数中 -
def roll_to_hit(hit, ac):
hit = random.randint(1, 20)
return hit
if hit >= ac:
return True
一旦返回hit
,函数就会结束,不会检查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"你击中并造成了 {dmg} 伤害。")
return dmg
你的第三个函数除了未使用的参数current_hp
外是正常的,可以简化为 -
def damage(monster_hp, dmg):
current_hp = monster_hp - dmg
return current_hp
现在在你的战斗循环中有很多错误,主要是因为你似乎没有存储/使用从函数返回的值(我强烈建议你学习一下函数在Python中的工作原理)。根据我对你尝试实现的目标的理解,以下是我认为你想要的代码 -
while current_hp > 0:
inp = input("一个怪物出现了,你要做什么? ").lower()
if inp == "攻击":
if roll_to_hit(ac):
damage_done = roll_4_dmg(strength)
current_hp = damage(current_hp, damage_done)
else:
print("你没有击中!")
print("你已经击败了怪物!")
对于一个刚开始学编程的人来说,这已经是相当不错的尝试了,但你需要学习一下变量是如何改变的(考虑到你在函数中有额外的参数,我觉得你可能对这方面有一些误解)。
*另外,你的roll_to_hit
函数目前只有15%的概率会“击中”,所以测试这个程序可能需要几次“攻击”循环。
英文:
Ok so there are a lot of problems with your code and I will try going through them one-by-one.
In your first function -
def roll_to_hit(hit, ac):
hit = random.randint(1, 20)
return hit
if hit >= ac:
return True
The moment you return hit
the function is going to end and will not check the hit against the AC. And you dont need to pass in hit
as an argument. What I think you were looking for was something more like this -
def roll_to_hit(ac):
hit = random.randint(1, 20)
if hit >= ac:
return True
return False
Your second function as a similar problem where you return a value and then try to execute another command. It also takes in an unnecessary argument dmg
. It should look more like this -
def roll_4_dmg(strength):
dmg = random.randint(1, 10) + strength
print(f"You hit and did {dmg} damage.")
return dmg
Your third function is fine apart from the unused argument current_hp
it can simply be -
def damage(monster_hp, dmg):
current_hp = monster_hp - dmg
return current_hp
Now in your combat loop there are a whole lot of errors mainly due to the fact that you don't seem to be storing/using the values returned from your functions (I highly recommend you read up on how functions are expected to work in python) From my understanding of what you are trying to achieve here is what I think you were going for -
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!")
Frankly its a pretty good effort for someone new to programming, you need to read up on how stuff stored in variables actually changes (given your extra arguments in the functions I have a feeling there a slight misunderstanding there)
*Also on a different note, your roll_to_hit function is only going to 'hit' 15% of the time as it is right now so it might take a few 'attack' rolls to test out the program
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论