[英]Moving Python graphics.py objects
我試圖在 python 圖形中同時移動兩個對象(這似乎是指John Zelle 的 graphics.py ),然后循環重復移動。 但是,當我嘗試循環它時,形狀消失了。 我怎樣才能解決這個問題?
def main():
win = GraphWin('Lab Four', 400, 400)
c = Circle(Point(100, 50), 40)
c.draw(win)
c.setFill('red')
s = Rectangle(Point(300, 300), Point(350, 350))
s.draw(win)
s.setFill('blue')
s.getCenter()
while not c.getCenter() == Circle(Point(400, 50), 40):
c.move(10, 0)
s.move(-10, 0)
win.getMouse
while not (win.checkMouse()):
continue
win.close()
您的代碼有幾個明顯的問題:您將圓的中心 Point 對象與圓對象進行比較——您需要與 Point 對象進行比較; 你在win.getMouse()
調用中win.getMouse()
了括號。 下面的返工解決了這些問題:
from graphics import *
WIDTH, HEIGHT = 400, 400
RADIUS = 40
def main():
win = GraphWin('Lab Four', WIDTH, HEIGHT)
c = Circle(Point(100, 50), RADIUS)
c.draw(win)
c.setFill('red')
s = Rectangle(Point(300, 300), Point(350, 350))
s.draw(win)
s.setFill('blue')
while c.getCenter().getX() < WIDTH - RADIUS:
c.move(10, 0)
s.move(-10, 0)
win.getMouse()
win.close()
main()
我沒有將中心點與點進行比較,而是簡單地檢查了 X 位置,因為它是水平移動的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.