我正在使用 python 开发游戏。
游戏中的 AI 使用玩家拥有的变量,反之亦然。
例如:
class Player():
def __init__(self, canvas...):
self.id = canvas.create_rectangle(...)
...
def touching_AI(self):
aipos = canvas.coords(AI object)
pos = canvas.coords(self.id)
...
#the function above checks if the player is touching the AI if it
#is, then call other functions
this = player(canvas...)
class AI():
def __init__(self, canvas...):
self.id = canvas.create_rectangle(...)
def chase_player(self):
playerpos = canvas.coords(this.id)
pos = canvas.coords(self.id)
...
# a lot of code that isn't important
显然,Python 说玩家类中的 AI 对象没有定义。两个类都依赖于另一个来工作。但是,一个还没有定义,所以如果我把一个放在另一个之前,它会返回一个错误。虽然可能只有这两个函数有一个解决方法,但还有更多我没有提到的函数。
总之,有没有办法(pythonic 或非 pythonic)在创建对象之前使用和/或定义它(即甚至制作更多文件)?
神不在的星期二
LEATH
杨魅力
相关分类