(Both Classes are at the bottom of the post)
Im trying to make a "world" in python looking from the top, the world is made up of a dictionary like
WorldDict =
{
0: ["-","-","-"],
1: ["-","-","-"],
2: ["-","-","-"]
}
I'm trying to "spawn" a player into the world and im gettting stuck on the part where it "renders" the player(changing a - to a p in the coordinates on the dictionary).
This is the starting code:
import WorldClass
import CreatureClass
MyWorld = WorldClass.World(5, 5, " -")
MyWorld.CreateWorld()
Player = CreatureClass.Creature(" P", "player", 100, 1, 10, 1, "1")
Doing MyWorld.PrintWorld()
will just print the dictionary in a nice format. Current output:
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
Heres the problem, When I do MyWorld.SpawnCreature(" P", Player)
or even just directly changing the dict:
coordinates = (1,3)
MyWorld.WorldDict[coordinates[1]][coordinates[0]] = " P"
it ends up making it look like this:
- P - - -
- P - - -
- P - - -
- P - - -
- P - - -
Can someone tell me why its affecting the all of the indexes at 1 instead of just the one at 3?
What I want:
- - - - -
- - - - -
- - - - -
- - - - -
- P - - -
Classes:
World:
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)
Creature:
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
I already described what happened, what I wanted to happen, and what I tried within the description of the post.