2012-09-17 55 views
2

我試圖填補彩在這些正方形:我如何填寫這些正方形的烏龜 - Python的

http://i.imgur.com/kRGgR.png

眼下龜僅填充論文正方形,而不是整個廣場的角落。

這裏是我的代碼:

import turtle 
import time 
import random 

print ("This program draws shapes based on the number you enter in a uniform pattern.") 
num_str = input("Enter the side number of the shape you want to draw: ") 
if num_str.isdigit(): 
    squares = int(num_str) 

angle = 180 - 180*(squares-2)/squares 

turtle.up 

x = 0 
y = 0 
turtle.setpos(x,y) 


numshapes = 8 
for x in range(numshapes): 
    turtle.color(random.random(),random.random(), random.random()) 
    x += 5 
    y += 5 
    turtle.forward(x) 
    turtle.left(y) 
    for i in range(squares): 
     turtle.begin_fill() 
     turtle.down() 
     turtle.forward(40) 
     turtle.left(angle) 
     turtle.forward(40) 
     print (turtle.pos()) 
     turtle.up() 
     turtle.end_fill() 

time.sleep(11) 
turtle.bye() 

我試圖在沒有運氣許多地方turtle.begin_fill()end_fill()走動......使用Python 3.2.3,謝謝。

+1

它_really_混淆您已經命名的變量'廣場'當它實際上代表**的數量**。 – Aesthete

+0

太棒了,很高興它的工作。 – Aesthete

回答

5

我沒有真的用過烏龜,但它看起來可能這是你想要做的。糾正我,如果我假設這些調用的功能錯誤:

turtle.begin_fill() # Begin the fill process. 
turtle.down() # "Pen" down? 
for i in range(squares): # For each edge of the shape 
    turtle.forward(40) # Move forward 40 units 
    turtle.left(angle) # Turn ready for the next edge 
turtle.up() # Pen up 
turtle.end_fill() # End fill. 
+0

感謝這工作! – Goose

2

您正在繪製一系列三角形,每個都使用begin_fill()end_fill()。你可能會做的是移動你的電話begin_fill()end_fill()以外的內部循環,所以你畫一個完整的廣場和然後要求它被填補。

+0

謝謝,我會接受這個答案,但我只能接受一個! – Goose

0

很好,你解決了它! 如果你想讓烏龜編程更容易,請使用turtle.seth而不是turtle.left或right。 turtle.left相對於烏龜的最後一個位置,所以你不要擔心,其中烏龜是一個命令之前面臨

0

使用補

t.begin_fill() 
t.color("red") 
for x in range(4): 
    t.fd(100) 
    t.rt(90) 
t.end_fill()