【发布时间】:2021-03-23 18:29:15
【问题描述】:
# Import Modules
from Dice import dice
d10 = dice(10,1)
d4 = dice(4,1)
# Assign Classes
class Player_Character:
def __init__(self, hp, maxhp, ac, THAC0, Surprise_Adjustment, Initiative_Adjustment):
self.hp = int(hp)
self.maxhp = int(maxhp)
self.ac = int(ac)
self.THAC0 = int(THAC0)
self.Surprise_Adjustment = int(Surprise_Adjustment)
self.Initiative_Adjustment = int(Initiative_Adjustment)
def attack(self, goblin):
Player_Character_Damage = d10.die_roll()
goblin.hp -= Player_Character_Damage
if (goblin.hp <= 0):
print("congratulations you killed the goblin")
def flee(self):
print("you run away ")
quit()
def heal(self, Player_Character):
Player_Character.hp += d10.die_roll()
if Player_Character.hp >= Player_Character.maxhp:
Player_Character.hp = Player_Character.maxhp
class goblin:
def __init__(self, hp, maxhp, ac, THAC0, Surprise_Adjustment, Initiative_Adjustment):
self.hp = int(hp)
self.maxhp = int(maxhp)
self.ac = int(ac)
self.THAC0 = int(THAC0)
self.Surprise_Adjustment = int(Surprise_Adjustment)
self.Initiative_Adjustment = int(Initiative_Adjustment)
def attack(self, Player_Character):
goblin_damage = d4.die_roll()
Player_Character.hp -= goblin_damage
if (Player_Character.hp <= 0):
print("oh dear you have died")
del Player_Character
MrHezy = Player_Character(10, 20, 10, 15, 0, 2)
def spawn_goblin(goblin):
G1 = goblin(5, 10, 8, 18, 0, 0)
return G1
goblin1 = spawn_goblin(goblin)
def battle(goblin1):
# user input
player_initiative_adjustment = MrHezy.Initiative_Adjustment
monster_initiative_adjustment = goblin1.Initiative_Adjustment
#define while loop for the battle
battle_not_over = 'yes'
while battle_not_over == 'yes':
#use random.randint(a,b) to generate player and monster base initiative
player_base_initiative = d10.die_roll()
monster_base_initiative = d10.die_roll()
#subtract the adjustment to get player and monster initiative
player_initiative = player_base_initiative - player_initiative_adjustment
monster_initiative = monster_base_initiative - monster_initiative_adjustment
#compare the initiatives and display the results
if (player_initiative < monster_initiative):
attack_flee_heal = input("congratulations you go first. Would you like to attack, flee, or heal?")
while attack_flee_heal != 'attack' or 'flee' or 'heal':
if attack_flee_heal == 'attack':
MrHezy.attack(goblin1)
elif attack_flee_heal == 'heal':
MrHezy.heal(MrHezy)
print("the goblin attacks")
goblin1.attack(MrHezy)
break
elif attack_flee_heal == 'flee':
MrHezy.flee()
break
else:
print("uhoh, the monsters go first, they attack!")
goblin1.attack(MrHezy)
attack_flee_heal = input("Would you like to attack, flee, or heal? ")
while attack_flee_heal != 'attack' or 'flee' or 'heal':
if attack_flee_heal == 'attack':
MrHezy.attack(goblin1)
elif attack_flee_heal == 'heal':
MrHezy.heal(MrHezy)
print("the goblin attacks")
goblin1.attack(MrHezy)
break
elif attack_flee_heal == 'flee':
MrHezy.flee()
break
#main game loop
while True:
spawn_goblin(goblin)
battle(goblin1)
这是我一直在开发的战斗模拟器的代码。它首先导入我制作的一个模块,该模块由一个名为“dice”的类组成,我用它来随机生成数字。接下来是定义等级,属性包括hp,maxhp,装甲等级,“击中装甲等级0”,突击调整和先攻调整,以及允许您攻击怪物的攻击,退出战斗的逃跑和治疗的方法这会给你的角色更多的生命值。程序继续定义生成 goblin 的 spawn_goblin() 函数(这在循环的第一次迭代中工作得很好)。然后进入非常简单的战斗部分;它所做的只是检查谁先走,然后让你攻击、逃跑或治愈自己。请忽略“治愈”和“逃离”方法,这些方法都很好用。当我攻击妖精时会出现问题。它不会杀死地精并生成另一个地精,而是创建一个无限循环,说“恭喜你杀死了地精”
【问题讨论】:
-
这段代码已经过编辑,它原来有一个while循环,现在改为if语句。但是问题仍然存在。
-
PS 我认为问题的一部分可能是每当你对地精造成伤害时,无论它有多少 hp 或你造成多少伤害,它都会死去......我如何让地精只承受它造成的伤害?
-
PPS 每次地精都死掉的原因是因为程序不是将地精的 hp 重置为 5,而是不断地将 hp 减去负数,所以当地精重生时它的 hp 为负数并且它会自动说“恭喜你杀死了妖精”......我仍然不知道如何解决这个问题
-
虽然最初的问题已经解决了,但地精血量仍然为负的问题仍然存在(但没有无限循环,现在我可以在两次攻击之间停下来治疗,但是除了第一次攻击之外的每一次攻击都会自动杀死妖精,因为它的 hp 为负)。
-
我解决了这个问题。我不得不从attack_flee_heal 中删除所有break 语句,并在战斗的while 循环开始时添加一个“ if (goblin1.hp
标签: python function oop methods while-loop