用python的turtle库画一个中国国旗
以下是使用Python的turtle库绘制中国国旗的代码:
import turtle
def draw_rectangle(x, y, width, height):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
def draw_star(x, y, radius):
turtle.penup()
turtle.goto(x, y)
turtle.setheading(162)
turtle.forward(radius)
turtle.setheading(0)
turtle.pendown()
turtle.begin_fill()
while True:
turtle.forward(2 * radius * 0.382)
turtle.right(144)
if abs(turtle.pos() - (x, y)) < 1:
break
turtle.end_fill()
def draw_flag():
turtle.speed(3)
turtle.penup()
turtle.goto(-250, 150)
turtle.pendown()
turtle.color("red")
turtle.begin_fill()
turtle.goto(-250, -150)
turtle.goto(250, -150)
turtle.goto(250, 150)
turtle.goto(-250, 150)
turtle.end_fill()
turtle.color("yellow")
positions = [(-180, 60), (-120, 0), (-180, -60)]
for position in positions:
draw_star(*position, 30)
turtle.hideturtle()
if __name__ == "__main__":
draw_flag()
turtle.done()
运行以上代码,会绘制一个中国国旗。其中,draw_rectangle函数用于绘制矩形,draw_star函数用于绘制五角星,draw_flag函数用于绘制完整的国旗
原文地址: https://www.cveoy.top/t/topic/inkW 著作权归作者所有。请勿转载和采集!