Python 看不到外部对象



* 我相信lvl1[(x,y)] = getattr(__import__('mapTiles'), tile_name)(x, y)导致了问题,我将其更改为具有相同问题的直接导入,这里的循环导入是......mapTiles导入世界,在load_tiles()函数中,它导入mapTiles。我不知道如何重组它以阻止圈子,有什么想法吗?*我正在制作一个文本RPG游戏,我编码了所有瓷砖并能够与之交互,但是当我去运行游戏时,它给了我下面的错误。我在任何地方都看不到循环导入,所以我不明白发生了什么。(如果您需要任何其他代码,请告诉我(

错误:(由在游戏中调用的 load_tiles(( 引起(

getattr(__import__('mapTiles'), tile_name)(x, y)
AttributeError: module 'mapTiles' has no attribute 'PlainsTile'

仅显示基类和一个磁贴,因为大约有十个不同的磁贴 mapTiles.py

import actions, items, enemies, actions, world
class MapTile:
def __init__(self,name, x, y, intro, description):
self.x = x 
self.y = y
self.intro = intro
def intro_text(self):
return self.intro
def terrain_interacts(self, terrain):
# For elemental effects
pass
def randomize_interactions(self, tile):
# Randomize enemy and loot spawns
pass

# Default actions
def adjacent_moves(self):
# Returns all move actions for adjacent tiles
moves = []
if world.tile_exists(self.x + 1, self.y):
moves.append(actions.MoveEast())
if world.tile_exists(self.x - 1, self.y):
moves.append(actions.MoveWest())
if world.tile_exists(self.x, self.y - 1):
moves.append(actions.MoveNorth())
if world.tile_exists(self.x, self.y + 1):
moves.append(actions.MoveSouth())
moves.append(actions.ViewInventory)
return moves

def available_actions(self):
# Returns all available actions for the current tile
moves = self.adjacent_moves()
return moves

class PlainsTile(MapTile):
def __init__(self, x, y):
self.name = "PlainsTile"
self.intro = "You enter a plains"
self.description = "A span of clear land, with the tall grass waving in the wind"
super().__init__(
name=self.name,
intro=self.intro,
description=self.description,
x=self.x,
y=self.y
)

play.py

#play.py
import character, world
def play_game(player):
world.load_tiles()
print("Called")
#These lines load the starting room and display the text
tile = world.tile_exists(x=player.location_x, y=player.location_y)
if tile != None:
print(tile)
while player.is_alive() and not player.victory:
tile = world.tile_exists(player.location_x, player.location_y)
# Check again since the room could have changed the player's state
if player.is_alive() and not player.victory:
print("Choose an action:n")
last[0] = player.location_x
last[1] = player.location_y
if tile != None:
available_actions = tile.available_actions()
if tile.name == 'BossTile' and player.victory:
tile.modify_character(player)
for action in available_actions:
print(available_actions.name)
for action in available_actions:
action_input = input("Choose an action ya prick: ")
if action_input == "quit":
quit()
if action_input == action.keyword:
player.do_action(action, **action.kwargs)
break
else:
print("Please choose one of the listed actions")
else:
print("You cannot go that way")

world.py

# No imports
lvl1 = {}
def load_tiles():
"""Parses a file that describes the world space into the _world object"""
with open('m1.txt', 'r') as f:
rows = f.readlines()
x_max = len(rows[0].split('t')) # Assumes all rows contain the same number of tabs
for y in range(len(rows)):
cols = rows[y].split('t')
for x in range(x_max):
tile_name = cols[x].replace('n', '') # Windows users may need to replace 'rn'
if tile_name == 'StartingRoom':
global starting_position
starting_position = (x, y)
if tile_name == '':
lvl1[(x, y)] = None
else:
getattr(__import__('mapTiles'), tile_name)(x, y)
def tile_exists(x,y):
return lvl1.get((x,y))

从所有其他文件导入

#items.py
#No imports
#actions.py
from play import player
import items
#enemies.py
import random
#character.py
import world, items, random

我修复了它,错误是从getattr导入的。我合并了这两个文件,添加了一个 try/except 来捕获索引错误,并添加了一堆条件,这些条件在任何坐标处实例化磁贴名称。

最新更新