3 回答

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
你不
而是使用參數(shù)
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)
但更好的是定義一個(gè)基礎(chǔ)對(duì)象類型來定義你的接口
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貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
您沒有依賴循環(huán)問題。但是,你有以下問題,
您正在嘗試使用 AI 對(duì)象,但您沒有在任何地方創(chuàng)建該對(duì)象。它需要看起來像,
foo = AI() #creating the object bar(foo) #using the object
周圍的語法錯(cuò)誤
canvas.coords(AI object)
。調(diào)用函數(shù)的方式是
foo(obj)
沒有類型。在定義函數(shù)時(shí),您可以選擇提及類型,例如
def foo(bar : 'AI'):
你可以相互依賴類的證明,https://pyfiddle.io/fiddle/b75f2de0-2956-472d-abcf-75a627e77204/

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以在不指定類型的情況下初始化一個(gè)并在之后分配它。Python 會(huì)假裝每個(gè)人都是成年人,所以..
例如:
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
添加回答
舉報(bào)