2

I'm trying to move two objects at the same time in python graphics (This seems to be referring to John Zelle's graphics.py), then repeat the movement in a loop. However when I try to loop it, the shapes disappear. How can I fix this?

def main():
    win = GraphWin('Lab Four', 400, 400)
    c = Circle(Point(100, 50), 40)
    c.draw(win)
    c.setFill('red')
    s = Rectangle(Point(300, 300), Point(350, 350))
    s.draw(win)
    s.setFill('blue')
    s.getCenter()
    while not c.getCenter() == Circle(Point(400, 50), 40):
        c.move(10, 0)
        s.move(-10, 0)
    win.getMouse
    while not (win.checkMouse()):
        continue
    win.close()
cdlane
  • 40,441
  • 5
  • 32
  • 81
user3817694
  • 21
  • 1
  • 1
  • 2

1 Answers1

0

There are a couple of obvious problem with your code: you compare the center Point object of the circle against a circle object -- you need to compoare to a Point object; you left off the parentheses in your win.getMouse() call. The rework below fixes these issues:

from graphics import *

WIDTH, HEIGHT = 400, 400
RADIUS = 40

def main():
    win = GraphWin('Lab Four', WIDTH, HEIGHT)

    c = Circle(Point(100, 50), RADIUS)
    c.draw(win)
    c.setFill('red')

    s = Rectangle(Point(300, 300), Point(350, 350))
    s.draw(win)
    s.setFill('blue')

    while c.getCenter().getX() < WIDTH - RADIUS:
        c.move(10, 0)
        s.move(-10, 0)

    win.getMouse()
    win.close()

main()

Instead of comparing the center Point with a Point, I simply checked the X position since it's moving horizontally.

cdlane
  • 40,441
  • 5
  • 32
  • 81