preface
The annual Christmas is approaching, and various blogs are beginning to appear on blogs. Of course, the circle of friends is also out of the circle. In the CSDN search column, we found that the reading volume of blog posts has exceeded [] 10000. Of course, we can't wet the heat. We must arrange...
Novice teaching, everybody hurry up
I won't say much. Let's see the final result first
Christmas tree with fallen leaves + colored lights + fixed snowflakes + blessing pop-up window
Word cloud picture of Christmas tree
Christmas tree with dynamic snowflakes
Do you think it's amazing to see here? Then start working. Don't miss such a beautiful day...
Can we start with one click and three links {like + follow + comment}
Hand in hand teaching drawing Christmas tree
Loading of resource library
The most important resource library in the production process of Christmas library is turtle.
The Turtle library is a very popular function library for drawing images in Python language. Imagine a little Turtle starting at the origin (0,0) of a coordinate system with a horizontal axis of x and a vertical axis of y. it moves in this plane coordinate system according to the control of a set of function instructions, so as to draw graphics on its crawling path.
import turtle as t import random import pygame import threading import time import tkinter as tk
If the partners who don't download can download with pip, we won't say much here. It should be noted that the image can be used to download safely, so the speed will be much faster.
Star drawing
The top of the Christmas tree is painted with a shining star. Pay attention to the setting of Star color and angle (144 degrees and 72 degrees).
# Draw stars t.left(90) t.forward(3 * n) t.color("orange", "yellow") # Define the color of stars. The outer ring is orange and the inner ring is yellow t.begin_fill() # Fill the drawing t.left(126) for i in range(5): # Draw five pointed stars t.forward(n / 5) t.right(144) # Angle of Pentagram t.forward(n / 5) t.left(72) # Continue to change the angle
Drawing of colored lights
Many small colored lights need to be hung on the Christmas tree. In order to show this effect, small circles of different colors and sizes replace small colored lights.
def drawlight(): # Define how to draw colored lights if random.randint(0,30) == 0: t.color('tomato') # Define the first color t.circle(6) # Define lantern size elif random.randint(0,30) == 1: t.color('orange') # Define the second color t.circle(3) # Define lantern size else: t.color("dark green") # Draw empty branches for the rest of the random numbers
Drawn Christmas tree as a whole
The drawing of Christmas tree needs to guide the recursive tree function, which is mainly operated by forword, right and backward in the turtle. At the same time, set the color of the Christmas tree to dark green.
# Tree function (recursive) def tree(d, s): if d <= 0: return t.forward(s) tree(d - 1, s * .8) t.right(120) tree(d - 3, s * .5) drawlight() # Call to draw small colored lights t.right(120) tree(d - 3, s * .5) t.right(120) t.backward(s) # Fallback function # Painting tree t.end_fill() t.right(126) t.color("dark green") t.backward(n * 4.8) tree(15, n) t.backward(n/2)
Draw the leaves of the Christmas tree
In order to simulate the effect of fallen leaves, small circles of different colors are used instead. Using the random function, two colors are controlled by judging the conditions. Of course, more colors can be added for drawing.
if random.randint(1, 2) == 1: t.color('sienna') else: t.color('lightcoral') # Light coral
The drawing is also controlled by turtles.
# Draw fallen leaves for i in range(200): a = 200 - 400 * random.random() b = 10 - 20 * random.random() t.speed(0) t.up() t.forward(b) t.left(90) t.forward(a) t.down() if random.randint(1, 2) == 1: t.color('sienna') else: t.color('lightcoral') # Light coral t.circle(4) t.up() t.backward(a) t.right(90)
Draw static snowflakes
When drawing snowflakes, you first need to define the number of snowflakes and the color of the brush {white}
for i in range(200): # How many snowflakes do you draw t.pencolor("white") # Define the brush color as white, in fact, the snowflake is white t.pu() # Pen lifting, pu=penup
Then the x and y coordinates of the snowflake are defined and controlled by the random function random
t.setx(random.randint(-350, 350)) # Define the x coordinate and select randomly from - 350 to 350 t.sety(random.randint(-100, 350)) # Define the y coordinate. Note that snowflakes generally don't fall on the ground, so they don't start from the too small longitudinal axis
The main drawing codes are as follows:
# Draw snowflakes def drawsnow(): # Define how to draw snowflakes t.speed(0) t.ht() # Hide pen head, ht=hideturtle t.pensize(2) # Define pen size for i in range(200): # How many snowflakes do you draw t.pencolor("white") # Define the brush color as white, in fact, the snowflake is white t.pu() # Pen lifting, pu=penup t.setx(random.randint(-350, 350)) # Define the x coordinate and select randomly from - 350 to 350 t.sety(random.randint(-100, 350)) # Define the y coordinate. Note that snowflakes generally don't fall on the ground, so they don't start from the too small longitudinal axis t.pd() # Write, pd=pendown dens = 6 # The number of snowflakes is set to 6 snowsize = random.randint(1, 10) # Define snowflake size for j in range(dens): # It's 6, that's 5 times, that's a snowflake pentagram # t.forward(int(snowsize)) #int() takes an integer t.fd(int(snowsize)) t.backward(int(snowsize)) # t.bd(int(snowsize)) #Note that there is no bd=backward, but there is fd=forward, a small bug t.right(int(360 / dens)) # Rotation angle drawsnow()
Draw dynamic snowflakes
Dynamic snowflake drawing requires pygame resource library, which needs to be initialized first
# Initialize pygame pygame.init() # Set the screen width and height, and adjust according to the background image bg_img = "1.png" # Set the screen length and width according to the size of the background picture bg_size = (841, 821) screen = pygame.display.set_mode(bg_size) pygame.display.set_caption("Christmas tree on a snowy night") bg = pygame.image.load(bg_img)
Then define the snowflake center position, x-axis offset, y-axis descent and radius
# Snowflake list snow_list = [] # Initialize Snowflake: [x coordinate, y coordinate, X-axis speed, Y-axis speed] for i in range(150): # Snowflake center position x_site = random.randrange(0, bg_size[0]) # Snowflake center position y_site = random.randrange(0, bg_size[1]) # x-axis offset X_shift = random.randint(-1, 1) # Radius and y-axis descent radius = random.randint(4, 6) snow_list.append([x_site, y_site, X_shift, radius]) # Create clock object clock = pygame.time.Clock()
Finally, the moving position and size of snowflakes are dynamically drawn through the message time cycle
done = False while not done: # Message event cycle, judge exit for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Black background # screen.fill((0, 0, 0)) screen.blit(bg, (0, 0)) # Snowflake list loop for i in range(len(snow_list)): # Draw snowflakes, color, position and size pygame.draw.circle(screen, (255, 255, 255), snow_list[i][:2], snow_list[i][3] - 3) # Move snowflake position (next cycle takes effect) snow_list[i][0] += snow_list[i][2] snow_list[i][1] += snow_list[i][3] # If snowflakes fall off the screen, reset the position if snow_list[i][1] > bg_size[1]: snow_list[i][1] = random.randrange(-50, -10) snow_list[i][0] = random.randrange(0, bg_size[0]) # Refresh screen pygame.display.flip() clock.tick(30)
Main drawing code
import random import pygame # Initialize pygame pygame.init() # Set the screen width and height, and adjust according to the background image bg_img = "1.png" # Set the screen length and width according to the size of the background picture bg_size = (841, 821) screen = pygame.display.set_mode(bg_size) pygame.display.set_caption("Christmas tree on a snowy night") bg = pygame.image.load(bg_img) # Snowflake list snow_list = [] # Initialize Snowflake: [x coordinate, y coordinate, X-axis speed, Y-axis speed] for i in range(150): # Snowflake center position x_site = random.randrange(0, bg_size[0]) # Snowflake center position y_site = random.randrange(0, bg_size[1]) # x-axis offset X_shift = random.randint(-1, 1) # Radius and y-axis descent radius = random.randint(4, 6) snow_list.append([x_site, y_site, X_shift, radius]) # Create clock object clock = pygame.time.Clock() done = False while not done: # Message event cycle, judge exit for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Black background # screen.fill((0, 0, 0)) screen.blit(bg, (0, 0)) # Snowflake list loop for i in range(len(snow_list)): # Draw snowflakes, color, position and size pygame.draw.circle(screen, (255, 255, 255), snow_list[i][:2], snow_list[i][3] - 3) # Move snowflake position (next cycle takes effect) snow_list[i][0] += snow_list[i][2] snow_list[i][1] += snow_list[i][3] # If snowflakes fall off the screen, reset the position if snow_list[i][1] > bg_size[1]: snow_list[i][1] = random.randrange(-50, -10) snow_list[i][0] = random.randrange(0, bg_size[0]) # Refresh screen pygame.display.flip() clock.tick(30) # sign out pygame.quit()
Happy christmas pop-up drawing
At this time, you need to use tkinter to draw, and randomly define the window size and position, font size and color.
# Pop up window making def dow(): window = tk.Tk() width = window.winfo_screenwidth() height = window.winfo_screenheight() a = random.randrange(0, width) b = random.randrange(0, height) window.title('Merry Christmas') window.geometry("200x50" + "+" + str(a) + "+" + str(b)) tk.Label(window, text='Merry Christmas!', # Label text bg='pink', # background color font=('..', 17), # Font and font size width=18, height=2 # Label length and width ).pack() # Fixed window position window.mainloop()
Loading of Merry Christmas songs
If you need to play a merry Christmas song while drawing a Christmas tree, you need to load the song file at the beginning
file=r'christmas.mp3' # The path of music pygame.mixer.init() # initialization track = pygame.mixer.music.load(file) # Load music file pygame.mixer.music.play() # Start playing music
Source code
# @Time : 2021/12/24 11:09 # @Author : WYJ # @File : christmas.py # @Software: PyCharm import turtle as t import random import pygame import threading import time import tkinter as tk # file=r'Joker Xue - Tardy.mp3' # The path of music # pygame.mixer.init() # initialization # track = pygame.mixer.music.load(file) # Load music file # pygame.mixer.music.play() # Start playing music n = 80.0 # Fast setting speed t.speed("fastest") # Background color: seashell shell color, pink t.screensize(bg='seashell') # You can change the color of the background # Draw stars t.left(90) t.forward(3 * n) t.color("orange", "yellow") # Define the color of stars. The outer ring is orange and the inner ring is yellow t.begin_fill() # Fill the drawing t.left(126) for i in range(5): # Draw five pointed stars t.forward(n / 5) t.right(144) # Angle of Pentagram t.forward(n / 5) t.left(72) # Continue to change the angle def drawlight(): # Define how to draw colored lights if random.randint(0,30) == 0: t.color('tomato') # Define the first color t.circle(6) # Define lantern size elif random.randint(0,30) == 1: t.color('orange') # Define the second color t.circle(3) # Define lantern size else: t.color("dark green") # Draw empty branches for the rest of the random numbers # Tree function (recursive) def tree(d, s): if d <= 0: return t.forward(s) tree(d - 1, s * .8) t.right(120) tree(d - 3, s * .5) drawlight() # Call to draw small colored lights t.right(120) tree(d - 3, s * .5) t.right(120) t.backward(s) # Fallback function # Painting tree t.end_fill() t.right(126) t.color("dark green") t.backward(n * 4.8) tree(15, n) t.backward(n/2) # Draw fallen leaves for i in range(200): a = 200 - 400 * random.random() b = 10 - 20 * random.random() t.speed(0) t.up() t.forward(b) t.left(90) t.forward(a) t.down() if random.randint(1, 2) == 1: t.color('sienna') else: t.color('lightcoral') # Light coral t.circle(4) t.up() t.backward(a) t.right(90) t.backward(b) # Draw snowflakes def drawsnow(): # Define how to draw snowflakes t.speed(0) t.ht() # Hide pen head, ht=hideturtle t.pensize(2) # Define pen size for i in range(200): # How many snowflakes do you draw t.pencolor("white") # Define the brush color as white, in fact, the snowflake is white t.pu() # Pen lifting, pu=penup t.setx(random.randint(-350, 350)) # Define the x coordinate and select randomly from - 350 to 350 t.sety(random.randint(-100, 350)) # Define the y coordinate. Note that snowflakes generally don't fall on the ground, so they don't start from the too small longitudinal seat axis t.pd() # Write, pd=pendown dens = 6 # The number of snowflakes is set to 6 snowsize = random.randint(1, 10) # Define snowflake size for j in range(dens): # It's 6, that's 5 times, that's a snowflake pentagram # t.forward(int(snowsize)) #int() takes an integer t.fd(int(snowsize)) t.backward(int(snowsize)) # t.bd(int(snowsize)) #Note that there is no bd=backward, but there is fd=forward, a small bug t.right(int(360 / dens)) # Rotation angle drawsnow() # Write down your signature t.color("red") # fill color t.up() # pen-up t.goto(200,-100) # Set start position t.down() # Write t.write("Merry Christmas-left handのtomorrow", align="center", font=("Comic Sans MS", 18, "normal")) t.ht() # t.done() t.hideturtle() time.sleep(2) # Pop up window making def dow(): window = tk.Tk() width = window.winfo_screenwidth() height = window.winfo_screenheight() a = random.randrange(0, width) b = random.randrange(0, height) window.title('Merry Christmas') window.geometry("200x50" + "+" + str(a) + "+" + str(b)) tk.Label(window, text='Merry Christmas!', # Label text bg='pink', # background color font=('..', 17), # Font and font size width=18, height=2 # Label length and width ).pack() # Fixed window position window.mainloop() threads = [] for i in range(10): # Number of cartridges required t = threading.Thread(target=dow) threads.append(t) time.sleep(0.01) threads[i].start()
Dynamic drawing of the whole process of Christmas tree
Christmas tree word cloud drawing
Drawing the word cloud of the Christmas tree is very simple. You only need more than ten lines of code to complete it. Of course, you need to prepare the mask image and txt text of the Christmas tree
from wordcloud import WordCloud import PIL.Image as image import numpy as np img_path = "wordcloud.png" # Output the generated word cloud picture path with open('./christmas.txt', 'r') as f: text = f.read() mask = np.array(image.open('./mask.png')) word_cloud = WordCloud(mask=mask).generate(str(text)) image_file = word_cloud.to_image() # Generate picture image_file.show() # display picture word_cloud.to_file(img_path) # Save the generated picture
appendix
Where's the big man Youngkers Draw a Christmas tree in Python Drew a Christmas tree, feel very quit, can learn from...
from turtle import * import time setup(500, 500, startx=None, starty=None) speed(0) pencolor("pink") pensize(10) penup() hideturtle() goto(0, 150) showturtle() pendown() shape(name="classic") # 1 seth(-120) for i in range(10): fd(12) right(2) penup() goto(0, 150) seth(-60) pendown() for i in range(10): fd(12) left(2) seth(-150) penup() fd(10) pendown() for i in range(5): fd(10) right(15) seth(-150) penup() fd(8) pendown() for i in range(5): fd(10) right(15) seth(-155) penup() fd(5) pendown() for i in range(5): fd(7) right(15) # 2 penup() goto(-55, 34) pendown() seth(-120) for i in range(10): fd(8) right(5) penup() goto(50, 35) seth(-60) pendown() for i in range(10): fd(8) left(5) seth(-120) penup() fd(10) seth(-145) pendown() for i in range(5): fd(10) right(15) penup() fd(10) seth(-145) pendown() for i in range(5): fd(12) right(15) penup() fd(8) seth(-145) pendown() for i in range(5): fd(10) right(15) penup() seth(-155) fd(8) pendown() for i in range(5): fd(11) right(15) # 3 penup() goto(-100, -40) seth(-120) pendown() for i in range(10): fd(6) right(3) penup() goto(80, -39) seth(-50) pendown() for i in range(10): fd(6) left(3) seth(-155) penup() fd(10) pendown() for i in range(5): fd(8) right(10) penup() fd(8) seth(-145) pendown() for i in range(7): fd(8) right(10) penup() fd(8) seth(-145) pendown() for i in range(7): fd(7) right(10) penup() fd(8) seth(-145) pendown() for i in range(7): fd(7) right(10) penup() fd(8) seth(-140) pendown() for i in range(7): fd(6) right(10) # 4 penup() goto(-120, -95) seth(-130) pendown() for i in range(7): fd(10) right(5) penup() goto(100, -95) seth(-50) pendown() for i in range(7): fd(10) left(5) penup() seth(-120) fd(10) seth(-155) pendown() for i in range(6): fd(8) right(10) penup() seth(-160) fd(10) seth(-155) pendown() for i in range(6): fd(8) right(10) penup() seth(-160) fd(10) seth(-155) pendown() for i in range(6): fd(8) right(10) penup() seth(-160) fd(10) seth(-160) pendown() for i in range(6): fd(8) right(10) penup() seth(-160) fd(10) seth(-160) pendown() for i in range(6): fd(8) right(10) penup() seth(-160) fd(10) seth(-165) pendown() for i in range(5): fd(10) right(11) # 5 penup() goto(-70, -165) seth(-85) pendown() for i in range(3): fd(5) left(3) penup() goto(70, -165) seth(-95) pendown() for i in range(3): fd(5) right(3) seth(-170) penup() fd(10) pendown() pendown() for i in range(10): fd(12) right(2) # 6 penup() goto(70, -165) pendown() seth(-90) pensize(8) pencolor("#de8891") circle(-20, 90) penup() goto(30, -185) pendown() seth(-180) pensize(8) pencolor("#de8891") fd(40) penup() goto(-5, -170) pendown() seth(-180) pensize(8) pencolor("#de8891") fd(35) def guest(x, y, z): penup() goto(x, y) seth(-z) pendown() for angel in range(5): fd(10) right(10) def guet(x, y, z): penup() goto(x, y) seth(-z) pendown() for angel in range(5): fd(10) left(10) def qu(x, y, z): penup() goto(x, y) seth(-z) pendown() for angel in range(5): fd(6) right(10) seth(-150) fd(20) # branch guest(-70, -150, 160) guest(100, -150, 160) guet(110, -110, 50) guest(160, -140, 150) qu(80, -120, 180) guest(70, -85, 165) guest(-40, -85, 165) guet(90, -50, 50) guest(130, -80, 150) pencolor("pink") qu(-40, -60, 180) pencolor('#de8891') qu(80, -30, 180) pencolor("pink") qu(40, 10, 180) pencolor("#de8891") guest(-60, 30, 120) guest(-20, -20, 150) guet(45, 40, 60) guest(-30, 40, 170) guest(-30, 110, 115) guet(40, 90, 60) guest(80, 50, 160) pencolor("#de8891") def hdj(x, y): penup() goto(x, y) seth(80) pendown() pensize(2) circle(5) seth(10) fd(15) seth(120) fd(20) seth(240) fd(20) seth(180) fd(20) seth(-60) fd(20) seth(50) fd(20) seth(-40) fd(30) seth(-130) fd(5) seth(135) fd(30) seth(-60) fd(30) seth(-150) fd(6) seth(110) fd(30) def uit(x, y): penup() goto(x, y) pendown() pensize(2) circle(5) seth(-10) fd(15) seth(90) fd(15) seth(200) fd(15) seth(160) fd(15) seth(-90) fd(15) seth(10) fd(15) seth(-60) fd(20) seth(-180) fd(5) seth(110) fd(20) seth(-90) fd(20) seth(-180) fd(6) seth(70) fd(15) hideturtle() def yut(x, y, z): penup() goto(x, y) pendown() seth(z) for po in range(5): fd(4) left(36) def ytu(x, y, z): penup() goto(x, y) pendown() seth(z) for kk in range(5): fd(4) left(36) # Small bow seth(0) uit(40, -160) hdj(-80, -120) yut(-67, -115, 120) yut(-86, -123, 150) hdj(40, -50) yut(52, -45, 130) yut(34, -55, 160) seth(0) uit(-20, -60) ytu(-4, -60, 100) ytu(-20, -60, 120) hdj(-30, 20) yut(-15, 25, 130) yut(-40, 20, 180) uit(30, 70) ytu(45, 70, 100) ytu(30, 70, 120) # Big bow pencolor("#f799e6") pensize(5) penup() seth(0) goto(0, 150) pendown() circle(10) seth(-15) fd(40) seth(90) fd(40) seth(200) fd(40) seth(160) fd(40) seth(-90) fd(40) seth(15) fd(40) seth(-70) pencolor("#f799e6") pensize(4) fd(40) seth(-180) fd(10) seth(100) fd(40) seth(-100) fd(40) seth(-180) fd(10) seth(70) fd(40) penup() seth(0) goto(0, 130) pencolor("pink") pendown() def iou(x, y, z): penup() goto(x, y) pencolor("#f799e6") pendown() seth(z) for po in range(10): fd(4) left(18) seth(0) iou(35, 145, 100) iou(-7, 145, 110) pencolor("red") pensize(7) penup() goto(-35, 135) pendown() # Christmas hat seth(-20) pensize(2) penup() goto(-30, -120) pencolor("black") pendown() fillcolor("red") fd(30) circle(4, 180) fd(30) circle(4, 180) penup() goto(-25, -115) seth(75) pendown() begin_fill() for i in range(5): fd(6) right(20) seth(-10) for i in range(5): fd(8) right(15) seth(145) for i in range(5): fd(5) left(2) seth(90) for i in range(5): fd(1) left(2) seth(-90) for i in range(4): fd(4) right(6) seth(161) fd(30) end_fill() pensize(1) pencolor("black") def koc(x, y, size): pensize(2) pencolor("black") penup() goto(x, y) pendown() begin_fill() fillcolor("yellow") for i in range(5): left(72) fd(size) right(144) fd(size) end_fill() # stars seth(-15) koc(-120, -70, 10) seth(10) koc(100, -20, 10) seth(-10) koc(10, 40, 10) seth(30) koc(-80, 60, 10) koc(100, -150, 10) koc(-140, -150, 10) koc(20, 120, 10) # Socks seth(-20) pensize(2) penup() goto(-20, 80) pencolor("black") pendown() fillcolor("red") fd(25) circle(4, 180) fd(25) circle(4, 180) penup() goto(-15, 80) pendown() begin_fill() fillcolor("red") seth(-120) fd(20) seth(150) fd(5) circle(7, 180) fd(15) circle(5, 90) fd(30) seth(160) fd(18) end_fill() penup() seth(0) goto(100, -230) pendown() write("Merry Christmas", align="right", font=("Founder yellow grass simplified", 24, "bold")) done()
I can't wait... I can't wait to try. Come on, I can't wait.... In the sprint....