2016-04-30 79 views
0

我想矩形移動到哪裏我點擊我的鼠標,任何想法? 我試過一切,似乎沒有任何工作。這是一個項目,球將落下,矩形將不得不接球。我只需要矩形沿在以往任何時候點擊鼠標時使用checkMouse()移動矩形沿X軸()

from graphics import* 
import time 
from random import randrange 

wd=GraphWin("Catch A Ball",500,500)#size of window 
wd.setBackground("lightblue") 


p1=220 #size of rectangle # size of rectangle 
p2=250 


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

p=wd.getMouse() # defining the y and x axis 
c=rct.getCenter() 
dx=p.getX() - c.getX() 
dy=p.getY() - c.getY() 
rct.move(dx,0) 

回答

0

我只需要矩形沿在以往任何時候鼠標 點擊X軸移動X軸移動

你的代碼的下面返工應該做你的描述:

from graphics import * 

SCREEN_WIDTH, SCREEN_HEIGHT = 500, 500 # size of window 

BOX_WIDTH, BOX_HEIGHT = 30, 20 

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

for i in range(1): 
    ll = Point(SCREEN_WIDTH/2 - BOX_WIDTH/2, SCREEN_HEIGHT) 
    ur = Point(SCREEN_WIDTH/2 + BOX_WIDTH/2, SCREEN_HEIGHT - BOX_HEIGHT) 
    rectangle = Rectangle(ll, ur) # outline of rectangle 
    rectangle.setFill("black") 
    rectangle.draw(window) 

while True: 
    point = window.getMouse() # obtain cursor position 
    center = rectangle.getCenter() 
    dx = point.getX() - center.getX() 
    rectangle.move(dx, 0) 

我不是無限循環完全舒適,但由於Zelle圖形不暴露任何Tkinter計時器事件,所以我們已經做好了。