gpt4 book ai didi

Python:彼此为 "talk"的类

转载 作者:行者123 更新时间:2023-11-28 18:48:31 28 4
gpt4 key购买 nike

我是一名大学生。我正在上我的第二个 comp sci 类(class),我们还没有太多简单地在其中制作类和函数,所以我还没有真正能够充分利用我在互联网上找到的复杂行话。

我正在制作蟑螂出没模拟游戏,我需要让“蟑螂”类能够检测窗口中“食物”类的位置。我的老师告诉我,我最好的办法可能是制作另一个类似策划者的类(class),它是两者之间的中间人,但我真的不知道那会如何运作。谁能帮我吗?我通过 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()

对于缺少评论和可能的青少年编程感到抱歉。非常感谢。

最佳答案

这是关于如何使用额外的“主”类来允许其他两个类进行通信的粗略概述,正如 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 实例。在这个例子中,Master 类是“中间人”。

关于Python:彼此为 "talk"的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683567/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com