Python 海龟图形,画一颗星星?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26356543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:23:33  来源:igfitidea点击:

Turtle graphics, draw a star?

pythonturtle-graphics

提问by ASm

I want to draw a filled-in star, such as:

我想画一个实心的星星,比如:

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

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

I have this code so far:

到目前为止我有这个代码:

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")

I want to fill in the star with whatever color the function is passed. How can I do this?

我想用函数传递的任何颜色填充星星。我怎样才能做到这一点?

回答by Konrad Rudolph

Experiment with turtle.fill. However, if you just use that in your code without further change, you'll get an “alternating” fill:

试验一下turtle.fill。但是,如果您只是在代码中使用它而不进行进一步更改,您将获得“交替”填充:

alternating fill

alternating fill

You'll need to adapt your routine to draw the star's outline without intersecting lines (can be done by alternating between two angles), or fill the inside of the star separately (by tracing the inscribed polygon).

您需要调整您的例程以绘制没有相交线的星形轮廓(可以通过在两个角度之间交替来完成),或单独填充星形内部(通过跟踪内接多边形)。

回答by Eric

Search for "fill" in the turtle documentation:

海龟文档中搜索“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")

N.B. The returnwasn superfluous, and by coding the loop like this it won't draw the outline twice.

注意这是return多余的,通过像这样编码循环,它不会绘制两次轮廓。

回答by John La Rooy

To get a 5 pointed star, you should draw 2 lines for each side. The angles need to add to 72 (360/5)

要获得 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")

Experiment with different values of angleto get the shape you want

尝试不同的值angle以获得您想要的形状

回答by Ruston YoungTueros

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()

回答by cdlane

If you're on Windows, you can probably get away with:

如果你在 Windows 上,你可能会逃脱:

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

However, this code has two problems: it isn't the same stylestar as your illustration; it doesn't work the same on all Python turtle/tkinter implementations (some only show partial fill):

但是,这段代码有两个问题:它与您的插图风格不同;它在所有 Python 龟/tkinter 实现上的工作方式不同(有些只显示部分填充):

enter image description here

enter image description here

Here's an alternate implementation using stampinginstead of drawingthat should fix both issues:

这是使用冲压而不是绘图的替代实现,可以解决这两个问题:

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)

enter image description here

enter image description here

回答by jj.ons

I made this quick, this can easily be more corrected. Feel free to comment a better solution :)

我做得很快,这很容易更正。随意评论更好的解决方案:)

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()

回答by Alex

        ...: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(put a size here)

draw_star(在此处输入尺寸)

回答by Alex

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)

回答by rahsut

Maybe try this:

也许试试这个:

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

This is just writing to turtle though so this may not help but you can try it.

这只是写给乌龟,所以这可能无济于事,但您可以尝试一下。

回答by Bat Andrea

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()