2012-12-08 54 views
0

我需要在Python 3中創建拼湊。我所要做的就是創建一個循環,使設計邊框成爲圖形窗口。我知道我需要一個for循環,但我不知道如何做到這一點。python3中的循環拼湊

這是我到目前爲止有:

from graphics import * 

def main(): 
    height = eval(input("What is the height of the window")) 
    width = eval(input("What is the width of the window")) 
    colour = input("enter the colour of the patch") 
    win = GraphWin("Patch", 100*width, 100*height) 
    boat_x = 0 
    boat_y = 0 
    for x in range (4): 
     boat(win, boat_x, boat_y, colour) 
     boat_x = boat_x + 23 
    for i in range(height * 5): 
     boat(win, boat_x, boat_y, colour) 
     boat_x = boat_x + 24 
    for j in range(height * 5): 
     boat(win, boat_x, boat_y, colour) 
     boat_y = boat_y + 100 
    win.getMouse() 
    win.close() 



def boat(win, x, y, colour): 
    body1 = Polygon(Point(1+x,95+y), Point(5+x,100+y), 
       Point(20+x,100+y), Point(24+x,95+y)) 
    body1.draw(win) 
    line1 = Line(Point(13+x,95+y), Point(13+x,90+y)) 
    line1.draw(win) 
    sail1 = Polygon(Point(1+x,90+y), Point(24+x,90+y), Point(13+x, 73+y)) 
    sail1.setFill(colour) 
    sail1.draw(win) 
    body2 = Polygon(Point(1+x, 63), Point(5+x, 68), 
         Point(20+x,68), Point(24+x,63)) 
    body2.draw(win) 
    line2 = Line(Point(13+x,63), Point(13+x,58)) 
    line2.draw(win) 
    sail2 = Polygon(Point(1+x,58), Point(24+x, 58), Point(13+x,40)) 
    sail2.setFill(colour) 
    sail2.draw(win) 
    body3 = Polygon(Point(1+x,28), Point(5+x,33), 
         Point(20+x,33), Point(24+x, 28)) 
    body3.draw(win) 
    line3 = Polygon(Point(13+x,28), Point(13+x,23)) 
    line3.draw(win) 
    sail3 = Polygon(Point(1+x,23), Point(24+x, 23), Point(13+x, 5)) 
    sail3.setFill(colour) 
    sail3.draw(win) 

main() 

到目前爲止,創建頂部邊框,但沒有別的。 我也知道,船的功能不是最有效的繪圖方式

+0

你的循環應該做什麼? '從圖形導入*'似乎並不熟悉,你使用的是什麼GUI庫/工具包? – mata

回答

0

當你說你需要「使設計邊框成爲圖形窗口」我假設你想讓你的設計重複幾次沿窗口的每個邊緣(即頂部,底部,左側和右側)。

這應該是在兩個循環中可行的。一個將繪製頂部和底部邊緣,另外兩個繪製左側和右側邊緣。我不太確定你的繪圖代碼是如何工作的,所以我在一些偏移這裏猜測:

top = 0 
bottom = (height-1) * 100 

for x in range(0, width*100, 25): 
    boat(x, top, colour) 
    boat(x, bottom, colour) 

left = 0 
right = width * 100 - 25 

for y in range(100, (height-1)*100, 100): 
    boat(left, y, colour) 
    boat(right, y, colour) 

這應該打電話給你boat子程序橫跨頂部和底部的每25個像素,每100個像素沿左邊和右邊。調整top,bottom,left,right值和range調用循環中的參數以使間距滿足您的需求(我剛剛完成)。此代碼避免了兩次繪製角點項目,但取決於繪圖例程的工作方式,可能不需要。