2017-05-24 82 views
0

我可能會受到不喜歡的事,但我真的需要知道這一點。 我試過turtle.begin_fill()等,但沒有真正適用於我。在Python中填充三角形(海龜)

我想在Python中製作實心三角形/矩形,但我不知道如何將其實現到我的代碼中。

當前代碼:

import turtle, math 
def theme(): 
    turtle.speed(10) 

    column_1() 
    reset_column() 
    column_2() 
    reset_column() 
    column_1() 

def reset_column(): 
    turtle.up() 
    turtle.forward(160) 
    turtle.left(90) 
    turtle.forward(480) 
    turtle.right(90) 
    turtle.down() 

def reset_box(): 
    turtle.up() 
    turtle.forward(3 * 32) 
    turtle.right(90) 
    turtle.forward(2 * 32) 
    turtle.right(180) 
    turtle.down() 

def column_2(): 
    empty() 
    reset_box() 
    filled() 
    reset_box() 
    empty() 
    reset_box() 

def column_1(): 
    filled() 
    reset_box() 
    empty() 
    reset_box() 
    filled() 
    reset_box() 

def filled(): 
    size = 480 
    box = size/3 
    for index in range(4): 
     turtle.forward(box) 
     turtle.right(90) 
    turtle.forward(32) 
    turtle.right(135) 
    turtle.forward(32 * math.sqrt(2)) 
    for index in range(4): 
     for index in range(3): 
      turtle.left(90) 
      turtle.forward((32 * math.sqrt(2))/2) 
      turtle.right(90) 
      turtle.forward((32 * math.sqrt(2))/2) 
     turtle.left(90) 
     turtle.forward(32 * math.sqrt(2)) 
    turtle.left(135) 
    turtle.up() 
    turtle.forward(32) 
    turtle.down() 
    for index in range(4): 
     turtle.forward(box - (2 * (box/5))) 
     turtle.right(90) 
    turtle.forward(32) 

    first = True 
    for index in range(4): 
     if (first): 
      turtle.right(135) 
      first = False 
     else: 
      turtle.left(90) 
     turtle.forward(32 * math.sqrt(2)) 
     turtle.left(90) 
     turtle.forward((32 * math.sqrt(2))/2) 
     turtle.right(90) 
     turtle.forward((32 * math.sqrt(2))/2) 

    turtle.left(135) 
    turtle.up() 
    turtle.forward(32) 
    turtle.down() 
    for index in range(4): 
     turtle.forward(box - (4 * (box/5))) 
     turtle.left(90) 
def empty(): 
    size = 480 
    box = size/3 
    for index in range(4): 
     turtle.forward(box) 
     turtle.right(90) 
    turtle.forward(32) 
    turtle.right(90) 
    turtle.up() 
    turtle.forward(32) 
    turtle.down() 
    for index in range(4): 
     turtle.forward(box - (2 * (box/5))) 
     turtle.left(90) 
    turtle.left(90) 
    turtle.forward(32) 

    first = True 
    for index in range(4): 
     if (first): 
      turtle.right(135) 
      first = False 
     else: 
      turtle.left(90) 
     turtle.forward(32 * math.sqrt(2)) 
     turtle.left(90) 
     turtle.forward((32 * math.sqrt(2))/2) 
     turtle.right(90) 
     turtle.forward((32 * math.sqrt(2))/2) 

    turtle.left(135) 
    turtle.up() 
    turtle.forward(32) 
    turtle.down() 
    for index in range(4): 
     turtle.forward(box - (4 * (box/5))) 
     turtle.left(90) 

theme() 

我的結果是: enter image description here 我想有: enter image description here

有什麼建議?真的不知道如何開始這樣的事情。這是我第一次使用Python,尤其是Turtle機制。

+1

