3 回答
TA贡献1963条经验 获得超6个赞
你不
而是使用参数
class Player():
def __init__(self, canvas...):
self.id = canvas.create_rectangle(...)
...
def touching(self,other):
aipos = canvas.coords(other.object)
pos = canvas.coords(self.id)
...
#the function above checks if the player is touching the AI if it
#is, then call other functions
class AI():
def __init__(self, canvas...):
self.id = canvas.create_rectangle(...)
def chase(self,player):
playerpos = canvas.coords(player.id)
pos = canvas.coords(self.id)
然后
player = Player(canvas...)
ai = AI(...)
ai.chase(player)
player.touching(ai)
但更好的是定义一个基础对象类型来定义你的接口
class BaseGameOb:
position = [0,0]
def distance(self,other):
return distance(self.position,other.position)
class BaseGameMob(BaseGameOb):
def chase(self,something):
self.target = something
def touching(self,other):
return True or False
那么你所有的东西都从这里继承
class Player(BaseGameMob):
... things specific to Player
class AI(BaseGameMob):
... things specific to AI
class Rat(AI):
... things specific to a Rat type AI
TA贡献1936条经验 获得超6个赞
您没有依赖循环问题。但是,你有以下问题,
您正在尝试使用 AI 对象,但您没有在任何地方创建该对象。它需要看起来像,
foo = AI() #creating the object bar(foo) #using the object
周围的语法错误
canvas.coords(AI object)
。调用函数的方式是
foo(obj)
没有类型。在定义函数时,您可以选择提及类型,例如
def foo(bar : 'AI'):
你可以相互依赖类的证明,https://pyfiddle.io/fiddle/b75f2de0-2956-472d-abcf-75a627e77204/
TA贡献1811条经验 获得超6个赞
您可以在不指定类型的情况下初始化一个并在之后分配它。Python 会假装每个人都是成年人,所以..
例如:
class A:
def __init__(self, val):
self.val = val
self.b = None
class B:
def __init__(self, a_val):
self.a = A(a_val)
a_val = 1
b = B(1)
a = b.a
a.b = b
添加回答
举报