龟图形,画一个明星?

时间:2014-10-14 08:56:12

标签: python turtle-graphics

我想绘制一个填充星,例如:

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

我想用函数传递的任何颜色填充星号。我怎么能这样做?

11 个答案:

答案 0 :(得分:3)

要获得一个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的不同值来获得您想要的形状

答案 1 :(得分:1)

turtle documentation

中搜索“填充”
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。 return并不是多余的,通过对此循环进行编码,它不会绘制两次轮廓。

答案 2 :(得分:1)

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)

答案 3 :(得分:0)

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

alternating fill

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

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

但是,此代码有两个问题:它与您的插图不同的 style 星形;它在所有Python turtle / tkinter实现上都不起作用(有些只显示部分填充):

enter image description here

以下是使用标记而非绘图的替代实现,它可以解决这两个问题:

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

答案 6 :(得分:0)

我这样做很快,可以很容易地纠正它。随时评论更好的解决方案:)

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

答案 7 :(得分:0)

也许尝试一下:

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

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

答案 8 :(得分:0)

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

答案 9 :(得分:0)

这应该可行,随时问任何问题!

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

答案 10 :(得分:-1)

        ...: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(在此处输入大小)