'Moving Python graphics.py objects
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()
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cdlane |