的可能的複製[我如何填寫烏龜這些廣場 - 的Python(https://stackoverflow.com/questions/12453572/how-can-i-fill-these-squares-in-龜 - 蟒蛇) – Personman

回答

1

使用turtle.begin_fill()turtle.end_fill(),並且還可以turtle.fillcolor

下面是代碼來實現它與demo一起。我對所有代碼行都有評論。

import turtle, math 
def theme(): 
    turtle.speed(10) 

    column_1() 
    reset_column() 
    column_2() 
    reset_column() 
    column_1() 

def reset_column(): 
    turtle.up() 
    turtle.forward(160) 
    turtle.left(90) 
    turtle.forward(480) 
    turtle.right(90) 
    turtle.down() 

def reset_box(): 
    turtle.up() 
    turtle.forward(3 * 32) 
    turtle.right(90) 
    turtle.forward(2 * 32) 
    turtle.right(180) 
    turtle.down() 

def column_2(): 
    empty() 
    reset_box() 
    filled() 
    reset_box() 
    empty() 
    reset_box() 

def column_1(): 
    filled() 
    reset_box() 
    empty() 
    reset_box() 
    filled() 
    reset_box() 

def filled(): 

    turtle.fillcolor('black') # NEW CODE 
    turtle.begin_fill()  # NEW CODE 

    size = 480 
    box = size/3 
    for index in range(4): 
     turtle.forward(box) 
     turtle.right(90) 
    turtle.forward(32) 
    turtle.right(135) 
    turtle.forward(32 * math.sqrt(2)) 
    for index in range(4): 
     for index in range(3): 
      turtle.left(90) 
      turtle.forward((32 * math.sqrt(2))/2) 
      turtle.right(90) 
      turtle.forward((32 * math.sqrt(2))/2) 
     turtle.left(90) 
     turtle.forward(32 * math.sqrt(2)) 
    turtle.left(135) 
    turtle.up() 
    turtle.forward(32) 
    turtle.down() 
    for index in range(4): 
     turtle.forward(box - (2 * (box/5))) 
     turtle.right(90) 
    turtle.forward(32) 

    first = True 
    for index in range(4): 
     if (first): 
      turtle.right(135) 
      first = False 
     else: 
      turtle.left(90) 
     turtle.forward(32 * math.sqrt(2)) 
     turtle.left(90) 
     turtle.forward((32 * math.sqrt(2))/2) 
     turtle.right(90) 
     turtle.forward((32 * math.sqrt(2))/2) 

    turtle.left(135) 
    turtle.up() 
    turtle.forward(32) 
    turtle.down() 
    for index in range(4): 
     turtle.forward(box - (4 * (box/5))) 
     turtle.left(90) 

    turtle.end_fill()  # NEW CODE 

def empty(): 

    size = 480 
    box = size/3 
    for index in range(4): 
     turtle.forward(box) 
     turtle.right(90) 
    turtle.forward(32) 
    turtle.right(90) 
    turtle.up() 
    turtle.forward(32) 
    turtle.down() 

    turtle.fillcolor('black') # NEW CODE 
    turtle.begin_fill()  # NEW CODE 

    for index in range(4): 
     turtle.forward(box - (2 * (box/5))) 
     turtle.left(90) 
    turtle.left(90) 
    turtle.forward(32) 

    turtle.end_fill()   # NEW CODE 

    turtle.fillcolor('white') # NEW CODE 
    turtle.begin_fill()  # NEW CODE 

    first = True 
    for index in range(4): 

     if (first): 
      turtle.right(135) 
      first = False 
     else: 
      turtle.left(90) 

     turtle.forward(32 * math.sqrt(2)) 
     turtle.left(90) 
     turtle.forward((32 * math.sqrt(2))/2) 
     turtle.right(90) 
     turtle.forward((32 * math.sqrt(2))/2) 

    turtle.end_fill()  # NEW CODE 

    turtle.left(135) 
    turtle.up() 
    turtle.forward(32) 
    turtle.down() 

    turtle.fillcolor('black') # NEW CODE 
    turtle.begin_fill()  # NEW CODE 

    for index in range(4): 
     turtle.forward(box - (4 * (box/5))) 
     turtle.left(90) 

    turtle.end_fill()  # NEW CODE 

theme() 
+0

非常感謝,但有一件事 - https://gyazo.com/9864ad67725e8c383eb09152fe7a95b2,就像......當筆是「上」時它也在填充? – SocketByte

+0

好吧我修好了,你只是忘了在up/down方法之間添加end/begin;)謝謝! – SocketByte

1

我相信這又是另一種情況衝壓工作比繪製。我由這是一組五個郵票,其中一人具有可變填充顏色:

from turtle import Turtle, Screen 

SIZE = 480 
BOX = SIZE // 3 
STAMP_SIZE = 20 
STEP = 32 * 2 ** 0.5 

screen = Screen() 
stamps = [] 

stamp = Turtle('square', visible=False) 
stamp.shapesize(BOX/STAMP_SIZE) 
stamp.color('black', 'white') 
stamps.append(stamp) 

stamp2 = Turtle() 
stamp2.goto(STEP/2, -BOX/2 - STEP/4) 
stamp2.begin_poly() 
for _ in range(4): 
    for _ in range(3): 
     stamp2.left(90) 
     stamp2.forward(STEP/2) 
     stamp2.right(90) 
     stamp2.forward(STEP/2) 
    stamp2.left(90) 
    stamp2.forward(STEP) 
stamp2.end_poly() 
polygon = stamp2.get_poly() 
screen.addshape('stamp2', polygon) 

stamp2.reset() 
stamp2.shape('stamp2') 
stamp2.color('white') 
stamp2.tilt(45) 
stamps.append(stamp2) 

stamp = Turtle('square', visible=False) 
stamp.shapesize(3 * BOX/5/STAMP_SIZE) 
stamp.color('black') 
stamps.append(stamp) 

stamp4 = Turtle() 
stamp4.setx(BOX/5) 
stamp4.right(45) 
stamp4.begin_poly() 
for index in range(4): 
    stamp4.forward(STEP/2) 
    stamp4.right(90) 
    stamp4.forward(STEP) 
    stamp4.right(90) 
    stamp4.forward(STEP/2) 
    stamp4.left(90) 
stamp4.end_poly() 
polygon = stamp4.get_poly() 
screen.addshape('stamp4', polygon) 

stamp4.reset() 
stamp4.shape('stamp4') 
stamp4.color('white') 
stamps.append(stamp4) 

stamp = Turtle('square', visible=False) 
stamp.shapesize(BOX/5/STAMP_SIZE) 
stamp.color('black') 
stamps.append(stamp) 

for stamp in stamps: 
    stamp.speed('fastest') 
    stamp.penup() 

parity = True 

for x in range(-BOX, BOX + 1, BOX): 
    for y in range(-BOX, BOX + 1, BOX): 

     stamps[0].fillcolor(['white', 'black'][parity]) 

     for stamp in stamps: 
      stamp.goto(x, y) 
      stamp.stamp() 

     parity = not parity 

for stamp in stamps: 
    stamp.hideturtle() 

screen.exitonclick() 

你會發現這使得代碼更簡單,更快速。

enter image description here