极客小将

您现在的位置是:首页 » python编程资讯

资讯内容

如何用python画叠加三角形?

极客小将2020-12-14-
简介python中可以使用下面的程序实现画叠加三角形:import turtledef drawTriangle(num,len,flag):#flag用来调整画三角形的方向    flag*=-1    

XA0少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

python中可以使用下面的程序实现画叠加三角形:
XA0少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

import turtle def drawTriangle(num,len,flag):#flag用来调整画三角形的方向     flag*=-1     len/=2     if(num==1):         if(flag==1):             turtle.left(60)             turtle.fd(len)             turtle.right(120)             turtle.fd(len)             turtle.right(120)             turtle.fd(len)             turtle.left(120)         else:             turtle.right(60)             turtle.fd(len)             turtle.left(120)             turtle.fd(len)             turtle.left(120)             turtle.fd(len)             turtle.right(120)     else:         if(flag==1):             turtle.left(60)             turtle.fd(len/2)             num=num-1             turtle.right(60)             drawTriangle(num,len,flag)             turtle.fd(len/2)             turtle.right(120)             turtle.fd(len)             turtle.right(120)             turtle.fd(len)             turtle.left(120)#注意递归过程中画笔返回的角度         else:             turtle.right(60)             turtle.fd(len/2)             num=num-1             turtle.left(60)             drawTriangle(num,len,flag)             turtle.fd(len/2)             turtle.left(120)             turtle.fd(len)             turtle.left(120)             turtle.fd(len)             turtle.right(120)#注意递归过程中画笔返回的角度          def main():     turtle.setup(600,600, 200, 200)     turtle.pendown()     turtle.speed(2)     turtle.pensize(5)     drawTriangle(5,512,-1)     turtle.hideturtle()#隐藏画笔图标 main() turtle.done()

更多Python知识请关注Python自学网。XA0少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

网友点评

共有5条评论来说两句吧...

在线客服