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

时间:2016-04-30 20:13:13

标签: python python-3.x zelle-graphics

我希望矩形移动到哪里我点击鼠标,任何想法? 我已经尝试了一切,似乎没有任何工作。这是一个项目,球将下降,矩形必须接球。我只需要沿着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)

1 个答案:

答案 0 :(得分:0)

  

我只需要沿着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)

我对无限循环while True并不完全感到满意,但由于Zelle图形没有公开任何Tkinter计时器事件,我们已经做好了。