Use code to draw two Christmas trees for you [with detailed code]

Hello everyone, I am Ning Yi

The magic of code is that it can help us realize many weird and interesting ideas.

For example, using Python's Turtle library can help us draw beautiful images on the computer.

The cherry blossom picture below is implemented using the Turtle library.

It's not Christmas.

Then use code to draw a Christmas tree of your own.

I have prepared two Christmas trees for everyone. You can add your name and blessings on the second one, which is suitable for posting to Moments~

first christmas tree

Merry Christmas

Python's Turtle library can define the canvas size, pen color thickness, pen position, fill color, etc.

Similar to how we usually draw pictures by hand, the code for the first Christmas tree is a bit too much.

You can get it directly from [Meow Ningyi] Princess Hao Restoration "Christmas Tree".

I tested the position of each ball one by one in the middle of the night, and finally got it aligned——

But in fact, there are still many rules in how to draw a Christmas tree.

We can use a for loop to draw each fork.

We apply this method to the Christmas tree in Lesson 2.

The second Christmas tree is solved recursively.

Let’s see how to do it.

from turtle import *
import random
import time

n = 100.0
setup(500, 700, startx=None, starty=None)  
# 画笔速度
speed("fastest")
# 背景颜色
screensize(bg='cornflowerblue')
# 画笔颜色,填充颜色
color("dark green")
fillcolor('yellow')
pensize(10)
# 画笔方向,向上
left(90)
# 每笔像素
forward(2.8*n)  

# 随机生成颜色
def get_color():
    color_arr = ['light coral', 'tomato', 'orange red', 'red','brown',
    'firebrick','salmon', 'dark salmon','light salmon', 'orange', 
    'chocolate','yellow','gold', 'goldenrod', 
    'dark goldenrod', 'rosy brown','indian red', 'saddle brown', 
    'dark orange','coral',  'hot pink', 'deep pink',
    'pink', 'light pink','pale violet red', 'maroon', 'medium violet red', 
    'violet red','medium orchid']

    index = random.randint(0,len(color_arr)) - 1
    return color_arr[index]

# 画雪花
def snow(snow_count):
    hideturtle()
    pensize(2)
    for i in range(snow_count):
        pencolor("white")
        pu()
        goto(random.randint(-180, 180), random.randint(-180, 340))
        pd()
        dens = random.randint(10, 12)
        snowsize = random.randint(6, 10)
        for _ in range(dens):
            forward(snowsize)  # 向当前画笔方向移动snowsize像素长度
            backward(snowsize)  # 向当前画笔相反方向移动snowsize像素长度
            right(360 / dens)  # 顺时针移动360 / dens度

# 飘落名字
def name(name_count):
    hideturtle()
    pensize(2)
    for i in range(name_count):
        pencolor(get_color())
        pu()
        goto(random.randint(-220, 220), random.randint(-300, 340))
        pd()
        # 这里可以换成自己的名字、自己想要的词语
        name_arr = ['猫','宁一','平安','喜乐']
        namesize = random.randint(10, 15)
        index = random.randint(0,len(name_arr)) - 1
        dens = random.randint(10, 12)
        write(name_arr[index], align="right", font=("Arial", namesize, "bold"))

# 画星星
def koc(size):
  pensize(3)
  pencolor(get_color())
  begin_fill()
  fillcolor('yellow')
  for i in range(5):
      left(72)
      fd(size)
      right(144)
      fd(size)
  end_fill()

# 画树干
backward(n*4.8)
def tree(d, s):
    if d <= 0: return
    if d == 1:
        koc(5)
    pensize(d)
    forward(s)
    tree(d-1, s*.81)
    right(120)
    tree(d-3, s*.5)
    right(120)
    tree(d-3, s*.5)
    right(120)
    backward(s)
    color("dark green")

# 执行函数
tree(14, n)
snow(40)
name(15)

# 写Merry Christmas文字
penup()
seth(0)
goto(190, -305)
pendown()   
color("red") 
write("Merry Christmas", align="right", font=("Arial", 50, "bold"))

done()

Copy the above code, save it as a tree.py file on your computer, and finally run the file on the command line to see the effect.

python tree.py

Finally, I wish everyone not only a Merry Christmas, but also peace and joy every day in the future~~

Insert image description here

Guess you like

Origin blog.csdn.net/shine_a/article/details/128200508