我想画一个实心的星星,如:
到目前为止我有这个代码:
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
值以获得您想要的形状
如果您使用的是 Windows,您可能可以这样做:
turtle.color("purple")
turtle.begin_fill()
turtle.circle(100, extent=720, steps=5)
turtle.end_fill()
但是,这段代码有两个问题:它与您的插图的style星不同;它在所有 Pythonturtle/tkinter 实现上的工作方式不同(有些只显示部分填充):
这是使用 stamping 而不是 drawing 的替代实现,应该可以解决这两个问题:
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)
turtle.fill
进行实验。但是,如果您只是在代码中使用它而不进行进一步更改,您将得到“交替”填充:
您需要调整例程以绘制不相交线的星星轮廓(可以通过在两个角度之间交替来完成),或单独填充星星的内部(通过追踪内接多边形)。
在 turtle 文档中搜索“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
是多余的,通过像这样编码循环,它不会绘制两次轮廓。
也许试试这个:
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()
import turtle
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(100, "purple")
turtle.exitonclick()
解决此问题的一种方法是绘制一个实心三角形,然后绕圆旋转:
from turtle import Turtle
def triangle(n):
t.right(90)
t.forward(n / 3)
t.left(108)
t.forward(n)
t.left(144)
t.forward(n)
t.left(108)
t.forward(n / 3)
t.left(90)
def star(n):
t.left(18)
t.dot(n / 10)
for _ in range(5):
t.begin_fill()
triangle(n)
t.end_fill()
t.left(360 / 5)
t = Turtle()
#t.getscreen().tracer(0) # add if you want instantaneous drawing
star(100)
t.getscreen().exitonclick()
这避免了这个答案中提到的未定义的相交多边形。事实上,这种方法基本上是该答案的无邮票版本。
现在OP的明星有了“胖”的特质。您可以在
triangle()
功能中调整转动和移动以适应口味(角度需要加起来为 360):
def triangle(n):
t.right(90)
t.forward(n / 2)
t.left(120)
t.forward(n)
t.left(120)
t.forward(n)
t.left(120)
t.forward(n / 2)
t.left(90)
def star(n):
t.dot(n / 10)
t.left(18)
for _ in range(5):
t.begin_fill()
triangle(n)
t.end_fill()
t.left(360 / 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()
我做得很快,这可以很容易地更正。欢迎评论更好的解决方案:)
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()
这应该有效,请随时提出任何问题!
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(在此输入尺寸)