使用Python turtle库画一个五角星,要求(1)在坐标(50,200)位置开始处绘画(2)画布颜色设置为red,画笔和填充颜色为yellow。注:五角星转角角度为144
参考一下
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()
import turtle as t
# 设置画布大小
t.setup(800, 800)
# 将画笔移动到绘画点
t.goto(50, 200)
# 设置背景颜色
# rgb 红绿蓝 最小值0 最大值1
t.bgcolor((1,0,0))
# 设置填充颜色
t.fillcolor((1,1,0))
# 设置画笔颜色
t.pencolor((1,1,0))
t.pensize(2)
# 开始填充点
t.begin_fill()
for i in range(5):
# 绘制五角星边长
t.forward(200)
# 转角144度
t.right(144)
#填充内置颜色
t.end_fill()
# 等待弹窗关闭
t.done()
import turtle as t
t.penup()
t.goto(50,200)
t.bgcolor("red")
t.fillcolor("yellow")
t.pencolor("yellow")
t.pendown()
t.begin_fill()
for i in range(5):
t.forward(200)
t.right(144)
t.end_fill()
t.hideturtle()
t.mainloop()