为什么我的循环仍在运行? (Python Zelle图形)

时间:2019-03-29 03:15:26

标签: python loops zelle-graphics

我试图弄清楚为什么即使在我的图形中的点相等之后,我的一个函数中的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()

2 个答案:

答案 0 :(得分:1)

我认为这可能是由于浮动精度引起的。我猜pete.getCenter()和endbox.getCenter()类似于[float,float],您应该避免在浮点之间使用!=,例如1.0000001不等于1。

因此,即使角色到达收尾箱,该位置仍会产生一些浮动偏差。

因此,当错误可接受时,您可以将a != b更改为abs(a - b) > acceptable_error。示例代码如下:

# while peteloc != endbox.getCenter():
while abs(peteloc.getX() - endbox.getCenter().getX()) > 0.01 and abs(peteloc.getY() - endbox.getCenter().getY()) > 0.01:

希望对您有帮助。

答案 1 :(得分:1)

Zelle图形Point对象似乎不会比较相等:

>>> from graphics import *
>>> a = Point(100, 100)
>>> b = Point(100, 100)
>>> a == b
False
>>> 

我们必须提取坐标并进行自己的比较。尽管@recnac提供了一个可行的解决方案(+1),但我将建议一个更通用的解决方案。我们将创建一个distance()方法,该方法对从_BBox继承的任何对象均有效,其中包括RectangleOvalCircleLine

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()