In this article, we use Python to draw a snowy Christmas tree with musical effects. The basic idea is as follows:

  • Draw a Christmas tree in Python as the background
  • Add snow fall effect and music to Christmas tree background

Let’s look at the implementation.

First, let’s draw a Christmas tree. The main Python library is Turtle. The main code implementation is as follows:

n = 80.0
turtle.setup(700.700.0.0)
turtle.speed("fastest")
turtle.screensize(bg='black')
turtle.left(90)
turtle.forward(3 * n)
turtle.color("orange"."yellow")
turtle.begin_fill()
turtle.left(126)
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)
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)
tree(15, n)
turtle.backward(n / 2)
for i in range(200):
    a = 200 - 400 * random.random()
    b = 10 - 20 * random.random()
    turtle.up()
    turtle.forward(b)
    turtle.left(90)
    turtle.forward(a)
    turtle.down()
    if random.randint(0.1) = =0:
        turtle.color('tomato')
    else:
        turtle.color('wheat')
        turtle.circle(2)
        turtle.up()
        turtle.backward(a)
        turtle.right(90)
        turtle.backward(b)
time.sleep(60)
Copy the code

Take a look at the results:

Then add snow fall effect and music with Christmas tree as background. The Python library is mainly used as PyGame, and the main code is as follows:

Initialize PyGame
pygame.init()
Set the screen width and height, adjust according to the background image
bg_img = "bg.png"
bg_size = (609.601)
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("Snow Night Christmas Tree")
bg = pygame.image.load(bg_img)
# Snowflake list
snow_list = []
for i in range(150):
    x_site = random.randrange(0, bg_size[0])   # snowflake center position
    y_site = random.randrange(0, bg_size[1])   # snowflake center position
    X_shift = random.randint(-1.1)         # x offset
    radius = random.randint(4.6)           # radius and y cycle drop
    snow_list.append([x_site, y_site, X_shift, radius])
Create a clock object
clock = pygame.time.Clock()
# Add music
track = pygame.mixer.music.load('my.mp3')  Load the music file
pygame.mixer.music.play() # Play music stream
pygame.mixer.music.fadeout(600000)  # Set the end time of music
done = False
while not done:
    # message event loop, judge exit
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.blit(bg, (0.0))
    # Snowflake list loop
    for i in range(len(snow_list)):
        # Draw snowflakes, color, position, size
        pygame.draw.circle(screen, (255.255.255), snow_list[i][:2], snow_list[i][3] - 3)
        # Move snowflake position (next loop works)
        snow_list[i][0] += snow_list[i][2]
        snow_list[i][1] += snow_list[i][3]
        If snow falls 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 the screen
    pygame.display.flip()
    clock.tick(30)
# exit
pygame.quit()
Copy the code

Take a look at the final result:

I’m not going to play the video here, but if you want to listen to the music you can do it yourself.

Source code and corresponding files in the public number Python minor 2 background reply 201225 to obtain.