(两个课程都在帖子底部) 我试图用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 World:
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 Creature:
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
我已经在帖子描述中描述了发生的事情、我想要发生的事情以及我尝试过的事情。
在 CreateWorld 函数中,每一行都是同一个对象,因此当您更改一个对象时,您会更改所有对象。
解决这个问题的方法如下所示:
def CreateWorld(self):
for number in range(0, self.height):
self.WorldDict[number] = [self.defaultchar for _ in self.width]