第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python 修復(fù)依賴循環(huán)

Python 修復(fù)依賴循環(huán)

阿晨1998 2021-11-02 20:23:16
我正在使用 python 開發(fā)游戲。游戲中的 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 functionsthis = 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 對(duì)象沒有定義。兩個(gè)類都依賴于另一個(gè)來工作。但是,一個(gè)還沒有定義,所以如果我把一個(gè)放在另一個(gè)之前,它會(huì)返回一個(gè)錯(cuò)誤。雖然可能只有這兩個(gè)函數(shù)有一個(gè)解決方法,但還有更多我沒有提到的函數(shù)??傊?,有沒有辦法(pythonic 或非 pythonic)在創(chuàng)建對(duì)象之前使用和/或定義它(即甚至制作更多文件)?
查看完整描述

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


查看完整回答
反對(duì) 回復(fù) 2021-11-02
?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

您沒有依賴循環(huán)問題。但是,你有以下問題,

  1. 您正在嘗試使用 AI 對(duì)象,但您沒有在任何地方創(chuàng)建該對(duì)象。它需要看起來像,

    foo = AI() #creating the object bar(foo) #using the object

  2. 周圍的語法錯(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/


查看完整回答
反對(duì) 回復(fù) 2021-11-02
?
楊魅力

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


查看完整回答
反對(duì) 回復(fù) 2021-11-02
  • 3 回答
  • 0 關(guān)注
  • 250 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)