Turtle graphics, draw a star?

穿精又带淫゛_ 提交于 2020-01-30 06:16:19

问题


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

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?


回答1:


To get a 5 pointed star, you should draw 2 lines for each side. The angles need to add to 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 angle to get the shape you want




回答2:


Search for "fill" in the 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. The return wasn superfluous, and by coding the loop like this it won't draw the outline twice.




回答3:


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)



回答4:


Experiment with turtle.fill. However, if you just use that in your code without further change, you’ll get an “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).




回答5:


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



回答6:


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

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 style star as your illustration; it doesn't work the same on all Python turtle/tkinter implementations (some only show partial fill):

Here's an alternate implementation using stamping instead of drawing that 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)




回答7:


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



回答8:


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.




回答9:


        ...: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)



来源:https://stackoverflow.com/questions/26356543/turtle-graphics-draw-a-star

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!