Rishabh2
Rishabh2

Reputation: 178

Trouble importing a module in Python

I am trying to create a dungeon developing program, and I have a number of modules I am using. I have a main module, a floor module, a room module, and a tile module. Floors are girds of rooms which are grids of tiles. In my floor module, I import Room so that I can store a two dimensional list of Rooms, but I keep getting the error:

Traceback (most recent call last):
 File "C:\Users\Rishabh\workspace\Bonding\Bonding\src\MainWork.py", line 15, in <module>
  floor = Floor.Floor()
 File "C:\Users\Rishabh\workspace\Bonding\Bonding\src\Floor.py", line 6, in __init__
  rooms = [[Room.RoomClass(i, j) for i in range(7)] for j in range(7)]
 File "C:\Users\Rishabh\workspace\Bonding\Bonding\src\Floor.py", line 6, in <listcomp>
  rooms = [[Room.RoomClass(i, j) for i in range(7)] for j in range(7)]
 File "C:\Users\Rishabh\workspace\Bonding\Bonding\src\Floor.py", line 6, in <listcomp>
  rooms = [[Room.RoomClass(i, j) for i in range(7)] for j in range(7)]
NameError: global name 'Room' is not defined

My code is as follows.

Mainwork.py

import Enemy
import Player
import Ribbon
import random
import Floor
import Room
import pygame as pyg
pyg.init()

screenWidth = 1280
screenHeight = 720
Player.health = 100
FPS = 60

floor = Floor.Floor()
floor.printgrid()

def mainLoop():
    pass

Floor.py

class Floor(object):

    import Room

    def __init__(self):
        rooms = [[Room.Room(i, j) for i in range(7)] for j in range(7)]
        current = [0, 0]
        roomStack = []
        totalRooms = 49
        visitedRooms = 1

Room.py

class Room(object):

    import Tile

    def __init__(self, floorx, floory, layout=[[0 for i in range(13)] for j in range(7)]):
        self.floorx = floorx
        self.floory = floory
        self.doors = [False, False, False, False]  # N,S,E,W
        for i in layout:
            for j in i:
                self.layout[i][j] = Tile.Tile(layout[i][j])

Tile.py

class Tile(object):

    def __init__(self, state):
        self.state = state

I have no idea what the problem could be at all. Thanks in advance

Upvotes: 0

Views: 117

Answers (1)

Siva Cn
Siva Cn

Reputation: 947

In Floor.py the import Room is a class attribute so, you have to use class object to access it. Try using....

import Room

class Floor(object):

    def __init__(self):
        # other code goes here ....

Upvotes: 1

Related Questions