【发布时间】:2018-02-16 00:37:51
【问题描述】:
我目前正在学习 Python3 的在线初学者课程之一,其中一个人制作 RPG 游戏以展示其功能。
所以一般来说游戏在 main.py 中运行并使用 3 个类,每个类都在单独的文件中。类是 Person、Spell 和 Item
基本上,它在整个游戏中循环运行,在游戏内部,它循环遍历队伍中的每个玩家(3 个玩家,一个敌人)。
Person 类看起来像这样:
class Person:
def __init__(self, name, hp, mp, atk, df, magic, items):
self.maxhp = hp
self.hp = hp
self.maxmp = mp
self.mp = mp
self.atkl = atk - 15
self.atkh = atk + 15
self.df = df
self.magic = magic
self.items = items
self.actions = ["Attack", "Magic", "Items"]
self.name = name
现在直奔问题:
当我以这种方式使用一个变量传递带有项目的字典/数组时:
player_items = [{"item": potion, "quantity": 4},
{"item": superPotion, "quantity": 5},
{"item": elixer, "quantity": 5},
{"item": grenade, "quantity": 2}]
player1 = Person("Nasteroth", 460, 130, 65, 34, player_spells, player_items)
player2 = Person("Vidala ", 380, 200, 55, 34, player_spells, player_items)
player3 = Person("Xerogas ", 540, 100, 80, 34, player_spells, player_items)
然后,在player1回合中,当它应该只为player1减少1的使用物品数量时,它会减少我们正在循环的所有玩家的数量?
这里是循环的一部分:
players = [player1, player2, player3]
for player in players:
item_choice = int(input(BColors.OKGREEN + "Choose item to use: " + BColors.ENDC)) - 1
item = player.items[item_choice]
if item["item"].type == "potion":
player.heal(item["item"].prop)
print(BColors.OKGREEN, "\n", item["item"].name, "heals for", str(item["item"].prop), "HP", BColors.ENDC)
item["quantity"] -= 1
但是当我以手动方式传递项目时:
player1 = Person("Nasteroth", 460, 130, 65, 34, player_spells, [{"item": potion, "quantity": 4}, {"item": superPotion, "quantity": 5},
{"item": elixer, "quantity": 5},
{"item": grenade, "quantity": 2}])
player2 = Person("Vidala ", 380, 200, 55, 34, player_spells, [{"item": potion, "quantity": 4}, {"item": superPotion, "quantity": 5},
{"item": elixer, "quantity": 5},
{"item": grenade, "quantity": 2}])
player3 = Person("Xerogas ", 540, 100, 80, 34, player_spells, [{"item": potion, "quantity": 4}, {"item": superPotion, "quantity": 5},
{"item": elixer, "quantity": 5},
{"item": grenade, "quantity": 2}])
代码运行良好,它分别减少了每个玩家使用的物品数量。
Python3 中是否有一些我还不知道的全局参数之类的东西?
编辑:
粘贴更多代码会使它变成一堵文字墙,因此可以在此处找到完整代码: https://pastebin.com/4V6NFu1z
【问题讨论】:
-
所有玩家都有相同的单个背包物品
player_items。显示您的Person课程。 -
把它放到问题中,而不是放到代码不可读的cmets中。
-
这是您的完整代码吗?你确定它在工作吗?有很多问题使它无效。
-
在我的问题底部添加了包含完整代码的链接,所有文件都包含在所有类中。一般来说,代码是好的,它启动并执行它应该做的除了项目之外的事情。整个程序仍在进行中,因此尚未添加一些功能。