Python draws snowflakes all over the sky and walks around the world

  the snowflakes all over the sky and walking on the horizon have always been what I think is a very romantic picture. After research, it is finally implemented in Python! In this article, let's take a look at the snow map with background pictures and music.


1, Effect drawing of snowflakes all over the sky


The snowflakes all over the sky wander the horizon

The snow is cold and cold, which has been reincarnated for thousands of years. The prosperity fades into yellow tears. Does it wither or turn decay into magic?

The snowflakes all over the sky walk on the horizon


2. Watch the snow scenery side by side


The snow falling on the lake is like a condensate on the quiet lake. I just want to stand side by side with you for the beautiful scenery of thousands of miles of ice.

Two people snuggling up to each other under the snowflakes


3 hold the hand of your son and grow old with your son


Snowflakes are flying all over the sky. I just want to hold my hand. I accidentally turn white.

hold your hand and grow old together with you


2, Realization principle of snowflakes in the sky

The code implementation of snowflakes wandering the horizon is divided into the following three steps:

  • step1: draw snowy snowflakes and control the falling speed and offset.
  • Step 2: add a background picture of snow.
  • step3: play background music and control the pause and restart buttons.

Next, let's look at the specific implementation code.


3, Snowflakes all over the sky realize step-by-step explanation


1 load package


First load the package needed to draw the snowflakes all over the sky.

# -*- coding: utf-8 -*-

Code purpose: simulate the snowflakes all over the sky and walk on the horizon
 Author: Ali Yiyang

import pygame
import random
import os
from turtle import *
from pygame.locals import *


2 set the size of the window

You can set the full screen display window, or customize the window size according to the background picture.

os.chdir(r'F:\Wechat official account\Python\42_python snow')
pygame.init()          #Initialize pygame
SIZE = (790, 430)      #Set the screen width and length, and adjust it according to the background picture (or adjust the picture)
bg_size = width, height = 300, 200         #Set start and close interface window
bg_rgb = (255, 255, 255)
screen = pygame.display.set_mode(bg_size)
screen = pygame.display.set_mode(SIZE)
screen1 = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Snowflakes all over the sky, walking on the horizon")

This article adds background music. Click the background picture to pause the music, and then click again to play it again.

3 control the falling speed of snowflakes and add background pictures

You can change the background picture according to your preference.

bg = pygame.image.load('Two people strolled in the snow V3.jpg')
snow_list = []         #Snowflake list
for i in range(300):
    x_site = random.randrange(0, SIZE[0])   #Snowflake center position
    y_site = random.randrange(0, SIZE[1])   #Snowflake center position
    X_shift = random.randint(-1, 1)         #x-axis offset
    radius = random.randint(4, 6)           #Radius and y-cycle descent
    snow_list.append([x_site, y_site, X_shift, radius])  
clock = pygame.time.Clock()                 #Create set frame rate object
track ='Beautiful time is quiet.mp3')  # Load music file                            # Start playing music stream           #Set how long the music fades out and ends
play_image = pygame.image.load("Two people strolled in the snow V3.jpg").convert_alpha()   # Create and play picture surface object
pause_image = pygame.image.load("Two people strolled in the snow V3.jpg").convert_alpha()  # Create a suspended picture surface object
pause_rect = pause_image.get_rect()         #Get playback rectangle
print(pause_rect.width,pause_rect.height)   #Gets the pause rectangle
pause_rect.left, = (width - pause_rect.width) // 2, (height - pause_rect.height) // 2
pause = False        # Define playback flag bit

Partial parameter analysis:

bg: the added background picture can be replaced with your favorite background.

snow_list: location, radius, offset distance and descent distance of snowflakes.

track: add background music and replace it with your favorite music.

pause_image: the image used by the pause button, the background image directly used in this article.


4 realize dynamic snow through circulation

Update the location of snowflakes to achieve the effect of dynamic snow.

while True:
  for event in pygame.event.get():
    if event.type == QUIT:
  screen1.blit(bg, (0, 0))                #Picture background
  for i in range(len(snow_list)):, (255, 255, 255), snow_list[i][:2], snow_list[i][3]-3)
    snow_list[i][0] += snow_list[i][2]
    snow_list[i][1] += snow_list[i][3]
    if snow_list[i][1] > SIZE[1]:        #Determine whether the snowflake position exceeds the screen, and reset the position if yes
      snow_list[i][0] = random.randrange(0, SIZE[0])
      snow_list[i][1] = random.randrange(0, SIZE[1])
  pygame.display.flip()  #Refresh screen
pygame.quit()              #sign out 

Partial parameter analysis: the parameters are (Surface, color, pos, radius, width=0)

Surface: represents the object and represents the window screen.

Color: the color of the circle.

pos: center position.

Radius: radius of the circle.

Width: the width of the border. If it is set to 0, it means to fill the figure.
At this point, the explanation of the code of snowflake wandering the horizon is all over. If you need the full amount of executable code, you can reply * * "snowflake" * * in the official account, and you can get it for free.
You may be interested in:
Drawing Picchu in Python
Drawing word cloud with Python
Python face recognition - I only have eyes for you
Python draws a beautiful picture of the starry sky (beautiful background)
Operate neo4j with py2neo Library in Python to build the correlation map
Python romantic confession source code collection (love, roses, photo wall, advertisement under the stars)

Long press (scan) to recognize the QR code above, learn more Python and modeling knowledge, and make your study and work more brilliant.

Tags: Python pygame

Posted by shameermp on Mon, 23 May 2022 10:27:19 +0300