赞
踩
下面是小编写的绘画五星红旗
- from turtle import *
- penup()
- goto(-300,200)
- pendown()
- fillcolor("red")
- begin_fill()
- for i in range(4):
- speed(250)
- forward(600)
- right(90)
- forward(400)
- right(90)
- end_fill()
- penup()
- goto(-265,110)
- pendown()
- fillcolor("yellow")
- begin_fill()
- for i in range(5):
- speed(250)
- forward(30)
- left(72)
- forward(30)
- right(144)
- end_fill()
- x = [-210,-160,-160,-210]
- y = [180,140,75,25]
- for h in range(4):
- penup()
- goto(x[h],y[h])
- fillcolor("yellow")
- begin_fill()
- pendown()
- for i in range(5):
- speed(250)
- forward(10)
- left(72)
- forward(10)
- right(144)
-
- end_fill()
- hideturtle()
- done()
-

这段代码是用Python的turtle模块来绘制图形。turtle模块是Python的标准库之一,用于进行基础的图形绘制。
from turtle import *
:从turtle模块导入所有函数和类。
penup()
:提起画笔,移动时不会绘制任何内容。
goto(-300,200)
:移动画笔到指定的坐标位置(-300, 200)。
pendown()
:放下画笔,开始绘制。
fillcolor("red")
:设置填充颜色为红色。
begin_fill()
:开始填充颜色。
for i in range(4):
:循环4次。
speed(250)
:设置画笔速度为250(最快)。forward(600)
:向前移动600个单位。right(90)
:向右转90度。forward(400)
:向前移动400个单位。right(90)
:再次向右转90度。这创建了一个L形状,重复4次,从而绘制了一个大的正方形。end_fill()
:结束填充颜色,这将使内部区域填充为红色。
penup()
:再次提起画笔。
goto(-265,110)
:移动画笔到新的位置。
重复上述过程,但这次填充颜色为黄色,并且绘制的是一个五角星。
x = [-210,-160,-160,-210]
和 y = [180,140,75,25]
:定义四个点的坐标。
另一个循环开始,循环4次。对于每个点:
penup()
:提起画笔。goto(x[h],y[h])
:移动到指定的坐标。hideturtle()
:隐藏画笔。
done()
:完成绘制。
总的来说,这段代码首先绘制了一个红色的正方形,然后在其旁边绘制了一个黄色的五角星。接着,在四个不同的位置绘制了四个小的黄色五角星。
小编推荐Pythonit教程网:blog.pythonit.cn
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。