怎么用编程画奥运五环

时间:2025-01-25 05:15:47 游戏攻略

```python

import turtle

设置画布大小和背景颜色

screen = turtle.Screen()

screen.setup(width=800, height=600)

screen.title('奥运五环')

初始化画笔

t = turtle.Turtle()

t.speed(10)

t.pensize(6)

定义画圆的函数

def draw_circle(x, y, color):

t.penup()

t.goto(x, y)

t.pendown()

t.color(color)

t.circle(50)

绘制五个圆环

colors = ["blue", "black", "red", "yellow", "green"]

positions = [(-220, 0), (0, 0), (220, 0), (-110, -100), (110, -100)]

for color, x, y in zip(colors, positions):

draw_circle(x, y, color)

隐藏海龟

t.hideturtle()

关闭画布

turtle.done()

```

代码解释:

导入turtle模块:

这是Python内置的绘图模块,不需要额外安装。

设置画布:

创建一个画布并设置其大小和标题。

初始化画笔:

设置画笔的速度和线条粗细。

定义画圆的函数:

`draw_circle`函数用于在指定位置绘制指定颜色的圆。

绘制五个圆环:

使用循环和颜色列表,按照奥运五环的标准位置和颜色绘制五个圆环。

隐藏海龟:

绘制完成后隐藏画笔。

关闭画布:

完成绘制后关闭画布。

运行上述代码,你将在屏幕上看到一个绘制了奥运五环图案的图形。