Python画五角星层叠图案和分形树——turtle包的使用
程序员文章站
2024-01-19 11:11:28
...
效果展示
代码展示
五角星代码
import turtle
def drawstar(size):
for i in range(5):
turtle.forward(size)
turtle.right(144)
def main():
# t=turtle.Pen()
turtle.penup()
turtle.right(90)
turtle.bk(200)
turtle.left(90)
turtle.pendown()
turtle.pensize(2)
turtle.pencolor('red')
size=50
while size<=100:
drawstar(size)
size+=10
turtle.exitonclick()
if __name__=='__main__':
main()
分形树代码
import turtle
def draw_branch(branch_length):
if branch_length>5:
turtle.forward(branch_length)
turtle.right(20)
print('右转 20')
draw_branch(branch_length-15)
turtle.left(40)
print('左转 40')
draw_branch(branch_length-15)
turtle.right(20)
print('右转 20')
turtle.backward(branch_length)
print('向后',branch_length)
def main():
turtle.left(90)
turtle.penup()
turtle.backward(150)
turtle.pendown()
turtle.color('brown')
draw_branch(40)
turtle.exitonclick()
if __name__=='__main__':
main()
下一篇: np.where()函数的详细使用介绍