Python:互相“交谈”的类

2024-10-01 15:43:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名大学生。我在上第二节comp-sci课程,我们没有太多的过去只是简单地在其中生成类和函数,所以我真的无法充分利用在互联网上找到的复杂行话。在

我正在制作一个蟑螂感染的模拟,我需要有一个“蟑螂”类能够检测“食物”类在窗口中的位置。我的老师告诉我,我最好的办法可能是让另一个像策划者一样的班,在这两个中间人之间,但我真的不知道那会怎样。有人能帮我吗?我通过印花布使用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()

很抱歉没有评论和可能的青少年节目。非常感谢你。在


Tags: selftruereturnifdefbodyrandomwin
1条回答
网友
1楼 · 发布于 2024-10-01 15:43:07

下面是一个粗略的概述,您可以如何使用一个额外的“Master”类来允许另外两个类进行通信,正如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

我们有一个主类,它本质上是一个食物实例列表的容器,而Roach类知道它属于哪个主实例,所以它可以访问Master的“is_Food_there”函数,该函数反过来查看属于它的食物实例。在这个例子中,主类是“中间人”。在

相关问题 更多 >

    热门问题