*我相信是lvl1[(x,y)] = getattr(__import__('mapTiles'), tile_name)(x, y)
造成了这个问题,我把它改成了直接导入,同样的问题,这里的循环导入是。。。mapTiles导入world,在load_tiles()
函数中,它导入mapTiles。我不知道如何重组这个来阻止循环,有什么想法吗?*
我正在做一个文本RPG游戏,我有所有的瓷砖编码,能够与之互动,但当我去运行游戏,它给我下面的错误。我在任何地方都看不到循环导入,所以我不明白发生了什么。(如果您需要其他代码,请告诉我)
错误:(由在播放中调用的load\u tiles()引起)
getattr(__import__('mapTiles'), tile_name)(x, y)
AttributeError: module 'mapTiles' has no attribute 'PlainsTile'
只显示基类和一个图块,因为大约有十个不同的图块 地图瓷砖.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
)
你知道吗播放.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")
你知道吗世界.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 '\r\n'
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来捕获索引错误,并添加了一组条件,在任何坐标处实例化tile名称。 我不明白的是,为什么你们中的任何一个人会拒绝说出我做错了什么,但仍然投反对票。如果你拒绝指出别人的错误,你怎么能期望他们变得更好呢。如果你想夺走名声,至少说说原因。难怪我的朋友拒绝使用这个网站,从来没有真正得到帮助。你知道吗
相关问题 更多 >
编程相关推荐