python turtle 海龟绘图 国旗
时间: 2023-10-19 22:07:10 浏览: 176
好的,我知道了。关于 Python turtle 绘制国旗,你可以先使用 `turtle` 库绘制一个矩形,然后在矩形中绘制五颗五角星。具体实现可以参考以下代码:
```python
import turtle
# 绘制矩形
def draw_rectangle(x, y, width, height):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color('red')
turtle.begin_fill()
for i 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.setpos(x, y)
pt1 = turtle.pos()
turtle.circle(-radius, 72)
pt2 = turtle.pos()
turtle.circle(-radius, 72)
pt3 = turtle.pos()
turtle.circle(-radius, 72)
pt4 = turtle.pos()
turtle.circle(-radius, 72)
pt5 = turtle.pos()
turtle.color('yellow', 'yellow')
turtle.begin_fill()
turtle.goto(pt3)
turtle.goto(pt1)
turtle.goto(pt4)
turtle.goto(pt2)
turtle.goto(pt5)
turtle.end_fill()
# 绘制五星红旗
def draw_china_flag(x, y):
turtle.speed(1)
# 绘制大矩形
draw_rectangle(x, y, 640, 400)
# 绘制小矩形
draw_rectangle(x, y, 320, 200)
# 绘制大星星
draw_star(x + 160, y - 100, 80)
# 绘制小星星1
draw_star(x + 60, y - 60, 30)
# 绘制小星星2
draw_star(x + 100, y - 20, 30)
# 绘制小星星3
draw_star(x + 100, y - 130, 30)
# 绘制小星星4
draw_star(x + 140, y - 80, 30)
# 测试绘制五星红旗
draw_china_flag(0, 0)
turtle.done()
```
阅读全文