0

那些年,我们画过的圣诞树

 2 years ago
source link: https://blog.csdn.net/sgzqc/article/details/122008925
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

又到了让人开心的周末啦,细细一算,还有一周马上就圣诞节啦!

在这里插入图片描述

各位小伙伴们是不是都蠢蠢欲动啦,希望大家圣诞节都收获美美的故事呀!

今天我们来教大家怎么使用Python画圣诞树。。。

2 圣诞树黑白版本

初级版本当然就是黑白字符简易效果啦,代码如下:

def demo1():
    height = 5
    stars = 1
    for i in range(height):
        print((' ' * (height - i)) + ('*' * stars))
        stars += 2
    print((' ' * height) + '|')

if __name__ == "__main__":
    demo1()

运行效果如下:
在这里插入图片描述
短小精致的字符效果版圣诞树气质一下子就上来了。。。

3 海龟绘图

哈哈哈,看到黑白版圣诞树,是不是有种被欺骗的感觉,和我们想象的彩色世界是不一样滴。
好嘛,满足你的需求,我们接着使用turtle包来实现彩色版圣诞树。

turtle包本身是一个绘图库,但是配合Python代码,就可以绘制各种复杂的图形。

很多同学不太熟悉turtle(海龟绘图)包,我们来看一段绘制长方形的代码,来对其进行直观的认识。

# 导入turtle包的所有内容:
from turtle import *
# 设置笔刷宽度:
width(4)
# 前进:
forward(200)
# 右转90度:
right(90)
# 笔刷颜色:
pencolor('red')
forward(100)
right(90)
pencolor('green')
forward(200)
right(90)
pencolor('blue')
forward(100)
right(90)
# 调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
done()

在命令行运行上述代码,会自动弹出一个绘图窗口,然后绘制出一个长方形:

在这里插入图片描述
从上述代码可以看出,海龟绘图就是指挥海龟前进、转向,海龟移动的轨迹就是绘制的线条。要绘制一个长方形,只需要让海龟前进、右转90度,反复4次。

调用width()函数可以设置笔刷宽度,调用pencolor()函数可以设置颜色。更多操作请参考turtle库的说明。

绘图完成后,记得调用done()函数,让窗口进入消息循环,等待被关闭。否则,由于Python进程会立刻结束,将导致窗口被立刻关闭。

4 圣诞树彩色版本

来了,再简单介绍玩海龟画图的基本操作之后,我们来画我们彩色版的圣诞树,代码如下:


def demo2():
    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()

    turtle.done()

运行结果如下:
在这里插入图片描述

本文实现了利用Python分别绘制黑白版圣诞树和彩色版圣诞树效果,并给出了完整代码实例。

您学废了吗?

链接一
链接二

在这里插入图片描述
关注公众号《AI算法之道》,获取更多AI算法资讯。

关注公众号,后台回复圣诞树,即可获取源代码。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK