0

I code runs fine when I take the objects out of the inside of the Game object, but I want to define them inside of it. The program is supposed to take the string in the data object input it into a list within a list with one list, with the data in this list being the tile. But I just get this error message any idea?

import pygame        

class Map:
    grid = [[],[],[],[],[],[],[],[],[],[]]
    def __init__(self):
        self.loadMap("Test")
        self.printMap()
    def loadMap(self, mapno):
        for x in range(10):
            for y in range(10):
                #print(str(data.map_1[x][y])+", ("+str(x)+","+str(y)+")")
                #print(len(self.grid[0]))
                self.grid[x].append(Tile(game.data.map_1[x][y], x, y))
    def printMap(self):
        pass


class Tile:
    def __init__(self, tile, x, y):
        self.name, self.color = game.data.tile_type[str(tile)]
        self.x, self.y = x, y
        self.rect = pygame.rect.Rect((self.x*50),(self.y*50),50,50)

class Data:
    tile_type = {
        "0":("Sea", (5, 28, 179)),
        "1":("Forest", (18, 122, 15)),
        "3":("River", (61, 181, 245)),
        "4":("Sand", (232, 232, 30)),
        "2":("Grass", (33, 235, 26)),
        "5":("House", (87, 61, 31))
    }

    map_1 = ((0,0,0,0,0,0,0,0,0,0),
             (0,0,0,4,4,4,4,0,0,0),
             (0,4,4,4,2,2,5,4,0,0),
             (0,4,1,2,3,2,2,4,0,0),
             (0,4,1,5,3,2,1,4,0,0),
             (0,4,1,2,1,3,1,4,0,0),
             (0,4,4,4,4,3,1,4,0,0),
             (0,0,0,0,0,4,3,4,0,0),
             (0,0,0,0,0,4,1,4,0,0),
             (0,0,0,0,0,0,4,0,0,0))
class Game:
    def __init__(self):
        self.data = Data()
        self.map = Map()
        pygame.init()
        size = 500
        self.surface = pygame.display.set_mode((size,size))
        self.main()
    def main(self):
        while True:
            self.ev = pygame.event.poll()
            if self.ev.type == pygame.QUIT:
                break
            surface.fill((255, 240, 53))
            pygame.time.delay(10)
game = Game()

Error Message

Traceback (most recent call last):
  File "C:\Users\Amanda\Downloads\Tilesets.py", line 62, in <module>
    game = Game()
  File "C:\Users\Amanda\Downloads\Tilesets.py", line 50, in __init__
    self.map = Map()
  File "C:\Users\Amanda\Downloads\Tilesets.py", line 6, in __init__
    self.loadMap("Test")
  File "C:\Users\Amanda\Downloads\Tilesets.py", line 13, in loadMap
    self.grid[x].append(Tile(game.data.map_1[x][y], x, y))
NameError: name 'game' is not defined
1
  • 1
    There just isn't anything defined as game in that method. It doesn't have anything to do with objects within objects or wheels within wheels. There just isn't a game there so python complains. You probably should pass the game instance as a parameter to your Map constructor. Commented Mar 24, 2017 at 21:59

2 Answers 2

2

If you want to access the instance of Game within the Map instance, then you can pass self to Map when instantiating it, and assign it to the Map instance in its __init__:

class Map:
    def __init__(self, game):
        self.game = game
        ...

class Game:
    def __init__(self):
        ...
        self.map = Map(self)
        ...

Then, when you want to use it, you can simply use self.game:

self.grid[x].append(Tile(self.game.data.map_1[x][y], x, y))

And as abccd pointed out, you probably should be calling main() outside of __init__:

game = Game()
game.main()
Sign up to request clarification or add additional context in comments.

Comments

0

The problem relies here:

game = Game()

The game never finish initializing because your program is still inside it's def __init__ because you called self.main() inside it while there's a mainloop in main. so technically, it the game instance hasn't been fully defined yet. What you can change is do this instead. By calling the main function outside the __init__.

class Game:
    def __init__(self):
        self.data = Data()
        self.map = Map()
        pygame.init()
        size = 500
        self.surface = pygame.display.set_mode((size,size))

    def main(self):
        while True:
            self.ev = pygame.event.poll()
            if self.ev.type == pygame.QUIT:
                break
            surface.fill((255, 240, 53))
            pygame.time.delay(10)
game = Game()
game.main() # calling the main function here 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.