0

我希望矩形移动到我点击鼠标的地方,有什么想法吗?我已经尝试了一切,但似乎没有任何效果。这适用于球会掉落并且矩形必须接住球的项目。我只需要矩形沿着 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)
4

1 回答 1

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 计时器事件,我们将不得不凑合。

于 2017-01-09T03:34:50.587 回答