3

我是一名大学生。我在我的第二个 comp sci 课程中,我们没有太多过去只是在其中创建类和函数,所以我还没有真正能够充分利用我在互联网上找到的复杂术语。

我正在制作蟑螂侵扰模拟,我需要能够让“蟑螂”类能够检测到“食物”类在窗口中的位置。我的老师告诉我,我最好的办法可能是再开一个类似策划师的课程,它是介于两者之间的中间人,但我真的不知道这会如何运作。谁能帮我吗?我正在通过 Calico 使用 Python。这是我对 roach 类的了解(编辑:猜想我也应该显示 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()

很抱歉缺乏评论和可能的青少年编程。太感谢了。

4

1 回答 1

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

这个想法是我们有一个 Master 类,它本质上是一个 Food 实例列表的容器,Roach 类知道它属于哪个 Master 实例,因此它可以访问该 master 的“is_food_there”函数,该函数反过来查看 Food属于它的实例。在这个例子中,大师班是“中间人”。

于 2013-05-22T05:29:09.273 回答