Python绘制多彩科赫雪花
时间: 2023-11-05 18:23:25 浏览: 184
可以使用turtle模块来绘制多彩科赫雪花。以下是一个简单的Python代码示例:
import turtle
# 颜色列表
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
def snowflake(lengthSide, levels):
"""绘制科赫雪花"""
if levels == 0:
turtle.forward(lengthSide)
return
lengthSide /= 3.0
snowflake(lengthSide, levels - 1)
turtle.left(60)
snowflake(lengthSide, levels - 1)
turtle.right(120)
snowflake(lengthSide, levels - 1)
turtle.left(60)
snowflake(lengthSide, levels - 1)
def main():
"""主函数"""
turtle.setup(width=800, height=800)
turtle.penup()
turtle.goto(-200, 100)
turtle.pendown()
turtle.bgcolor("black")
side = 400.0
level = 4
turtle.speed(100)
for i in range(6):
turtle.penup()
turtle.goto(-150 + i * 50, 0)
turtle.pendown()
turtle.pencolor(colors[i])
snowflake(side, level)
turtle.hideturtle()
turtle.done()
if __name__ == '__main__':
main()
希望这个代码示例能够帮助你绘制多彩科赫雪花!
阅读全文