簡體   English   中英

海龜圖形,畫一顆星星?

[英]Turtle graphics, draw a star?

我想畫一個實心的星星,比如:

http://www.seaviewstickers.co.uk/shop/media/catalog/product/cache/1/thumbnail/600x600/9df78eab33525d08d6e5fb8d27136e95/b/l/black_11.jpg

到目前為止我有這個代碼:

def draw_star(size,color):
    count = 0
    angle = 144
    while count <= 5:
        turtle.forward(size)
        turtle.right(angle)
        count += 1
    return

draw_star(100,"purple")

我想用函數傳遞的任何顏色填充星星。 我怎樣才能做到這一點?

要獲得 5 角星,您應該為每邊畫 2 條線。 角度需要加到 72 (360/5)

import turtle
def draw_star(size, color):
    angle = 120
    turtle.fillcolor(color)
    turtle.begin_fill()

    for side in range(5):
        turtle.forward(size)
        turtle.right(angle)
        turtle.forward(size)
        turtle.right(72 - angle)
    turtle.end_fill()
    return

draw_star(100, "purple")

嘗試不同的angle值以獲得您想要的形狀

海龜文檔中搜索“fill”:

def draw_star(size,color):
    count = 0
    angle = 144
    turtle.fillcolor(color)
    turtle.begin_fill()
    for _ in range(5):
        turtle.forward(size)
        turtle.right(angle)
    turtle.end_fill()

draw_star(100,"purple")

注意return多余的,通過像這樣編碼循環,它不會繪制兩次輪廓。

def draw_star(size, color):
...:     turtle.reset()
...:     turtle.color(color)
...:     turtle.fillcolor(color)
...:     turtle.begin_fill()
...:     turtle.lt(260)
...:     for _ in range(5):
...:         turtle.fd(size)
...:         turtle.lt(170)
...:         turtle.fd(size)
...:         turtle.rt(100)
...:     turtle.end_fill()
...:     
...:draw_star(size, color)

嘗試使用turtle.fill 但是,如果您只是在代碼中使用它而不進行進一步更改,您將獲得“交替”填充:

交替填充

你需要調整你的程序來繪制沒有相交線的星星的輪廓(可以通過在兩個角度之間交替來完成),或者單獨填充星星的內部(通過跟蹤內接多邊形)。

def create_star (pointer, color_mix, central_point_x_value,\
                    central_point_y_value, length, height):
    travel_distance = length * .223
    pointer.home() # resets the orientation of the pointer
    #   without reseting everything
    pointer.penup()

    pointer.goto (central_point_x_value,central_point_y_value)
    pointer.left(90)
    pointer.forward(height) #move to the top of the star
    pointer.left(18) # must straighten the direction to match the iteration needs
    #draw Star , going counterclockwise
    # starting at the top and ending at the top of each outside triangle
    pointer.pendown()
    pointer.fillcolor (color_mix)
    pointer.begin_fill()
    count = 5
    while count != 0:
        #draw the star
        pointer.left(144)
        pointer.forward(travel_distance)
        pointer.right(72)
        pointer.forward(travel_distance)

        count -=1
    pointer.end_fill()

如果你在 Windows 上,你可能會逃脫:

turtle.color("purple")
turtle.begin_fill()
turtle.circle(100, extent=720, steps=5)
turtle.end_fill()

但是,這段代碼有兩個問題:它與您的插圖風格不同 它在所有 Python 龜/tkinter 實現上的工作方式不同(有些只顯示部分填充):

在此處輸入圖片說明

這是使用沖壓而不是繪圖的替代實現,可以解決這兩個問題:

STAR_SIZE = 100

EXPANSION = 1.2
TRANSLATION = STAR_SIZE * EXPANSION / 4

turtle.hideturtle()
turtle.color("purple")
turtle.shape("triangle")
turtle.turtlesize(STAR_SIZE * EXPANSION / 20)

for _ in range(5):
    turtle.right(72)
    turtle.forward(TRANSLATION)
    turtle.stamp()
    turtle.backward(TRANSLATION)

在此處輸入圖片說明

我做得很快,這很容易更正。 隨意評論更好的解決方案:)

import turtle 

x = turtle.Turtle()
x.speed(0)

 def draw_star(length, angle):

 for i in range(5): #the star is made out of 5 sides
  x.forward(length)
  x.right(angle)


for i in range(1):
 x.color("purple") #if  you want outline of the star choose .color("purple" + "outline color")
 x.begin_fill()
 draw_star(100, 144) #144 is the perfect angle for a star
 x.end_fill()

也許試試這個:

turtle.write("★", font=("Arial", 40, "normal"))

這只是寫給烏龜,所以這可能無濟於事,但您可以嘗試一下。

from turtle import *
hideturtle()

def draw_star(sidesize, points, alfacorner, color):
    betacorner = 360/points+alfacorner
    fillcolor(color)
    begin_fill()
    for x in range(points*2):
        forward(sidesize)
        if x % 2 == 0:
            left(180-alfacorner)
        else:
            right(180-betacorner)
    end_fill()


draw_star(70, 8, 90, 'grey')
draw_star(90, 5, 72, 'yellow')
draw_star(120, 5, 36, 'red')
draw_star(65, 6, 60, 'darkblue')
draw_star(80, 4, 45, 'darkviolet')
draw_star(80, 3, 30, 'greenyellow')

exitonclick()

這應該有效,請隨時提出任何問題!

def draw_star(size,color):
    count = 0
    angle = 144
    turtle.color(color)
    turtle.begin_fill()
    while count <= 5:
        turtle.forward(size)
        turtle.right(angle)
        count += 1
    turtle.end_fill()
    return

draw_star(100,"purple")
        ...:def draw_star(size):
        ...:     turtle.reset()
        ...:     turtle.color("silver")
        ...:     turtle.fillcolor("yellow")
        ...:     turtle.begin_fill()
        ...:     turtle.lt(260)
        ...:     for _ in range(5):
        ...:         turtle.fd(size)
        ...:         turtle.lt(170)
        ...:         turtle.fd(size)
        ...:         turtle.rt(100)
        ...:     turtle.end_fill()
        ...:

draw_star(在此處輸入尺寸)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM