今天练习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()
运行效果: