为什么我的循环还在运行?(Python Zelle图形)
我试图弄清楚为什么我的一个函数中的为什么我的循环还在运行?(Python Zelle图形),python,loops,zelle-graphics,Python,Loops,Zelle Graphics,我试图弄清楚为什么我的一个函数中的while循环即使在图形中的点相等后仍在运行,也就是我将其设置为停止时。我做错什么了吗?我试着换其他的东西让它工作,但没有运气。 这是一个游戏——当角色到达结束框时,循环需要中断,但在我显式地将其编码到结束框后,循环就不会这样做了。这是我的第二个功能: from graphics import * def field(): #creating the window win = GraphWin('The Field',400,400)
while
循环即使在图形中的点相等后仍在运行,也就是我将其设置为停止时。我做错什么了吗?我试着换其他的东西让它工作,但没有运气。
这是一个游戏——当角色到达结束框时,循环需要中断,但在我显式地将其编码到结束框后,循环就不会这样做了。这是我的第二个功能:
from graphics import *
def field():
#creating the window
win = GraphWin('The Field',400,400)
win.setBackground('white')
#drawing the grid
boxlist = []
for i in range(0,400,40):
for j in range(0,400,40):
box = Rectangle(Point(i,j),Point(i+40,j+40))
box.setOutline('light gray')
box.draw(win)
boxlist.append(box)
#creating other boxes
startbox = Rectangle(Point(0,0),Point(40,40))
startbox.setFill('lime')
startbox.setOutline('light gray')
startbox.draw(win)
endbox = Rectangle(Point(360,360),Point(400,400))
endbox.setFill('red')
endbox.setOutline('light gray')
endbox.draw(win)
boxlist.append(startbox)
boxlist.append(endbox)
#creating Pete
pete = Rectangle(Point(2,2),Point(38,38))
pete.setFill('gold')
pete.draw(win)
return win,boxlist,pete
def move(win2,boxlist,pete,endbox):
peteloc = pete.getCenter()
#creating loop to move pete
while peteloc != endbox.getCenter():
click = win2.getMouse()
x = click.getX()
y = click.getY()
peteloc = pete.getCenter()
petex = peteloc.getX()
petey = peteloc.getY()
#moving pete
if x>=petex+20 and y<=petey+20 and y>=petey-20:
pete.move(40,0)
elif x<=petex-20 and y<=petey+20 and y>=petey-20:
pete.move(-40,0)
elif y>=petey+20 and x<=petex+20 and x>=petex-20:
pete.move(0,40)
elif y<=petey-20 and x<=petex+20 and x>=petex-20:
pete.move(0,-40)
peteloc = pete.getCenter()
# The main function
def main():
win2,boxlist,pete = field()
endbox = boxlist[len(boxlist)-1]
move(win2,boxlist,pete,endbox)
main()
从图形导入*
def字段():
#创建窗口
win=GraphWin(“现场”,400400)
胜利。挫折(“白色”)
#绘制网格
boxlist=[]
对于范围(0400,40)内的i:
对于范围(0400,40)内的j:
长方体=矩形(点(i,j),点(i+40,j+40))
框。设置轮廓(“浅灰色”)
盒子。抽签(赢)
boxlist.append(框)
#创建其他框
startbox=矩形(点(0,0),点(40,40))
startbox.setFill(“石灰”)
startbox.setOutline(“浅灰色”)
startbox.draw(赢)
端盒=矩形(点(360360),点(400400))
endbox.setFill('red')
endbox.setOutline(“浅灰色”)
endbox.draw(赢)
boxlist.append(startbox)
boxlist.append(endbox)
#创造皮特
皮特=矩形(点(2,2),点(38,38))
彼得·塞菲尔(“黄金”)
皮特,平局(赢)
返回胜利,boxlist,pete
def移动(win2,框列表,pete,结束框):
peteloc=pete.getCenter()
#创建循环以移动pete
当彼得洛克!=endbox.getCenter():
单击=win2.getMouse()
x=单击.getX()
y=单击.getY()
peteloc=pete.getCenter()
petex=peteloc.getX()
petey=peteloc.getY()
#移动皮特
如果x>=petex+20且y=petey-20:
皮特,快走(40,0)
elif x=petey+20和x=petex-20:
皮特,移动(0,40)
elif y我想可能是因为浮子的精度。我猜pete.getCenter()和endbox.getCenter()类似于[float,float],应该避免使用=代码>介于浮点之间,例如1.0000001不等于1
因此,即使角色到达结束框,位置仍然会有一点浮动偏差
因此,您可以更改a!=b
至abs(a-b)>可接受的\u错误
当错误可接受时。示例代码如下所示:
# while peteloc != endbox.getCenter():
while abs(peteloc.getX() - endbox.getCenter().getX()) > 0.01 and abs(peteloc.getY() - endbox.getCenter().getY()) > 0.01:
希望这能对您有所帮助。Zelle graphicsPoint
对象似乎永远不会进行相等的比较:
>>> from graphics import *
>>> a = Point(100, 100)
>>> b = Point(100, 100)
>>> a == b
False
>>>
我们必须提取坐标并进行我们自己的比较。尽管@recnac提供了一个可行的解决方案(+1),但我将推荐一个更通用的解决方案。我们将创建一个distance()
方法,该方法对从\u BBox
继承的任何对象都有效,它包括矩形
、椭圆形
、圆形
和直线
:
def distance(bbox1, bbox2):
c1 = bbox1.getCenter()
c2 = bbox2.getCenter()
return ((c2.getX() - c1.getX()) ** 2 + (c2.getY() - c1.getY()) ** 2) ** 0.5
我们现在可以测量物体之间的距离,水平、垂直和对角。由于长方体一次移动20个像素,因此我们可以假设如果它们彼此间隔1个像素,则它们位于同一位置。您的代码重写为使用distance()
方法和其他调整:
from graphics import *
def field(win):
# drawing the grid
boxlist = []
for i in range(0, 400, 40):
for j in range(0, 400, 40):
box = Rectangle(Point(i, j), Point(i + 40, j + 40))
box.setOutline('light gray')
box.draw(win)
boxlist.append(box)
# creating other boxes
startbox = Rectangle(Point(0, 0), Point(40, 40))
startbox.setFill('lime')
startbox.setOutline('light gray')
startbox.draw(win)
boxlist.append(startbox)
endbox = Rectangle(Point(360, 360), Point(400, 400))
endbox.setFill('red')
endbox.setOutline('light gray')
endbox.draw(win)
boxlist.append(endbox)
# creating Pete
pete = Rectangle(Point(2, 2), Point(38, 38))
pete.setFill('gold')
pete.draw(win)
return boxlist, pete
def distance(bbox1, bbox2):
c1 = bbox1.getCenter()
c2 = bbox2.getCenter()
return ((c2.getX() - c1.getX()) ** 2 + (c2.getY() - c1.getY()) ** 2) ** 0.5
def move(win, pete, endbox):
# creating loop to move pete
while distance(pete, endbox) > 1:
click = win.getMouse()
x, y = click.getX(), click.getY()
peteloc = pete.getCenter()
petex, petey = peteloc.getX(), peteloc.getY()
# moving pete
if x >= petex + 20 and petey - 20 <= y <= petey + 20:
pete.move(40, 0)
elif x <= petex - 20 and petey - 20 <= y <= petey + 20:
pete.move(-40, 0)
elif y >= petey + 20 and petex - 20 <= x <= petex + 20:
pete.move(0, 40)
elif y <= petey - 20 and petex - 20 <= x <= petex + 20:
pete.move(0, -40)
# The main function
def main():
# creating the window
win = GraphWin('The Field', 400, 400)
win.setBackground('white')
boxlist, pete = field(win)
endbox = boxlist[-1]
move(win, pete, endbox)
main()
从图形导入*
def字段(win):
#绘制网格
boxlist=[]
对于范围(0、400、40)内的i:
对于范围(0、400、40)内的j:
长方体=矩形(点(i,j),点(i+40,j+40))
框。设置轮廓(“浅灰色”)
盒子。抽签(赢)
boxlist.append(框)
#创建其他框
startbox=矩形(点(0,0),点(40,40))
startbox.setFill(“石灰”)
startbox.setOutline(“浅灰色”)
startbox.draw(赢)
boxlist.append(startbox)
端点框=矩形(点(360,360),点(400,400))
endbox.setFill('red')
endbox.setOutline(“浅灰色”)
endbox.draw(赢)
boxlist.append(endbox)
#创造皮特
皮特=矩形(点(2,2),点(38,38))
彼得·塞菲尔(“黄金”)
皮特,平局(赢)
返回装箱单,皮特
def距离(bbox1、bbox2):
c1=bbox1.getCenter()
c2=bbox2.getCenter()
返回((c2.getX()-c1.getX())**2+(c2.getY()-c1.getY())**2)**0.5
def移动(win、pete、endbox):
#创建循环以移动pete
当距离(皮特,收尾盒)>1时:
click=win.getMouse()
x、 y=单击.getX(),单击.getY()
peteloc=pete.getCenter()
petex,petey=peteloc.getX(),peteloc.getY()
#移动皮特
如果x>=petex+20和petey-20欢迎来到Stackoverflow,请提供一个,并向我们展示您想要什么,您当前得到什么,或者您当前的错误消息,以及您到目前为止尝试了什么,还可以看到,也可以看到非常清晰和详细的答案,一些小建议:为了获得更好的性能,您可以使用sqr_distance
而不是distance
,以避免平方根操作。这就是我们的游戏在游戏开发中所做的。