为什么对象在Zelle图形中不停止移动?

2024-10-01 15:34:39 发布

您现在位置:Python中文网/ 问答频道 /正文

from graphics import *
win = GraphWin("Circle Race", 500, 500)

#red circle position
RcenterX = 50
RcenterY = 100
Rright = RcenterX+25
Rleft = RcenterY

# The Red circle
cr = Circle(Point(RcenterX,RcenterY), 25)
cr.setFill('red')
cr.setOutline('black')
cr.draw(win)


if RcenterX<=400:
    win.getMouse()
    cr.move(50 ,0)
elif RcenterX>300:
    win.getMouse()
    win.close()

我尝试在每次点击后移动红色圆圈50个像素,当圆圈的右边缘达到400时停止。 但它在达到400后继续移动? 我怎么能在400停下来?你知道吗


Tags: fromimportpositionredwincrracecircle
1条回答
网友
1楼 · 发布于 2024-10-01 15:34:39

两个基本更改:您的if语句需要是while循环;您需要重新计算RcenterX,可以自己更新它,也可以询问您的circle对象:

from graphics import *

win = GraphWin("Circle Race", 500, 500)

# red circle position
RcenterX, RcenterY = 50, 100
Rradius = 25

# The Red circle
cr = Circle(Point(RcenterX, RcenterY), Rradius)
cr.setFill('red')
cr.setOutline('black')
cr.draw(win)

while cr.getCenter().getX() + Rradius/2 < 400:  # right edge stops at 400
    win.getMouse()
    cr.move(50, 0)

win.getMouse()
win.close()

相关问题 更多 >

    热门问题