使用Python turtle库画一个五角星,要求(1)在坐标(50,200)位置开始处绘画(2)画布颜色设置为red,画笔和填充颜色为yellow。注:五角星转角角度为144
3条回答 默认 最新
CSDN专家-黄老师 2021-12-06 09:28关注参考一下
import turtle import math def edge(x, y): turtle.penup() turtle.goto(x - 144, y + 96) # 起始位置 turtle.pendown() turtle.begin_fill() turtle.fillcolor('#F40002') turtle.pen(pencolor='#F40002') for i in range(2): turtle.fd(288) turtle.right(90) turtle.fd(192) turtle.right(90) turtle.end_fill() def big_star(x, y, len): # x,y为起始坐标,len为五角星边长 turtle.goto(x, y) # 起始位置 turtle.setheading(0) # 倾斜起始角度 turtle.begin_fill() turtle.fillcolor('#FAF408') turtle.penup() turtle.left(18) turtle.fd(len / 2 / math.cos(18 * math.pi / 180)) turtle.left(162) turtle.pendown() turtle.pen(pencolor='#FAF408') for i in range(5): turtle.fd(len) turtle.left(144) turtle.end_fill() turtle.penup() def small_star(x, y, len): # x,y为起始坐标,len为五角星边长 turtle.goto(x, y) # 起始位置 angle = turtle.towards(-10 * 9.8, 5 * 9.8) turtle.setheading(angle) # 倾斜起始角度 此时小五角星头部对准大五角星中心 turtle.fd(len / 2 / math.cos(18 * math.pi / 180)) turtle.right(162) turtle.pendown() turtle.begin_fill() turtle.fillcolor('#FAF408') for i in range(5): turtle.fd(len) turtle.right(144) turtle.end_fill() turtle.penup() if __name__ == '__main__': turtle.setup(width=468, height=312) # 左右两侧留出90像素距离 上下两侧留出60像素距离 turtle.hideturtle() edge(0, 0) big_star(-10 * 9.8, 5 * 9.8, 6 * 9.8 * math.cos(18 * math.pi / 180)) small_star(-5 * 9.8, 8 * 9.8, 2 * 9.8 * math.cos(18 * math.pi / 180)) small_star(-3 * 9.8, 6 * 9.8, 2 * 9.8 * math.cos(18 * math.pi / 180)) small_star(-3 * 9.8, 3 * 9.8, 2 * 9.8 * math.cos(18 * math.pi / 180)) small_star(-5 * 9.8, 1 * 9.8, 2 * 9.8 * math.cos(18 * math.pi / 180)) turtle.done()解决 无用评论 打赏 举报