我想画一个填充的星形,例如:
到目前为止,我的代码是这样的:
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")
我想用传递的函数颜色来填充星星。我该怎么做?
我想画一个填充的星形,例如:
到目前为止,我的代码是这样的:
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")
要画一个五角星,每边需要画两条线。每个角度需要加起来等于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")
尝试不同的角度
值,以获得所需的形状。
size
参数有疑问——它与什么相关?例如,我认为size
是一个圆的半径,星星应该适合其中。然而,在这个实现中,图像的大小随着angle
和size
的变化而改变,因此很难正确调整大小。 - cdlanedef 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
语句是必要的,通过像这样编写循环,它不会重复绘制轮廓。
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)
fillcolor()
调用与前面的.color()
调用重复了。请在您的答案中解释此解决方案为现有讨论添加了什么内容。 - cdlane我匆忙地完成了这个,它可以更加完善。欢迎提出更好的解决方案 :)
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()
尝试使用turtle.fill
进行实验。但是,如果您在代码中仅使用它而没有进一步更改,您将得到一个“交替”填充:
您需要调整例程以绘制星形的轮廓,避免交叉线(可以通过在两个角度之间交替进行绘制来实现),或单独填充星形内部(通过跟踪内接多边形来实现)。
也许可以尝试这个:
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()
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()
create_star(turtle.Turtle(), "purple", 0, 0, 100, 100)
,我期望在屏幕中央得到一个“填充”我的圆的星形图案,但实际上我得到的是一个大小只有我的圆的一半,且位于我的圆上方的星形图案。 - cdlane如果你在使用Windows系统,可能可以采用以下方法:
turtle.color("purple")
turtle.begin_fill()
turtle.circle(100, extent=720, steps=5)
turtle.end_fill()
这里有一个使用印章而不是绘图的替代实现,应该可以解决这两个问题:
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)
这应该可以工作,随时问问题!
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")