本文共 1906 字,大约阅读时间需要 6 分钟。
由于已有C语言基础,故不再重申基础知识,直接上代码!
import turtleturtle.up()turtle.goto(-200, 200)turtle.down()turtle.begin_fill()turtle.fillcolor("red")turtle.pencolor("red")turtle.color('red', 'red')for i in range(2): turtle.forward(300) turtle.right(90) turtle.forward(200) turtle.right(90)turtle.end_fill()
turtle.forward(distance)
:向当前画笔方向移动distance像素长度。turtle.begin_fill()
:准备开始填充图形。turtle.fillcolor("red")
:设置填充颜色为红色。turtle.end_fill()
:填充完成,生成填充色的图形。在国旗背景的基础上,依次绘制五个黄色五角星。
turtle.up()turtle.goto(-170, 145)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")for x in range(5): turtle.forward(45) turtle.right(144)turtle.end_fill()
turtle.up()turtle.goto(-115, 175)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")turtle.right(30)for x in range(5): turtle.forward(18) turtle.right(144)turtle.end_fill()
turtle.up()turtle.goto(-90, 157)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")turtle.right(30)for x in range(5): turtle.forward(18) turtle.right(144)turtle.end_fill()
turtle.up()turtle.goto(-87, 130)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")turtle.right(30)for x in range(5): turtle.forward(18) turtle.right(144)turtle.end_fill()
turtle.up()turtle.goto(-106, 106)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")turtle.right(30)for x in range(5): turtle.forward(18) turtle.right(144)turtle.end_fill()turtle.hideturtle()
turtle.hideturtle()
:隐藏画笔的turtle形状。turtle.showturtle()
:显示画笔的turtle形状。请参考以下图片描述链接查看完整效果图:
[图片描述链接,存于mailto:image图片由于无法嵌入,建议参考源图片]
以上代码基于turtle库编写,适合用于生成图形。通过循环调用turtle的_basic方法,实现了国旗背景的绘制以及五角星的放置。每个五角星的位置和大小均经过精心调试,以确保最终效果符合预期。
转载地址:http://tbeoz.baihongyu.com/