我正在使用discord.py创建一个Discord机器人。我希望它成为Discord中的一种RPG,您可以在Discord中与怪物战斗,升级并进行任务。问题出在代码中,该代码生成用户必须与之战斗的敌人列表。现在,它应该创建2个具有相同属性的相同类型的敌人。
代码如下所示:
#startBattle method. Creates an enemy and adds them to the user's 'fightingNow' list
def startBattle(id, room, area):
#prevents player from progressing while in a battle
playerData[id]['canProgress'] = False
#Chooses amount of enemies to be created. Set it just 2 for testing reasons
amount = random.randint(2,2)
for enemyID in range(0, amount):
#Creates a modifier to be applied to an enemy's stats
randomMod = random.randint(-3,3)
modifier = 1 + (room + randomMod) / 10
#chooses an enemy from a .json file. Set to only choose one enemy for testing reasons
enemy = random.randint(1,1)
enemy = enemyData[str(enemy)]
#Apply modifiers to related stats
enemy['maxHP'] = int(modifier * enemy['maxHP'])
enemy['baseHP'] = enemy['maxHP']
enemy['baseEnd'] = int(modifier * enemy['baseEnd'])
enemy['baseAttack'] = int(modifier * enemy['baseAttack'])
enemy['baseAgi'] = int(modifier * enemy['baseAgi'])
enemy['basePre'] = int(modifier * enemy['basePre'])
enemy['baseLvl'] = int(modifier * enemy['baseLvl'])
#sets the enemies id, used in the battle system to determine who the player is attacking
enemy['id'] = enemyID
#print() will be removed in final version.
print(enemy)
#Appends the created enemy to the user's list of enemies they are fighting
playerData[id]['fightingNow'].append(enemy)
#saves data to some .json files
_save()
它应该像这样工作: 敌人1和敌人2都是骷髅。敌人1的最大生命值统计为10,而敌人2的最大生命值为12。因此,这两个值都应添加到用户的“ fightingNow”列表中。相反,该列表包含两个骨骼,最大HP为12。生成了敌人1,但被敌人2的克隆覆盖。谢谢您提供的任何帮助。
答案 0 :(得分:1)
字典是可变对象,因此,当您附加enemy
时,实际上是在附加对enemy
的引用,而不是enemy
的副本。幸运的是,这是一个相当容易的修复-只需添加.copy()
:
playerData[id]['fightingNow'].append(enemy.copy())