无法在Python中导入模块

时间:2014-06-22 17:45:53

标签: python

我正在尝试创建一个地下城开发程序,我正在使用一些模块。我有一个主模块,一个地板模块,一个房间模块和一个瓷砖模块。地板是房间的网格,是瓷砖网格。在我的楼层模块中,我导入Room以便我可以存储二维房间列表,但我一直收到错误:

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

我的代码如下。

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

我根本不知道问题究竟是什么。提前致谢

1 个答案:

答案 0 :(得分:1)

Floor.py中,import Roomclass属性,因此您必须使用类对象来访问它。尝试使用....

import Room

class Floor(object):

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