2013-05-22 128 views
3

我是一名大學生。我在我的第二個補習課程中,我們沒有太多簡單的課程和功能,所以我沒有真正能夠充分利用我在互聯網上找到的複雜術語。Python:相互「交談」的課程

我正在做一個蟑螂爲患SIM卡,我需要能夠有一個「蟑螂」級能夠檢測,其中「食品」類是在一個窗口。我的老師告訴我,我最好的選擇可能是創建另一個類似於兩者之間的中間人的類似智囊團的類,但我真的不知道這是如何工作的。誰能幫我嗎?我通過Calico使用Python。下面是我有蟑螂類(編輯:想我應該表現在main()等也是如此):

class Roach: 
    def __init__(self, win, placex, placey): 
     self.timer = 320 + (10 *(int(10 *random.random()))) 
     self.speed = 0.2 
     self.heading = 360 
     self.win = win 
     self.x = placex 
     self.y = placey 
     self.body = self.makeBody() 
     self.body.draw(self.win) 
    def __str__(self): 
     t = "Roach at ("+str(self.x)+","+str(self.y)+")" 
     return t 
    def makeBody(self): 
     body = Rectangle((-5,-5),(5,5)) 
     body.setFill(Color('Brown')) 
     body.moveTo(self.x,self.y) 
     return body 
    def move(self, win): 
     self.x = self.x + self.speed *sin(self.heading) 
     self.y = self.y + self.speed *cos(self.heading) 
     self.body.moveTo(self.x,self.y) 
     self.avoid(win) 
    def avoid(self, win): 
     Border = False 
     if self.x >= win.getWidth(): 
      self.setHeading(random.randrange(91, 269)) 
      self.x = win.getWidth() - 1 
      Border = True 
     elif self.x <= 0: 
      self.setHeading(random.randrange(271, 449)) 
      self.x = 1 
      Border = True 
     elif self.y >= win.getHeight(): 
      self.setHeading(random.randrange(1, 179)) 
      self.y = win.getHeight() - 1 
      Border = True 
     elif self.y <= 0: 
      self.setHeading(random.randrange(181, 359)) 
      self.y = 1 
      Border = True 
     return Border 
     #Getters 
    def getSpeed(self): 
     return self.speed 
    def getHeading(self): 
     return self.heading 
    def getX(self): 
     return self.x 
    def getY(self): 
     return self.y 
    #Setters 
    def setSpeed(self, speed): 
     self.speed = speed 
    def setHeading(self, heading): 
     self.heading = heading 
     self.body.rotateTo(self.heading) 
    def setX(self, x): 
     self.x = x 
    def setY(self, y): 
     self.y = y 


def main(): 
    win = Window(400, 400) 
    win.setBackground(Color("White")) 
    Rpop = [] 
    Wpop = [] 
    i = 320 
    menu(win, Rpop, Wpop) 
    while getKeyPressed() != 'Escape': 
     for roach in Rpop: 
      if roach.avoid(win) == True: 
       roach.avoid(win) 
      elif i % roach.timer == 0: 
       roach.setHeading(360*random.random()) 
      roach.move(win) 
     if getKeyPressed() == 'm': 
      menu(win, Rpop, Wpop) 
     i = i + 1 


def menu(win, Rpop, Wpop): 
    while getKeyPressed() != 's': 
     if getKeyPressed() == 'r': 
      Rpop.append(Roach(win,getMouseNow()[0],getMouseNow()[1])) 
     if getKeyPressed() == 'w': 
      point1 = getMouse() 
      dot1 = Circle((point1[0],point1[1]), 3) 
      dot1.draw(win) 
      point2 = getMouse() 
      dot2 = Circle((point2[0],point2[1]), 3) 
      dot2.draw(win) 
      Wpop.append(Wall(win, point1[0], point1[1], point2[0], point2[1])) 
    return 

main() 

對不起,我缺乏的意見和可能的青少年節目。非常感謝。

回答

1

這裏是一個老的如何使用一個額外的「大師」級,允許作爲OP被提到了另外兩類溝通粗線條的:

class Master(): 
    ... 
    def is_food_there(self, coords): 
     for food_instance in self.food_instances: 
      if coords == food_instance.coords: 
       return True 

class Food(): 
    def __init__(self, master_instance): 
     self.coords = ###some coordinates or whatever 
     master_instance.food_instances.append(self) ### register this food with master 

class Roach(): 
    def __init__(self, master_instance): 
     self.master_instance = master_instance 
    ... 
    def move(self, newlocation): 
     if(self.master_instance.is_food_there(newlocation)): ### check with master for food coords 
      ###do something 

的想法是,我們有法師類基本上是用於食品實例列表的容器,和羅奇類就知道哪個主控實例屬於,所以它可以訪問大師的「is_food_there」功能,這反過來會仔細檢查屬於它的食品的情況。在這個例子中,大師班是「中間人」。

+0

謝謝你。只是一個簡單的問題:沒有「master_instance」的意思是「主=主()」或者是你談論的主類(食品相同的問題)的屬性?我只是在理解如何重新格式化代碼時遇到輕微問題。 – user2407969

+0

是的,我認爲你有。 '主=主()'將使名爲'master'的了'master'的類的實例。對於'Roach'類,每個'Roach'實例將具有屬性'master_instance'它指向'master'的類的實例,其該'Roach'實例所屬 – qwwqwwq