欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-14 16:14:43
...

(Turtle)????绘制Rose(????)

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

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

Turtle(????)

个人接触的第一个Python库就是Turtle,业内Guido称我们这类人为:画龟入门,也快过年了,学了这么长时间的语言了,也该给自己另一个她拿程序写个小东西,表达一下自己的爱慕之情,所以年前赶忙写了这篇博客,希望阔以帮助大家,新年里把这段代码送给自己的另一个她,不要再让外面传程序员不懂浪漫熬,证明自己,程序员的浪漫是俗人无法理解的,奥里给!

先让你们酸一下,上一张成品图

好不好看,观众姥爷说了算。

初出茅庐——利用Python的Turtle库绘制玫瑰花
此图应该是全网拿Python绘制最好看玫瑰花,自信一点说,自己动手翻阅博客,目前没有找到阔以与之媲美的,当然好看是需要代价的,繁琐的循环,500+行的代码,计算起来,复杂程度不用我多说,所以个人推荐:Copy走自己玩一玩发给另一个她,尽量不要自己动手瞧;当然如果有铁子想表达一下对另一个她的真心,阔以亲自动手敲一遍。

完整源代码

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

'''
#      a+=
#        if i<:
#            left()
#        elif <i<:
#            left(1)
#    elif i >=:
#        pensize(a)
#        forward()
#        a-=
#        if <i<:
#            left()
#        elif <i<:
#            left()
# setheading(270)
# for i in range ():
#    if i < :
#        pensize(a)
#        forward()
'''
#环境配备: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<i<24:
            left(1)
        elif 24<i<40:
            left(3)
        elif 40<i<50:
            left(4)
    elif i >=50:
        pensize(a)
        forward(2.2)
        a-=0.25
        if 50<i<65:
            left(6.7)
        elif 65<i<80:
            left(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<i<24:
            left(0.8)
        elif 24<i<40:
            left(1.7)
        elif 40<i<60:
            left(2.2)
    elif i >=60:
        pensize(a)
        forward(2.4)
        a-=0.15
        if 60<i<85:
            left(3.2)
        elif 85<i<90:
            left(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<i<20:
            right(3.5)
        elif 20<i<40:
            right(6.8)
        elif 40<i<50:
            right(0.6)
        elif 50<i<60:
            left(1)
    elif i >=60:
        pensize(a)
        forward(2.4)
        a-=0.16
        if 60<i<85:
            left(0.4)
        elif 85<i<90:
            left(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<i<15:
            left(8)
        elif 15<i<35:
            left(6)
        elif 35<i<40:
            left(4)
        elif 40<i<60:
            left(1)
    elif i >=60:
        pensize(a)
        forward(3)
        a-=0.15
        if 60<i<90:
            right(0.4)
        elif 90<i<120:
            right(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<i<40:
            left(0.3)
    elif i >=40:
        pensize(a)
        forward(1.2)
        a-=0.15
        if 40<i<65:
            left(0.6)
        elif 65<i<80:
            left(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<i<40:
            right(0.3)
    elif i >=40:
        pensize(a)
        forward(1.2)
        a-=0.16
        if 40<i<65:
            right(0.6)
        elif 65<i<80:
            right(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<i<30:
            left(3)
        elif 30<i<50:
            left(0.1)
    elif i >=50:
        pensize(a)
        forward(3)
        a-=0.15
        if 50<i<80:
            right(0.3)
        elif 80<i<100:
            right(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<i<30:
            right(5)
        elif 30<i<60:
            right(0.1)
    elif i >=60:
        pensize(a)
        forward(3)
        a-=0.15
        if 50<i<80:
            left(0.6)
        elif 80<i<100:
            left(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<i<40:
            right(0.1)
    elif i >=40:
        pensize(a)
        forward(2.4)
        a-=0.15
        if 40<i<65:
            left(2)
        elif 65<i<80:
            left(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<i<40:
            left(0.1)
    elif i >=40:
        pensize(a)
        forward(3)
        a-=0.15
        if 40<i<65:
            right(2)
        elif 65<i<80:
            right(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<i<20:
            left(0.4)
    elif i >=20:
        pensize(a)
        forward(1.3)
        a-=0.15
        if 20<i<35:
            left(0.7)
        elif 35<i<40:
            left(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<i<40:
            right(1)
    elif i >=40:
        pensize(a)
        forward(2.5)
        a-=0.15
        if 40<i<55:
            right(1)
        elif 55<i<70:
            right(2)
        elif 70<i<80:
            right(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<i<40:
            left(1)
    elif i >=40:
        pensize(a)
        forward(3)
        a-=0.15
        if 40<i<55:
            left(2)
        elif 55<i<70:
            left(1.3)
        elif 70<i<80:
            left(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<i<30:
            left(2)
    elif i >=30:
        pensize(a)
        forward(2.5)
        a-=0.15
        if 30<i<45:
            left(2)
        elif 45<i<50:
            left(2.5)
        elif 50<i<60:
            left(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<i<40:
            right(2)
    elif i >=40:
        pensize(a)
        forward(2.5)
        a-=0.15
        if 40<i<55:
            right(2)
        elif 55<i<70:
            right(1.5)
        elif 70<i<80:
            right(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:HaoXuan
#如果大家绘制中出现了问题,欢迎观众姥爷留言提出,我会逐一解答问题,提前祝大家新年快乐!