我正在try 制作一个基于文本的RPG.我有一些代码,比如:
英雄.py:
class Hero():
def __init__(self):
pass
def Attack(self, target):
# ...
def TakeDamage(self, amount):
# ...
怪物.py:
class Monster():
def __init__(self):
pass
def Attack(self, target):
# ...
def TakeDamage(self, amount):
# ...
整个文件 struct 如下所示:
|__ backend
__init__.py
monsters.py
heroes.py
MainGame.py
假设我想要Monster
和Hero
访问彼此的Attack
和TakeDamage
函数,例如:
class Monster():
def __init__(self):
pass
def Attack(self, target):
# ...
def TakeDamage(self, amount, target:Hero):
damage = # damage calculation here
target.TakeDamage(damage)
我该怎么做?到目前为止,我已经try 过:
- 在各自的文件中相互导入(例如
from .monsters import Monster
)-这会导致读取ImportError: cannot import name 'Monster' from partially initialized module 'backend.monsters' (most likely due to a circular import)
时出错.