循环遍历实例并更改特定实例参数的值会出错

2024-10-03 21:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在学习一个在线的Python3初学者课程,在那里一个家伙制作一个RPG游戏来展示它的功能。你知道吗

所以一般来说,游戏在主.py并使用3个类,每个类在单独的文件中。类包括Person、Spell和Item

基本上,它在整个游戏中是一个循环,在它里面,它循环通过一方的每个玩家(3个玩家,一个敌人)。你知道吗

人类看起来是这样的:

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


Tags: nameself玩家itemsmpitemquantityperson
1条回答
网友
1楼 · 发布于 2024-10-03 21:30:36

我简化了您的代码(将一些数据转换为player\u项中的字符串),以使其正常工作。无论如何,它说明了这个想法。启动:

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_spells = None

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)
# 1
print(id(player1.items))
print(id(player2.items))
# 2
print((player1.items))
print((player2.items))

player1.items[0] = {}
# 3
print((player1.items))
print((player2.items))

它会告诉你1)两个玩家拥有相同的物品,但是2)这些物品的ID是相同的:两个玩家拥有相同的引用,所以在内存中操作相同的数据,如图3所示:更改一个玩家的数据,更改另一个玩家的物品。你知道吗

如果您在上面的代码中引入一个小的更改(注意.copy()),并说:

player2 = Person("Vidala   ", 380, 200, 55, 34, player_spells, player_items.copy())

然后,两个播放器将有两个不同的items列表,并且操纵其中一个不会更新另一个。同样,对象的id也会有所不同。你知道吗

相关问题 更多 >