移动Python graphics.py对象

时间:2014-07-08 18:31:51

标签: python zelle-graphics

我试图在python图形中同时移动两个对象(这似乎是指John Zelle's graphics.py),然后在循环中重复移动。然而,当我尝试循环它时,形状消失。我该如何解决这个问题?

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

1 个答案:

答案 0 :(得分:0)

您的代码有一些明显的问题:您将圆的中心Point对象与圆形对象进行比较 - 您需要组合Point对象;你在win.getMouse()电话中留下了括号。下面的返工修复了这些问题:

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

不是将中心点与Point进行比较,而是简单地检查了X位置,因为它正在水平移动。

相关问题