2014-10-04 109 views
1

我想用蟒蛇繪製多米尼加國旗(不要與多米尼加國旗混淆)。我目前在代碼中除鳥之外(我不知道該怎麼做)。我似乎也無法弄清楚如何讓10顆星在紅色圓圈內適當地出現,星星內部沒有任何線條。如果我能夠得到一些關於如何將星體置於圓內的技巧以及如何去處理中間的鳥,這將是非常棒的。謝謝!使用Python烏龜繪製多米尼加國旗(不是多米尼加)

這裏是它的圖像:

enter image description here

這裏是我的代碼至今:

import turtle 

wn = turtle.Screen() 
alex = turtle.Turtle() 


def drawRect(): 
    alex.speed(0) 
    alex.up() 
    alex.fillcolor("green") 
    alex.begin_fill() 
    alex.setpos(-250, -100) 
    alex.down() 
    for i in range(2): 
     alex.forward(500) 
     alex.left(90) 
     alex.forward(300) 
     alex.left(90) 
    alex.end_fill() 
drawRect() 

def drawLines(): 
    alex.speed(0) 
    alex.fillcolor("yellow") 
    alex.begin_fill() 
    alex.penup() 
    alex.setpos(-250, 70) 
    alex.pendown() 
    for i in range(2): 
     alex.forward(500) 
     alex.left(90) 
     alex.forward(30) 
     alex.left(90) 
    alex.end_fill() 

    alex.fillcolor("black") 
    alex.begin_fill() 
    alex.penup() 
    alex.setpos(-250, 40) 
    alex.pendown() 
    for i in range(2): 
     alex.forward(500) 
     alex.left(90) 
     alex.forward(30) 
     alex.left(90) 
    alex.end_fill() 

    alex.pencolor("white") 
    alex.fillcolor("white") 
    alex.begin_fill() 
    alex.penup() 
    alex.setpos(-250, 10) 
    alex.pendown() 
    for i in range(2): 
     alex.forward(500) 
     alex.left(90) 
     alex.forward(30) 
     alex.left(90) 
    alex.end_fill() 

    alex.speed(0) 
    alex.fillcolor("yellow") 
    alex.begin_fill() 
    alex.penup() 
    alex.setpos(-40, -100) 
    alex.pendown() 
    for i in range(2): 
     alex.forward(30) 
     alex.left(90) 
     alex.forward(300) 
     alex.left(90) 
    alex.end_fill() 

    alex.speed(0) 
    alex.fillcolor("black") 
    alex.begin_fill() 
    alex.penup() 
    alex.setpos(-10, -100) 
    alex.pendown() 
    for i in range(2): 
     alex.forward(30) 
     alex.left(90) 
     alex.forward(300) 
     alex.left(90) 
    alex.end_fill() 

    alex.speed(0) 
    alex.pencolor("white") 
    alex.fillcolor("white") 
    alex.begin_fill() 
    alex.penup() 
    alex.setpos(20, -100) 
    alex.pendown() 
    for i in range(2): 
     alex.forward(30) 
     alex.left(90) 
     alex.forward(300) 
     alex.left(90) 
    alex.end_fill() 

drawLines() 

def drawCircle(): 
    alex.speed(0) 
    alex.up() 
    alex.setpos(10, -50) 
    alex.down() 
    alex.fillcolor("red") 
    alex.begin_fill() 
    alex.circle(100) 
    alex.end_fill() 
drawCircle() 

def drawStars(): 
    alex.speed(0) 
    alex.pencolor("green") 
    alex.fillcolor("green") 
    alex.begin_fill() 

    for i in range(5): 
     alex.forward(20) 
     alex.right(144) 

def makeStars(): 
    alex.penup() 
    alex.setpos(25, -10) 
    alex.pendown() 
    for i in range(11): 
     drawStars() 
     alex.left(35) 
     alex.penup() 
     alex.forward(45) 
     alex.pendown() 

makeStars() 
+0

你可以直接顯示它:'image = tkinter.PhotoImage(file ='/ path/to/dominica-flag.png')' – jfs 2014-10-04 23:12:04

+0

只是爲了顯示標誌,看起來像SVG(http ://upload.wikimedia.org/wikipedia/commons/c/c4/Flag_of_Dominica.svg獲得寬鬆許可)。如果這是一種烏龜學習練習,你甚至可以將它的路徑轉換成烏龜命令,儘管這並不是微不足道的(首先你要查看SVG路徑語法,然後在座標尺度和形狀描述方式之間進行轉換。 ..)。 – twotwotwo 2014-10-05 08:15:46

+0

這是一個海龜學習練習。所以我不能只是得到一張照片並顯示它。我需要從頭開始儘可能詳細地描述它。 – Nate 2014-10-05 19:35:06

回答

0

這是家庭作業,或者說你做了你自己-學習?無論哪種方式,繪製鸚鵡是而不是會很容易!兩個連接的SVG使用超過40個扭曲的橢圓和大約30個線性路徑;我想它是在矢量繪圖程序(例如Inkscape)中創建的。

即使你不熟悉SVG該文件仍可能對你有用。因爲它來自維基百科,所以我認爲它是當前官方多米尼加國旗的很好代表,所以元素的大小,位置和顏色應該相當準確。

這裏有一些從該文件的顏色:

yellow: "#fcd116" 
green: "#006b3f" 
red: "#d41c30" 

烏龜模塊瞭解到色彩使用該格式。

...

現在,到您目前的問題。

我還沒有用許多年的龜圖形,但你啓發我安裝tkinter,以便我可以修補你的代碼。我注意到的第一個問題是makeStars()中的alex.left(35),應該是36,因爲360°/ 10 = 36°。

我嘗試了各種東西來獲得明星圈正確居中,和正確對齊的明星,但一個小時後還是這樣的挫折我放棄了簡單的繪畫命令。我相信這對烏龜專家來說是一件輕而易舉的事情,但我決定採用稍微更多的三角函數法。

由於龜模塊不填充的方式,有畫充滿星星沒有簡單的方法。一些矢量繪圖系統有替代填充模式來解決這個問題,但龜似乎沒有這樣做。所以你要麼用五邊形填充星的中心,要麼畫一個10邊的多邊形。

我寫了一些代碼,繪製了正確居中和對齊的星星。我的代碼中的大小不是,正好是與SVG中的大小相同,但它們非常接近。如果這是爲了您自己的自學習練習,我很高興在這裏發佈我的代碼。 OTOH,如果這是我的功課也許不應該張貼整個事情...

PS。如果您先定義所有函數,然後在程序底部調用它們(最好在main()函數中),則它更易於閱讀代碼。將函數調用與函數定義分開有點麻煩。