2016-04-30 9 views
0

X軸に沿って移動する矩形を取得しようとしていますが、checkMouse()は機能しません。それを機能させるために何をする必要がありますか?checkMouse()を使用してX軸に沿って移動する矩形を取得する方法は?

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) 

答えて

0

あなたはループを逃しているとあなたがcheckMouse()に切り替える必要性をしたまで、私が最初にgetMouse()での作業をお勧めします:

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:から削除します。

関連する問題