0

I'm trying to get a rectangle to move along the X axis but checkMouse() isn't working. What needs to be done to make it work?

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

1 回答 1

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:无限循环。

于 2017-01-18T19:52:07.480 回答