0

I'm storing these different objects in data dictionaries and am switching which one is printed with the currentmap variable, however when I change the index that of the data.tilemaps it doesn't matter if I set it to 0 or 1 it always just shows the tilemap of the first map defined in the data dictionary. Why is this the case?

class Map:
   grid = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
   object = []
   def __init__(self):
       self.loadMap()
   def loadMap(self):
       for x in range(18):
        for y in range(20):
            print(str(self.maplist[x][y])+", ("+str(x)+","+str(y)+")")
            #print(len(self.grid[0]))
            self.grid[x].append(Tile(self.maplist[x][y], y, x))        
    def printMap(self):
        for x in range(18):
            for y in range(20):
                self.grid[x][y].printTile()

class HomeIsland(Map):
    maplist = ((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
           (0,0,0,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
           (0,0,4,2,2,2,4,4,0,0,0,0,0,0,0,0,0,0,0,0),
           (0,0,4,2,2,2,2,2,4,0,0,0,0,0,0,0,0,0,0,0),
           (0,0,4,2,2,2,2,2,2,4,4,4,0,0,0,0,0,0,0,0),
           (0,0,4,2,2,2,5,2,2,2,2,4,4,4,4,0,0,0,0,0),
           (0,0,0,4,2,2,2,2,2,2,2,2,2,2,2,4,4,0,0,0),
           (0,0,0,0,3,3,3,3,3,9,9,3,3,2,2,2,4,4,0,0),
           (0,0,0,0,3,3,3,3,3,9,9,3,3,3,2,2,2,4,4,0),
           (0,0,0,0,4,4,2,2,2,2,2,2,3,3,3,2,2,2,4,0),
           (0,0,0,0,4,4,2,2,2,8,2,2,2,3,3,2,2,2,4,0),
           (0,0,0,0,4,4,2,2,2,2,2,2,2,3,3,2,2,4,0,0),
           (0,0,0,0,4,1,6,2,2,2,2,2,2,3,3,2,2,4,0,0),
           (0,0,0,4,1,1,1,1,1,1,1,2,2,2,3,3,2,4,0,0),
           (0,0,0,4,1,1,7,7,7,1,1,1,2,2,3,3,2,4,0,0),
           (0,0,4,1,1,1,7,7,7,1,1,1,1,2,3,3,2,4,0,0),
           (0,0,4,1,1,1,1,1,1,1,1,1,1,2,3,3,2,4,0,0),
           (0,0,4,1,1,1,1,1,1,1,1,1,1,2,3,3,2,2,4,0))
class DungeonHub(Map):
    maplist = ((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
           (0,0,0,0,0,12,12,12,12,12,12,12,12,12,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,11,10,10,11,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,11,10,10,11,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,11,10,10,11,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,11,10,10,11,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,10,10,10,10,10,10,10,10,12,0,0,0,0,0),
           (0,0,0,0,0,12,12,12,12,12,12,12,12,12,12,0,0,0,0,0),
           (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
class Data:
    def loadTileMaps(self):
        self.tilemaps = {
            0:DungeonHub(),
            1:HomeIsland()
        }
class Game:
    def __init__(self):
        pygame.init()
        xsize = 1000
        ysize = 1000
        self.surface = pygame.display.set_mode((xsize,ysize))
        data.loadTileMaps()
    def main(self):
        while True:
            self.ev = pygame.event.poll()
            if self.ev.type == pygame.QUIT:
                break
            self.surface.fill((202, 205, 209))
            currentmap.printMap()
            currentmap.printAllObject()
            pygame.display.flip()
            pygame.time.delay(10)
currentmap = data.tilemaps[1]
Rob.S
  • 23
  • 1
  • 4
  • 1
    Answer to title: Yes. Answer to question: What dictionary are you talking about? Answer to actual question: you are using class variables, you want instance variables. – timgeb Mar 29 '17 at 17:27
  • 1
    And a question on my part: Why are you putting everything in a class? – timgeb Mar 29 '17 at 17:28
  • This dictionary `self.tilemaps = { 0:DungeonHub(), 1:HomeIsland() }`, and everything is in a class because I find it easier to organise everything that way – Rob.S Mar 29 '17 at 17:31
  • 1
    ok, but get ready to get screamed at by other Python programmers :) – timgeb Mar 29 '17 at 17:32
  • Why what's the standard way to do things? – Rob.S Mar 29 '17 at 17:34
  • 1
    Use a class when you need instances of that class. When you need a function, just define a function. Don't put it into a class for no reason. – timgeb Mar 29 '17 at 17:36

0 Answers0