简体   繁体   中英

Turtle graphics, draw a star?

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

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?

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

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

NB The return wasn superfluous, and by coding the loop like this it won't draw the outline twice.

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)

Experiment withturtle.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).

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

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)

在此处输入图片说明

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

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.

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

This should work, feel free to ask any questions!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM