我正在尝试让我的程序以矩形方式顺时针移动“寿司盘”,尽管在我的第三条if
语句之后它似乎不会继续移动:
from graphics import *
win =GraphWin("Draw and Animate",640,480)
win.setBackground("wheat1")
#Right
xmove = 1
ymove = 0
#Down
xmove2 = 0
ymove2 = 1
#Left
xmove3 = -1
ymove3 = 0
#Up
xmove4 = 0
ymove4 = -1
sushiplate = Rectangle(Point(300,160),Point(340,180))
sushiplate.setFill("white")
sushiplate.draw(win)
leftsushi = Circle(Point(310,170), 5)
leftsushi.setOutline("Green")
leftsushi.setFill("orange")
leftsushi.draw(win)
rightsushi = Circle(Point(330,170), 5)
rightsushi.setOutline("Green")
rightsushi.setFill("orange")
rightsushi.draw(win)
while(True):
#Move sushi
sushiplate.move(xmove4, ymove4)
leftsushi.move(xmove4, ymove4)
rightsushi.move(xmove4, ymove4)
sushicenter = sushiplate.getCenter()
if (sushicenter.getY()<=130):
xmove4 = xmove
ymove4 = ymove
if (sushicenter.getX()>=510):
xmove = xmove2
ymove = ymove2
if (sushicenter.getY()>=360):
xmove2 = xmove3
ymove2 = ymove3
update(60)
我的寿司盘将上下左右移动,但将继续向下移动,并且我的第三条if
语句将不会运行:
if (sushicenter.getY()>=360):
xmove2 = xmove3
ymove2 = ymove3
答案 0 :(得分:1)
这些条件应该处于同一级别,而不是嵌套的。由于第二级如果检查sushicenter.getY() <= 130
,则只有在getY()
既是>=360
又是<=130
时才可以触发您的第四级条件,而这种情况永远不会发生。
我认为您打算将它们保持在同一水平,以便可以独立触发它们,而不依赖于其他条件。
from graphics import *
win =GraphWin("Draw and Animate",640,480)
win.setBackground("wheat1")
#Right
xmove = 1
ymove = 0
#Down
xmove2 = 0
ymove2 = 1
#Left
xmove3 = -1
ymove3 = 0
#Up
xmove4 = 0
ymove4 = -1
sushiplate = Rectangle(Point(300,160),Point(340,180))
sushiplate.setFill("white")
sushiplate.draw(win)
leftsushi = Circle(Point(310,170), 5)
leftsushi.setOutline("Green")
leftsushi.setFill("orange")
leftsushi.draw(win)
rightsushi = Circle(Point(330,170), 5)
rightsushi.setOutline("Green")
rightsushi.setFill("orange")
rightsushi.draw(win)
while(True):
#Move sushi
sushiplate.move(xmove4, ymove4)
leftsushi.move(xmove4, ymove4)
rightsushi.move(xmove4, ymove4)
sushicenter = sushiplate.getCenter()
if (sushicenter.getY()<=130):
xmove4 = xmove
ymove4 = ymove
if (sushicenter.getX()>=490):
xmove = xmove2
ymove = ymove2
if (sushicenter.getY()>=360):
xmove2 = xmove3
ymove2 = ymove3
if(sushicenter.getX()<=120):
xmove3 = xmove4
ymove3 = ymove4
update(60)
答案 1 :(得分:0)
您已经在if (sushicenter.getY()<=130):
分支下。在这种情况下,sushicenter.getY()>=360
可能不正确。
答案 2 :(得分:0)
问题在于,一旦这样的情况触发:
if (sushicenter.getX()>=510):
每次更改都会继续触发,因为结果是更改为Y方向,并且对象固定在X方向。此后的案件永远不会触发。我们可以通过先“测试水域”来解决此问题,以查看此举是否会触发更改,然后再进行更改。我在下面的代码重写中做到了这一点,但由于所有带编号的变量都令人困惑,即使在代码中给您带来错误的情况下,也试图摆脱数字而取而代之的符号:
from graphics import *
WIDTH, HEIGHT = 640, 480
BORDER = 130
RADIUS = 5
DIAMETER = RADIUS * 2
# Right
XMOVE_RIGHT = 1
YMOVE_RIGHT = 0
# Down
XMOVE_DOWN = 0
YMOVE_DOWN = 1
# Left
XMOVE_LEFT = -1
YMOVE_LEFT = 0
# Up
XMOVE_UP = 0
YMOVE_UP = -1
win = GraphWin("Draw and Animate", WIDTH, HEIGHT)
win.setBackground('wheat1')
sushiplate = Rectangle(Point(BORDER - DIAMETER * 2, BORDER - DIAMETER), Point(BORDER + DIAMETER * 2, BORDER + DIAMETER))
sushiplate.setFill('white')
sushiplate.draw(win)
leftsushi = Circle(Point(BORDER - DIAMETER, BORDER), RADIUS)
leftsushi.setOutline('green')
leftsushi.setFill('orange')
leftsushi.draw(win)
rightsushi = Circle(Point(BORDER + DIAMETER, BORDER), RADIUS)
rightsushi.setOutline('green')
rightsushi.setFill('orange')
rightsushi.draw(win)
xmove = XMOVE_RIGHT
ymove = YMOVE_RIGHT
while True:
# Move sushi
center = sushiplate.getCenter()
x, y = center.getX() + xmove, center.getY() + ymove
if x > WIDTH - BORDER:
xmove, ymove = XMOVE_DOWN, YMOVE_DOWN
elif y > HEIGHT - BORDER:
xmove, ymove = XMOVE_LEFT, YMOVE_LEFT
elif x < BORDER:
xmove, ymove = XMOVE_UP, YMOVE_UP
elif y < BORDER:
xmove, ymove = XMOVE_RIGHT, YMOVE_RIGHT
for meal_object in (sushiplate, leftsushi, rightsushi):
meal_object.move(xmove, ymove)
update(60)