(两个类别都在帖子的底部) 我试图用Python 创造一个"世界",从顶部看,世界是由一本字典组成的,就像

WorldDict = 
{
0: ["-","-","-"],
1: ["-","-","-"],
2: ["-","-","-"]
}

我试图将一个玩家"催生"到世界中,并陷入了它"渲染"玩家的部分(将字典上的坐标中的a -更改为p).

这是开始代码:

import WorldClass
import CreatureClass

MyWorld = WorldClass.World(5, 5, "  -")
MyWorld.CreateWorld()

Player = CreatureClass.Creature("  P", "player", 100, 1, 10, 1, "1")

执行MyWorld.PrintWorld()只会以良好的格式打印词典.当前输出:

  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -

问题来了,当我做MyWorld.SpawnCreature(" P", Player)甚至只是直接更改法令时:

coordinates = (1,3)
MyWorld.WorldDict[coordinates[1]][coordinates[0]] = "  P"

它最终会让它看起来像这样:

  -  P  -  -  -
  -  P  -  -  -
  -  P  -  -  -
  -  P  -  -  -
  -  P  -  -  -

有人能告诉我为什么它会影响1处的所有指数而不是仅影响3处的指数吗? 我想要的:

  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -
  -  P  -  -  -

班级:

世界:

import CreatureClass
class 世界:
    def __init__(self, width, height, defaultchar):
        self.width = width
        self.height = height
        self.defaultchar = defaultchar
        self.WorldDict = {}

    def CreateWorld(self):

        defaultchunk = []

        for number in range(0, self.width):
            defaultchunk.append(self.defaultchar)

        for number in range(0, self.height):
            self.WorldDict[number] = defaultchunk

    def PrintWorld(self):
        WorldDictKeys = list(self.WorldDict.keys())
        for key in WorldDictKeys:
            print("".join(self.WorldDict[key]))

    def GetCoords(self, coords):
        if self.CoordsValid(coords):
            item = self.WorldDict[coords[1]][coords[0]]
            return item

    def LocationIsEmpty(self, coords):
        if self.CoordsValid(coords):
            if self.GetCoords(coords) == self.defaultchar:
                return True
            else:
                return False

    def DrawOnWorld(self, coords, symbol):
        if self.CoordsValid(coords):
            self.WorldDict[coords[1]][coords[0]] = symbol
            print(f"drew on points ({coords[0]},{coords[1]})")


    def ClearCoords(self, coords):
        if self.CoordsValid(coords):
            self.DrawOnWorld(coords, self.defaultchar)


    def SpawnCreature(self, coords, aCreature):
        if self.CoordsValid(coords):
            if aCreature.creaturehasbeenspawned:
                print("creature has already been spawned")
            else:
                aCreature.creaturehasbeenspawned = True
                aCreature.coords = coords
                self.DrawOnWorld(aCreature.coords, aCreature.symbol)


    def CoordsValid(self,coords):
        if coords[0] <= len(self.WorldDict[0]) - 1 and coords[1] in list(self.WorldDict.keys()) and coords[0] >= 0 and coords[1] >= 0:
            return True
        else:
            return False
    def MoveCreature(self, aCreature, coords):
        if self.CoordsValid(coords):
            beforecoords = aCreature.coords

            aCreature.coords = coords
            self.ClearCoords(beforecoords)
            self.DrawOnWorld(aCreature.coords, aCreature.symbol)

    def TryMoveCreature(self, aCreature, coords):
        if self.CoordsValid(coords):
            if self.LocationIsEmpty(coords):
                self.MoveCreature(aCreature, coords)

生物:你在做什么?

class 生物:你在做什么?
    def __init__(self, symbol, type, defaulthealth, defaultmovespeed, defaultdamage, skill, team):
        self.symbol = symbol
        self.type = type
        self.defathealth = defaulthealth
        self.defatmovespeed = defaultmovespeed
        self.defatdamage = defaultdamage
        self.skill = skill
        self.team = team
        self.health = defaulthealth
        self.movespeed = defaultmovespeed
        self.damage = defaultdamage
        self.creaturehasbeenspawned = False
        self.coords = None

我已经在帖子的描述中描述了发生了什么、我想发生什么以及我try 过什么.

推荐答案

在收件箱World函数中,每一行都是同一个对象,因此当您更改一行时,您就会更改所有对象.

修复方法如下(使用list comprehension):

    def CreateWorld(self):
        for number in range(0, self.height):
            self.WorldDict[number] = [self.defaultchar for _ in self.width]

您可以阅读有关该问题的更多here

Python相关问答推荐

Python中两个矩阵的自定义Hadamard风格产物

Tkinter滑动条标签.我不确定如何删除滑动块标签或更改其文本

查找下一个值=实际值加上使用极点的50%

Python -Polars库中的滚动索引?

拆分pandas列并创建包含这些拆分值计数的新列

如何根据条件在多指标框架上进行groupby

Python Hashicorp Vault库hvac创建新的秘密版本,但从先前版本中删除了密钥

什么相当于pytorch中的numpy累积ufunc

聚合具有重复元素的Python字典列表,并添加具有重复元素数量的新键

无法定位元素错误404

如何在solve()之后获得症状上的等式的值

如何使用表达式将字符串解压缩到Polars DataFrame中的多个列中?

如果满足某些条件,则用另一个数据帧列中的值填充空数据帧或数组

python中的解释会在后台调用函数吗?

lityter不让我输入左边的方括号,'

使用Python从rotowire中抓取MLB每日阵容

如何在Python请求中组合多个适配器?

如何删除重复的文字翻拍?

GPT python SDK引入了大量开销/错误超时

如何从比较函数生成ngroup?