I am trying to draw circle in the upper left corner of a graphics window, and then move that circle at a diagonal to the bottom right corner, and then bounce it back to the upper left, and so on until my loop is exhausted. I can't get my moving circle to reverse direction at the first bounce point (first if-statement inside for loop), and I don't know why...

from graphics import*

def main():
    # create a 200 x 200 graphics window
    win = GraphWin()
    x = 10
    y = 10
    # create a circle of radius 10 at point (10, 10)
    circle = Circle(Point(x, y), 10)
    circle.setFill("Red")
    circle.setOutline("black")
    # draw the circle in the graphics window
    circle.draw(win)
    # wait for a mouse click
    win.getMouse()
    dx = .01
    dy = .01
    for i in range (100000):
        # move the circle 
        circle.move(dx, dy)
        x = x + dx
        y = y + dy
        # when x reaches 190, reverse the direction of movement in
        # the x direction
        if x == 190:
            dx = -1*dx
        else:
            dx = dx
        # when y reaches 190, reverse the direction of movement in
        # the y direction
        if y == 190:
            dy = -1*dy
        else:
            dy = dy
        # when x reaches 10, reverse the direction of movement in
        # the x direction
        if x == 10:
            dx = -1*dx
        else:
            dx = dx
        # when y reaches 10, reverse the direction of movement in
        # the y direction
        if y == 10:
            dy = -1*dy
        else:
            dy = dy
    #wait for a mouse click
    win.getMouse()
    win.close()
main()

Using == is always dicey. Add a print statement to see what happens, and instead of ==190, use > 189.

for i in range (100000):
        print x, y
        # move the circle 
        circle.move(dx, dy)
        x = x + dx
        y = y + dy
        # when x reaches 190, reverse the direction of movement in
        # the x direction
        if x > 189:
            dx = -1*dx
#        else:
#            dx = dx   ## Huh...this statement does nothing
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.