python-3.x - 再次调用函数后,有没有办法让随机数重新填充到函数内部?
问题描述
我对 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-3.x - ModuleNotFoundError:没有名为“networkx”的模块
- mysql - mysql错误排序字符串中的数字,包括符号(-,破折号)
- python - 使用python加密和解密电子邮件
- azure - 每个分区的事件中心日志
- powershell - Powershell AzureAD 应用注册权限 New-AzureADApplication -RequiredResourceAccess
- javascript - 如何创建模拟 POST 请求以测试页面是否已更改
- security - 是否可以获得整个 GitHub 组织的 Dependabot 统计信息?
- python - 是否可以在“DjangoFilterBackend”的“filterset_fields”列表中使用“__date”作为“timestamp”字段
- android - 如果文本不适合 TextView,如何模糊最后几行文本
- android - 我需要使用优化路线的 mapbox 开始导航,但应用程序爆炸了