2015-05-29 59 views
0

我目前使用graphics.py,我想弄清楚如何將一堆線條移動到一起。我寫的程序繪製了一個網格線,我不確定我會如何寫一些東西,每隔X秒將整個網格移動Y秒。有沒有辦法將所有的行合併在一起?或者,如何去做這件事?如何移動一組對象?

from graphics import * 
win = GraphWin("Thing",500,500) 
win.setCoords(0.0, 0.0, 10.0, 10.0) 
a = 0 
slength = 1 
while True: 
    a = a + slength 
    vgrid = Line(Point(a,0),Point(a,500)) 
    hgrid = Line(Point(0,a),Point(500,a)) 
    vgrid.draw(win) 
    hgrid.draw(win) 
+0

我建議你瞭解線性變換。 –

回答

0

您可以嘗試將它們存儲在一個類中,然後一起訪問它們的位置。請記住,我對graphics.py庫一無所知,但你應該可以執行諸如Line.Point1 = Point(b, c)之類的操作。那麼你應該可以這樣做:

win = GraphWin("Thing",500,500) 
win.setCoords(0.0, 0.0, 10.0, 10.0) 

class MyDrawing: 

    def __init__(self): 

     self.horizontals = [] 
     self.verticals = [] 
     a = 0 
     slength = 1 

     while True: 
      a = a + slength 
      self.verticals.append(Line(Point(a,0),Point(a,500))) 
      self.horizontals.append(hgrid = Line(Point(0,a),Point(500,a))) 
     for line in self.verticals: 
      line.draw(win) 
     for line in self.horizontals: 
      line.draw(win) 

    def move(self, x, y): 

     for line in self.verticals: 
      line.Point1 = Point(line.Point1.x + x, line.Point1.y + y) 
      line.Point2 = Point(line.Point2.x + x, line.Point2.y + y) 
     for line in self.horizontals: 
      line.Point1 = Point(line.Point1.x + x, line.Point1.y + y) 
      line.Point2 = Point(line.Point2.x + x, line.Point2.y + y) 
+0

但是我也會建議一個爲更高級別的圖形編程而建立的庫(這看起來並不像 - 圖形聽起來和低級編程一樣低)。你應該看看pygame和Kivy! – Dagrooms