用Python代码画党旗
绘制党旗(中国国旗)通常涉及红色背景和黄色星星的排列。下面是使用Python绘制中国国旗的示例代码,使用了turtle
模块进行绘图。在绘制前,请确保你的Python环境已安装turtle
模块。
pythonimport turtle
def draw_rectangle(width, height, color):
turtle.begin_fill()
turtle.color(color)
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
def draw_star(x, y, radius, color):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
turtle.color(color)
for _ in range(5):
turtle.forward(radius)
turtle.right(144)
turtle.forward(radius)
turtle.left(72)
turtle.end_fill()
def draw_chinese_flag():
turtle.speed(5)
turtle.penup()
turtle.goto(-300, 150)
turtle.pendown()
# Draw red background
draw_rectangle(600, 400, "#DE2910")
# Draw large yellow star
draw_star(-80, 80, 100, "#FFDE00")
# Draw four smaller yellow stars
positions = [(-25, 50), (-25, 15), (0, 30), (-15, 0)]
for pos in positions:
draw_star(pos[0], pos[1], 30, "#FFDE00")
turtle.hideturtle()
turtle.done()
# Call the function to draw the Chinese flag
draw_chinese_flag()
总结:
以上代码使用Python的turtle
模块绘制了中国国旗。通过定义函数绘制矩形和五角星,实现了红色背景和黄色星星的排列。使用turtle
模块可以轻松绘制简单的图形,适合教学和初学者练习。
关键字:
Python, turtle模块, 绘图, 中国国旗, 红色背景, 黄色星星。