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()
對不起,我缺乏的意見和可能的青少年節目。非常感謝。
謝謝你。只是一個簡單的問題:沒有「master_instance」的意思是「主=主()」或者是你談論的主類(食品相同的問題)的屬性?我只是在理解如何重新格式化代碼時遇到輕微問題。 – user2407969
是的,我認爲你有。 '主=主()'將使名爲'master'的了'master'的類的實例。對於'Roach'類,每個'Roach'實例將具有屬性'master_instance'它指向'master'的類的實例,其該'Roach'實例所屬 – qwwqwwq