In those years, the Christmas trees we painted

1 Introduction

It's a happy weekend again. After careful calculation, there is only one week before Christmas!

insert image description here

Dear friends, are you all ready to move, I hope you all have beautiful stories for Christmas!

Today we will teach you how to draw a Christmas tree using Python. . .

2 Christmas tree black and white version

The primary version is of course the simple effect of black and white characters, the code is as follows:

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

if __name__ == "__main__":
    demo1()

The running effect is as follows:
insert image description here
the Christmas tree temperament of the short and delicate character effect version comes up at once. . .

3 Turtle Drawing

Hahaha, do you feel cheated when you see the black and white version of the Christmas tree? It is different from the colorful world we imagined.
Well, to meet your needs, we then use the turtle package to realize the colorful version of the Christmas tree.

The turtle package itself is a drawing library, but with Python code, it can draw various complex graphics.

Many students are not familiar with the turtle (turtle drawing) package. Let's take a look at a piece of code for drawing a rectangle to get an intuitive understanding of it.

# 导入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()

Run the above code on the command line, a drawing window will pop up automatically, and then draw a rectangle:

insert image description here
As can be seen from the above code, the turtle drawing is to direct the turtle to move forward and turn, and the trajectory of the turtle's movement is the drawn line. To draw a rectangle, you only need to let the turtle move forward, turn right 90 degrees, and repeat 4 times.

Call the width() function to set the brush width, and call the pencolor() function to set the color. For more operations, please refer to the description of the turtle library .

After the drawing is completed, remember to call the done() function to let the window enter the message loop and wait for it to be closed. Otherwise, the window will be closed immediately since the Python process will end immediately.

4 Christmas tree color versions

Here we go, after a brief introduction to the basic operations of playing turtle drawing, let's draw our colored version of the Christmas tree, the code is as follows:


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()

The result of the operation is as follows:
insert image description here

5 summary

This article realizes the effect of using Python to draw black and white Christmas trees and color Christmas trees, and gives a complete code example.

Have you lost your studies?

6 Reference

link one
link two

insert image description here
Follow the official account "The Way of AI Algorithms" to get more information about AI algorithms.

Follow the official account, reply to the Christmas tree in the background , and you can get the source code.

Guess you like

Origin blog.csdn.net/sgzqc/article/details/122008925