2

I'm trying to figure out why the while loop in one of my functions is still running even after the points in my graphics are equal, which is when I set it to stop. Is there anything I'm doing wrong? I've tried to switch other things around to get it to work but no luck. It's for a game--when the character reaches the endbox the loop needs to break, but it isn't doing that after I explicitly coded it to. It's in the second function I have:

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81
jklo
  • 31
  • 2
  • Welcome to Stackoverflow, Please provide a **[Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)**, and show us what you want, what you're currently getting, or your current error message, and what you've tried so far, also see [**How do I ask a good question?**](https://stackoverflow.com/help/how-to-ask), and also see [**How to write a perfect question**](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question) – U13-Forward Mar 29 '19 at 03:16

2 Answers2

1

I think maybe it is caused by precision of float. I guess pete.getCenter() and endbox.getCenter() are something like [float, float], you should avoid using != between float, such as 1.0000001 is not equal to 1.

So even if the character reaches the endbox, the position will still get a little float bias.

So you can change a != b to abs(a - b) > acceptable_error when the error is acceptable. Sample code is like:

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

Hope that will help you.

recnac
  • 3,744
  • 6
  • 24
  • 46
1

Zelle graphics Point objects don't appear to ever compare as equal:

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

We have to extract coordinates and do our own comparison. Although @recnac provides a workable solution (+1), I'm going to suggest a more general one. We'll create a distance() method that's valid for any object that inherits from _BBox, which includes Rectangle, Oval, Circle and Line:

def distance(bbox1, bbox2):
    c1 = bbox1.getCenter()
    c2 = bbox2.getCenter()

    return ((c2.getX() - c1.getX()) ** 2 + (c2.getY() - c1.getY()) ** 2) ** 0.5

We can now measure the distance between objects, horizontally, vertically and diagonally. Since your boxes are moving twenty pixels at a time, we can assume that if they are withing 1 pixel of each other, they are in the same location. Your code rewritten to use the distance() method and other tweaks:

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • 1
    very clear and elaborate answer, some little suggestion: to get a better performance, you can use `sqr_distance` instead of `distance`, to avoid the square root operation. That is what our game do in game develop. – recnac Apr 01 '19 at 02:08