(两个类别都在帖子的底部) 我试图用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 过什么.