初出茅庐——利用Python的Turtle库绘制玫瑰花

用Python绘制一朵玫瑰花(附源代码)

要求:
①电脑需配备Python3.xx的环境
②需要的模块包 turtle(Python内置包)

Turtle()

个人接触的第一个Python库就是Turtle,业内Guido称我们这类人为:画龟入门。
(Turtle)绘制Rose()

成品图

初出茅庐——利用Python的Turtle库绘制玫瑰花_第1张图片
此图应该是全网拿Python绘制最好看玫瑰花,自信一点说,自己动手翻阅博客,目前没有找到阔以与之媲美的,当然好看是需要代价的,繁琐的循环,500+行的代码,计算起来,复杂程度不用我多说,所以个人推荐:Copy走自己玩一玩即可,尽量不要自己动手瞧,纯纯浪费时间,仅供娱乐。

代码展示

#套用的循环我已放入Shell命令行供大家参考改进。

'''
#      a+=
#        if i<:
#            left()
#        elif =:
#        pensize(a)
#        forward()
#        a-=
#        if 
#环境配备:Python自带的turtle库
#Created on Nov 10, 2019
#@author: Lkang


from turtle import *
#设置绘制起点
setup (700,700)
#画笔颜色
pencolor("red")
hideturtle()
penup()
goto(0,280)
pendown()
left(135)
speed(0)
#左侧弧形绘制
a=0.2
for i in range (80):
    if i < 50:
        pensize(a)
        forward(2)
        a+=0.15
        if i<10:
            left(0.7)
        elif 10(1)
        elif 24(3)
        elif 40(4)
    elif i >=50:
        pensize(a)
        forward(2.2)
        a-=0.25
        if 50(6.7)
        elif 65(2)
setheading(0)
penup()
goto(40,305)
pendown()
left(145)
#右侧弧形绘制
a=0.1
for i in range (100):
    if i < 60:
        pensize(a)
        forward(3)
        a+=0.15
        if i<10:
            left(0.8)
        elif 10(0.8)
        elif 24(1.7)
        elif 40(2.2)
    elif i >=60:
        pensize(a)
        forward(2.4)
        a-=0.15
        if 60(3.2)
        elif 85(4)
setheading(0)
penup()
goto(45,320)
pendown()
left(10)
a=0.1
for i in range (100):
    if i < 60:
        pensize(a)
        forward(3)
        a+=0.15
        if i<17:
            right(3.2)
        elif 17(3.5)
        elif 20(6.8)
        elif 40(0.6)
        elif 50(1)
    elif i >=60:
        pensize(a)
        forward(2.4)
        a-=0.16
        if 60(0.4)
        elif 85(1)
setheading(0)
penup()
goto(-135,265)
pendown()
left(160)
a=0.1
for i in range (120):
    if i < 60:
        pensize(a)
        forward(3)
        a+=0.15
        if i<10:
            left(2)
        elif 10(8)
        elif 15(6)
        elif 35(4)
        elif 40(1)
    elif i >=60:
        pensize(a)
        forward(3)
        a-=0.15
        if 60(0.4)
        elif 90(0.8)
setheading(0)
penup()
goto(80,250)
pendown()
left(245)
a=0.2
for i in range (80):
    if i < 40:
        pensize(a)
        forward(1.2)
        a+=0.15
        if i<15:
            left(0.6)
        elif 15(0.3)
    elif i >=40:
        pensize(a)
        forward(1.2)
        a-=0.15
        if 40(0.6)
        elif 65(0.35)
setheading(0)
penup()
goto(-150,195)
pendown()
left(300)
a=0.2
for i in range (80):
    if i < 40:
        pensize(a)
        forward(1.2)
        a+=0.16
        if i<15:
            right(0.6)
        elif 15(0.3)
    elif i >=40:
        pensize(a)
        forward(1.2)
        a-=0.16
        if 40(0.6)
        elif 65(0.35)
setheading(0)
penup()
goto(-205,225)
pendown()
left(194)
a=0.1
for i in range (100):
    if i < 50:
        pensize(a)
        forward(3)
        a+=0.15
        if i<10:
            left(7)
        elif 10(3)
        elif 30(0.1)
    elif i >=50:
        pensize(a)
        forward(3)
        a-=0.15
        if 50(0.3)
        elif 80(0.6)
setheading(0)
penup()
goto(110,280)
pendown()
left(6)
a=0.1
for i in range (120):
    if i < 60:
        pensize(a)
        forward(3)
        a+=0.15
        if i<10:
            right(4.5)
        elif 10(5)
        elif 30(0.1)
    elif i >=60:
        pensize(a)
        forward(3)
        a-=0.15
        if 50(0.6)
        elif 80(0.9)
setheading(0)
penup()
goto(-160,110)
pendown()
left(283)
a=0.2
for i in range (80):
    if i < 40:
        pensize(a)
        forward(2.4)
        a+=0.15
        if i<10:
            right(0.6)
        elif 15(0.1)
    elif i >=40:
        pensize(a)
        forward(2.4)
        a-=0.15
        if 40(2)
        elif 65(3)
setheading(0)
penup()
goto(90,155)
pendown()
left(258)
a=0.2
for i in range (80):
    if i < 40:
        pensize(a)
        forward(3)
        a+=0.15
        if i<10:
            left(0.8)
        elif 15(0.1)
    elif i >=40:
        pensize(a)
        forward(3)
        a-=0.15
        if 40(2)
        elif 65(3)
setheading(0)
penup()
goto(-60,-45)
pendown()
right(9)
a=0.2
for i in range (40):
    if i < 20:
        pensize(a)
        forward(1.3)
        a+=0.15
        if i<5:
            left(0.7)
        elif 5(0.4)
    elif i >=20:
        pensize(a)
        forward(1.3)
        a-=0.15
        if 20(0.7)
        elif 35(0.4)
setheading(0)
penup()
goto(-70,-55)
pencolor("green")
pendown()
right(170)
a=0.2
for i in range (80):
    if i <40 :
        pensize(a)
        forward(2)
        a+=0.15
        if i<25:
            right(0.5)
        elif 25(1)
    elif i >=40:
        pensize(a)
        forward(2.5)
        a-=0.15
        if 40(1)
        elif 55(2)
        elif 70(1)
setheading(270)
left(30)
a=0.2
for i in range (80):
    if i <40 :
        pensize(a)
        forward(2.5)
        a+=0.15
        if i<25:
            left(0.5)
        elif 25(1)
    elif i >=40:
        pensize(a)
        forward(3)
        a-=0.15
        if 40(2)
        elif 55(1.3)
        elif 70(1)
setheading(270)
penup()
goto(20,-55)
pendown()
left(65)
a=0.2
for i in range (60):
    if i <30 :
        pensize(a)
        forward(2)
        a+=0.15
        if i<15:
            left(1)
        elif 15(2)
    elif i >=30:
        pensize(a)
        forward(2.5)
        a-=0.15
        if 30(2)
        elif 45(2.5)
        elif 50(1.5)
setheading(270)
right(17)
a=0.2
for i in range (80):
    if i <40 :
        pensize(a)
        forward(2.2)
        a+=0.15
        if i<25:
            right(1)
        elif 25(2)
    elif i >=40:
        pensize(a)
        forward(2.5)
        a-=0.15
        if 40(2)
        elif 55(1.5)
        elif 70(0.6)
setheading(0)
penup()
pencolor("brown")
goto(-49,-90)
pendown()
right(89)
a=1
for i in range (120):
    if i < 60:
        pensize(a)
        forward(3.5)
        a+=0.08
    elif i >=60:
        pensize(a)
        forward(3.5)
        a-=0.08
setheading(0)
penup()
goto(-16,-70)
pendown()
right(91)
a=1
for i in range (120):
    if i < 60:
        pensize(a)
        forward(3.5)
        a+=0.08
    elif i >=60:
        pensize(a)
        forward(3.5)
        a-=0.08
setheading(0)

speed(1)
penup()
goto(-16,-150)
pendown()
pensize(3)
left(35)
forward(15)
setheading(0)
right(115)
forward(34)

setheading(180)
penup()
goto(-49,-250)
pendown()
pensize(3)
right(35)
forward(15)
setheading(180)
left(115)
forward(34)
input()
done

是不是让繁杂的代码整懵了,哈哈哈,总的来说就是不断套用循环的思路去绘制,只是其中用到的数理知识比较多,所以呐,大家如果想玩转Turtle库得好好温习下自己的数理学知识喽。
如果大家绘制中出现了问题,欢迎留言/私信提出,我会逐一解答问题,提前祝大家新年快乐!
了解更多知识请戳下:

@Author:懒羊羊

你可能感兴趣的:(Haphazard,python,线性代数,程序人生)