当再次调用函数时,有没有办法获得一个随机数来重新填充函数内部



我对Python相当陌生,我知道函数中调用的值只在函数内部。在我写的一个小文字游戏中,我试图在玩家和老板之间展开一场战斗;然而,它只是在每次调用函数时不断填充相同的信息。我觉得我错过了什么。如有任何帮助,我们将不胜感激。

类别:

class Character:
def __init__(self, name, stats):
self.name = name
self.stats = stats
name = {
"Name": ""
}
stats = {
"Dexterity": "",
"Strength": "",
"Health": 20,
"AC": 16,
"Weapon": "",
}
damage = 2 * random.randrange(1, 7)
ability_check = random.randrange(1, 20)
initiative = random.randrange(1,20)

class Boss:
def __init__(self, name, stats):
self.name = name
self.stats = stats

name = {
"Name": "Gargamel"
}
stats = {
"AC": 16,
"Health": 15,
"Weapon": "Sword"
}
damage = random.randrange(1, 6)
initiative = random.randrange(1,20)

功能:

def battle():
choice = input("Do you wish to continue fighting or run? F or R  ")
if (choice.lower() == 'f'):
boss_battle()
if (choice.lower() == 'r'):
pass
def boss_battle():
print("The skeletal creature grabs a sword from the wall and takes a swing at you...n")
print(f"Boss init {Boss.initiative}, Character init {Character.initiative}")
while Boss.stats["Health"] > 0 or Character.stats["Health"]:
if (Boss.initiative > Character.initiative):
boss_damage = Boss.damage
current_player_health = (Character.stats["Health"] - boss_damage)
Character.stats.update({"Health": current_player_health})
print(f"The boss did {boss_damage} damage. You now have {current_player_health} hitpoints left.")
if (Character.stats["Health"] <= 0):
print('You died!')
break
battle()  
elif (Character.initiative > Boss.initiative):
player_damage = Character.damage + stat_block_str(int)
current_boss_health = Boss.stats["Health"] - player_damage
Boss.stats.update({"Health": current_boss_health})
print(f"You attacked the creature with your {Character.stats['Weapon']} and dealt {player_damage} damage.")
if (Boss.stats["Health"] <= 0):
print(f'Congratulations {Character.name["Name"]}! You have beaten the boss and claimed the treasure!')
break
battle()

您已经用类变量声明了类,但没有创建任何类实例,因此这些值都是固定的,因为在定义类时会初始化一次。

要创建类实例,您可以"呼叫";使用圆括号的类,该类在实例上调用__init__函数,该函数设置实例变量。

这里有一个小例子:

import random
class Character:
def __init__(self,name):
self.name = name
self.health = 20
self.damage = 2 * random.randrange(1,7)
def attack(self,target):
print(f'{self.name} attacks {target.name}...')
target.health -= self.damage
print(f'{target.name} has {target.health} health remaining.')
# defines how to display the class instance when printed.
def __repr__(self):
return f"Character(name={self.name!r}, health={self.health}, damage={self.damage})"
fred = Character('Fred')
george = Character('George')
print(fred)
print(george)
print(f'{fred.name} can deliver {fred.damage} damage.')
fred.attack(george)

输出:

Character(name='Fred', health=20, damage=4)
Character(name='George', health=20, damage=10)
Fred can deliver 4 damage.
Fred attacks George...
George has 16 health remaining.

虽然这是真的,但当你应该更好地使用实例变量时,你使用类变量(有关详细信息,请参阅Python中的实例变量与类变量(,我认为这还不足以实现你的目标。

你在评论中描述的是因为类只设置了一次,从那时起,类变量(如损坏等(是静态的,这意味着整个程序只执行一次随机调用。我建议将损伤变量转换为这样的函数:

class Character:
def __init__(self,name):
self.name = name
self.health = 20
# just as a simple example
self.base_damage = 2 
def damage(self):
return self.base_damage * random.randrange(1,7)

通过这种方式,您将获得一个新的随机值来调用函数(战斗回合(,而不仅仅是类内伤害的静态值。假设你为角色和老板创建了实例,那么调用可能是这样的:

current_boss_health = bossInstance.stats["Health"] - characterInstance.damage()

如果你想更进一步,让它更像蟒蛇,你可以使用@property decorator来修复损坏:

@property
def damage(self):
return self.base_damage * random.randrange(1,7)

这样,函数的使用将看起来更像您的原始函数,并且它隐藏了函数调用:

current_boss_health = bossInstance.stats["Health"] - characterInstance.damage

最新更新