如何使用checkMouse()使矩形沿X轴移动?

时间:2016-04-30 19:25:39

标签: python-3.x zelle-graphics

我正试图让一个矩形沿X轴移动,但checkMouse()无效。需要做些什么来使其发挥作用?

from graphics import*
import time
from random import randrange

wd=GraphWin("Catch A Ball",500,500)
wd.setBackground("lightblue")

p1=220 #size of rectangle 
p2=250

for i in range(1):
spt1=Point(p1,480)
spt2=Point(p2,500)
rct=Rectangle(spt1,spt2)
rct.setOutline("black")
rct.setFill("black")
rct.draw(wd)

p=wd.checkMouse()
c=rct.getCenter()
dx=p.getX() - c.getX()
dy=p.getY() - c.getY()
rct.move(dx,0)

1 个答案:

答案 0 :(得分:0)

您错过了一个循环,我建议您最初使用getMouse(),直到您需要切换到checkMouse()

from graphics import *

window = GraphWin("Catch A Ball", 500, 500)
window.setBackground("lightblue")

point1 = Point(220, 480)  # size of rectangle
point2 = Point(250, 500)

rectangle = Rectangle(point1, point2)
rectangle.setFill("black")
rectangle.draw(window)

while True:
    point = window.getMouse()

    if point is not None:  # so we can switch to checkMouse() if desired
        center = rectangle.getCenter()
        dx = point.getX() - center.getX()
        dy = point.getY() - center.getY()
        rectangle.move(dx, 0)

您需要添加某种操作/事件才能突破while True:无限循环。