上一篇: Python 海龟绘图编程 画奥运五环
下一篇: Python 海龟绘图编程 利用circle函数画多边形

Python 海龟绘图编程 画五星红旗

今天练习Python海龟图编程:画一个五星红旗。

画五星红旗分三步:画红旗,画一个五角星,复制4个五角星。

画红旗的代码:
import turtle
turtle.pensize(4)
##画红旗
turtle.up()
turtle.goto(-400,300)
turtle.down()
turtle.color("red")
turtle.fillcolor("red")
turtle.begin_fill()
turtle.forward(800)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(800)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.end_fill()

海龟图画五角星,模拟用笔画五角星的方法。
画五角星的代码:
import turtle
turtle.pensize(4)
##画五角星
turtle.up()
turtle.goto(-350,250)
turtle.down()
turtle.color("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.forward(25)
turtle.right(72)
turtle.forward(25)
turtle.right(72)
turtle.forward(25)
turtle.right(72)
turtle.forward(25)
turtle.right(72)
turtle.forward(64)
turtle.right(144)
turtle.forward(103)
turtle.right(144)
turtle.forward(103)
turtle.right(144)
turtle.forward(103)
turtle.right(144)
turtle.forward(103)
turtle.right(144)
turtle.forward(103)
turtle.end_fill()
turtle.up()

Python画五星红旗的全部代码:
import turtle
turtle.pensize(4)
##画红旗
turtle.up()
turtle.goto(-400,300)
turtle.down()
turtle.color("red")
turtle.fillcolor("red")
turtle.begin_fill()
turtle.forward(800)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(800)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.end_fill()

##画一个大五角星
turtle.up()
turtle.goto(-350,250)
turtle.down()
turtle.color("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.forward(25)
turtle.right(72)
turtle.forward(25)
turtle.right(72)
turtle.forward(25)
turtle.right(72)
turtle.forward(25)
turtle.right(72)
turtle.forward(64)
turtle.right(144)
turtle.forward(103)
turtle.right(144)
turtle.forward(103)
turtle.right(144)
turtle.forward(103)
turtle.right(144)
turtle.forward(103)
turtle.right(144)
turtle.forward(103)
turtle.end_fill()
turtle.up()

##画第1个小五角星
##设置两个变量,分别表示缩小的倍数和画笔旋转的角度
a = 2
b = 0

turtle.right(b)
turtle.goto(-200,250)
turtle.down()
turtle.color("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(64/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(39)
turtle.end_fill()
turtle.up()


##画第2个小五角星
a = 2
b = 46

turtle.right(b)
turtle.goto(-200,150)
turtle.down()
turtle.color("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(64/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(39)
turtle.end_fill()
turtle.up()

##画第3个小五角星
a = 2
b = 56

turtle.right(b)
turtle.goto(-250,70)
turtle.down()
turtle.color("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(64/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(39)
turtle.end_fill()
turtle.up()

##画第4个小五角星
a = 2
b = 66

turtle.right(b)
turtle.goto(-350,50)
turtle.down()
turtle.color("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(25/a)
turtle.right(72)
turtle.forward(64/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(144)
turtle.forward(103/a)
turtle.right(39)
turtle.end_fill()


运行效果:


 
Copyright@2023-2004 少年编程竞赛网版权所有
转载本站任何内容请注明出处
苏ICP备2023040873号-1
网站地图