2021 the first snow in the south this year is in Python

Hello, everyone

2021 is almost over, and the first snow of this year has already fallen in the north. However, it hasn't started to rain in the south, so I want to make a snow in Python and give it to my friends in the South who want to see the snow~

First introduce the tools used: it is implemented with pygame, and the Python version uses 3.8

First, install pygame by executing the following command

 pip install pygame

Then create a Python source file and write the following code to do some initialization

 import sys
 import random
 import pygame
 pygame.init()
 clock = pygame.time.Clock()
 bg_size = width, height = 1200, 750
 screen = pygame.display.set_mode(bg_size)
 bg = pygame.image.load('bg.jpg')

clock will be used to set the frame rate, bg_size is the size (width and height) of the application window. Screen is the created screen (application window) to which elements can be added. bg is a loaded picture used as the background of the window.

To make a snow effect, there are actually two steps. The first step is to define the snow, and the second step is to make the snow move.
1. Define snowflakes

The definition of snowflakes is relatively simple. We can use large and small circles instead. To define a circle, you need to have a center and radius. At the same time, we will also define the moving distance in this step.

Write a function to generate circles in batch

 def get_snows_circle(pic_num):
 """
 Generate an array and return some snowflakes, including the position, radius and movement of snowflakes on the screen x Coordinates and y coordinate
 :param pic_num: How many snowflakes
 :return: Array containing snowflake attributes
 """
 snows = []
 for i in range(pic_num):
 x_pos = random.randint(0, width) # Snowflakes in the screen x coordinate
 y_pos = random.randint(0, height) # y coordinate of snowflake on screen
 radius = random.randint(2, 4) # Radius of snowflakes
 x_delta = random.randint(-1, 1) # The distance the snowflake moves in the x-axis direction
 y_delta = random.randint(4, 6) # The distance the snowflake moves in the y-axis direction
 snows.append([[x_pos, y_pos], [x_delta, y_delta], radius])
 return snows

2. Let the snowflakes move

The idea of moving snowflakes is also relatively simple. When snowflakes are displayed on the screen for the first time, we follow x_delta and y_delta adjusts the center of the snowflake, and then refreshes the screen. At this time, the snowflake will move to a new position. Then we move the center again, and the snowflake can continue to move. Repeat this step continuously to achieve the effect of snow.

The above process can be done with a for loop.

 snow_pics = get_snows_circle(250)
 while True:
 for event in pygame.event.get(): # Listen to whether there is an exit event. The writing method is fixed
 if event.type == pygame.QUIT:
 sys.exit()
 screen.blit(bg, (0, 0)) # Set background image
 for snow_info in snow_pics:
 pos = snow_info[0]
 pos_delta = snow_info[1]
 pygame.draw.circle(screen, (255, 255, 255), pos, snow_info[2]) # Draw all the snowflakes on the screen
 screen.blit(snow_info[2], pos)
 according to x Shaft and y Axial movement
 pos[0] += pos_delta[0]
 pos[1] += pos_delta[1]
 if pos[1] > height: # If the screen height is exceeded, reset the starting position
 pos[0] = random.randint(0, width)
 pos[1] = random.randint(-50, -30) # If the y coordinate is set to a negative value, the initial snowflake will be outside the screen
 pygame.display.flip() # Refresh screen
 clock.tick(30) # No more than 30 frames per second, snowflakes fall more smoothly

Call get_snows_circle(250) generates 250 snowflakes, followed by a while loop to refresh the screen.

for snow_ info in snow_ The pics: code is what we started talking about, displaying 250 snowflakes, and then adjusting the center of the circle.

Other codes have comments, which will not be repeated here.
3. Add music

A little sound medicine still feels different. The way pygame adds music is also very simple. Just execute the following two sentences of code

 pygame.mixer.music.load('snow_down.mp3') # Play music, the sound of snow falling
 pygame.mixer.music.play()

I use the accompaniment version of the sound of snow falling, which sounds very good.
4. Another version

In addition to using circles, you can also use ready-made icons, such as

I used it to achieve a version, the effect is as follows

Personally, I think this circle is not good-looking. Although the icon is better than the circle, it is too square. On the contrary, the whole picture looks very rigid. This varies from person to person. You can also change the background picture into a sweet picture to have a programmer style romance.

Keywords: Python

Added by GaryAC on Thu, 23 Dec 2021 07:25:42 +0200