Hello everyone, here is the list of code gods (a little boastful, ha ha). It's a newcomer. I hope you can give me more advice. [customary opening remarks]
Last weekend, the North ushered in the first snow of this year, a little earlier than that between Israel and Israel. The sudden snowfall made me want to make a snow animation in Python and give it to our friends in the south.
Here I use pygame to implement it, and the Python version uses 3.8. The implementation method is described below.
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, and screen is the created screen (application window) to which you can add elements. bg is a loaded picture used as the background image 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 pos_delta = snow_info pygame.draw.circle(screen, (255, 255, 255), pos, snow_info) # Draw all the snowflakes on the screen # screen.blit(snow_info, pos) # Move in the x-axis and y-axis directions pos += pos_delta pos += pos_delta if pos > height: # If the screen height is exceeded, reset the starting position pos = random.randint(0, width) pos = 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 a programmer like romantic picture.
In addition, remind everyone to keep warm in cold weather, especially friends who ride to and from work should pay attention to safety.