圣诞节前夕,我用Python画了一棵圣诞树,直接预判女朋友的预判

2021-12-18  本文已影响0人  Python百事通

前因:粉丝求助

最近有个粉丝私信我说“你能教我用Python写一个圣诞树吗,快圣诞节了,想给女朋友一个惊喜”

附图一张

当我看他发来这张图,我也是被我这个粉丝给“直”到了,估计回去之后免不了又要跪键盘。

当然,我也不能眼睁睁看着我的粉丝跪键盘却还无动于衷,于是我连夜用Python写了一个“圣诞树代码”来拯救我的粉丝。当然,肯定还有没有女朋友的小伙伴,你也不用灰心,趁着还有几天时间,我来给你开开小灶学会这个代码,向心仪的女孩子表白去吧。

有女朋友的更不用说了,别看了,说的就是你!你也想今晚回家跪键盘吗?

后果:开始代码

想要抱得美人归,代码自取通道→关注、私信“333”

1、一倍快乐

import turtle

screen = turtle.Screen()

screen.setup(375, 700)

circle = turtle.Turtle()

circle.shape('circle')

circle.color('red')

circle.speed('fastest')

circle.up()

square = turtle.Turtle()

square.shape('square')

square.color('green')

square.speed('fastest')

square.up()

circle.goto(0, 280)

circle.stamp()

k = 0

for i in range(1, 13):

    y = 30 * i

    for j in range(i - k):

        x = 30 * j

        square.goto(x, -y + 280)

        square.stamp()

        square.goto(-x, -y + 280)

        square.stamp()

if i % 4 == 0:

    x = 30 * (j + 1)

    circle.color('red')

    circle.goto(-x, -y + 280)

    circle.stamp()

    circle.goto(x, -y + 280)

    circle.stamp()

    k += 3

if i % 4 == 3:

    x = 30 * (j + 1)

    circle.color('yellow')

    circle.goto(-x, -y + 280)

    circle.stamp()

    circle.goto(x, -y + 280)

    circle.stamp()

square.color('brown')

for i in range(13, 17):

    y = 30 * i

    for j in range(2):

        x = 30 * j

        square.goto(x, -y + 280)

        square.stamp()

        square.goto(-x, -y + 280)

        square.stamp()

2.双倍快乐

class="highlight">

import turtle

# 定义圣诞树的绿叶函数

def tree(d, s):

    if d <= 0:

        return

    turtle.forward(s)

    tree(d - 1, s * .8)

    turtle.right(120)

    tree(d - 3, s * .5)

    turtle.right(120)

    tree(d - 3, s * .5)

    turtle.right(120)

    turtle.backward(s)

n = 100

""" 设置绘图速度

'fastest' : 0

'fast'  : 10

'normal' : 6

'slow'  : 3

'slowest' : 1

"""

turtle.speed('fastest') # 设置速度

turtle.left(90)

turtle.forward(3 * n)

turtle.color("orange", "yellow")

turtle.left(126)

# turtle.begin_fill()

for i in range(5):

    turtle.forward(n / 5)

    turtle.right(144)

    turtle.forward(n / 5)

    turtle.left(72)

    turtle.end_fill()

turtle.right(126)

turtle.color("dark green")

turtle.backward(n * 4.8)

# 执行函数

tree(15, n)

turtle.backward(n / 5)

3、终极快乐

猜猜最后这个是怎么写出来的。

最后

关注+点赞

代码领取私信“333”即可

希望每个小伙伴,能够在今年的圣诞节收获不一样的快乐,免受朋友圈的刷屏困扰。

上一篇下一篇

猜你喜欢

热点阅读