python 叠加等边三角形的绘制

xiaoxiao2021-02-28  23

python语言程序设计基础 习题2.5

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()#which returns after the main loop exits不知道啥意思,反正可以让窗口停住(欢迎评论告诉我啊)
转载请注明原文地址: https://www.6miu.com/read-2650246.html

最新回复(0)