Article \ | wild guest

Source: Python Technology “ID: pythonall”

In recent days, there has been snow in many places, and people’s enthusiasm for the first snow of the year is obviously high. For example, many people go to various scenic spots to clock in despite the snow. Another example is that a big brother from northeast China wants to sell snow and deliver spicy cabbage to southerners live.

“For the seller, it’s like buying spicy cabbage as a gift of snow, while for southerners, it’s like buying snow as a gift of spicy cabbage,” commented Blogger Tang Seng. Melodyming2 replied, “Holy Monk, I have an idea.” If you are interested, you can go to the website to read the detailed report.

Back to the topic, this article we use Python to achieve snow with music falling snow map, function to achieve the use of Python library pyGame, has been introduced many times before, I believe you should be more familiar with.

implementation

The implementation of snow map is relatively simple, the basic idea is as follows:

  • Find an image you like as a background
  • Added snow flying effect
  • Add music effects

First of all, we will generate the main window and set the background image, the code implementation is as follows:

bg_img = "bg.jpeg"
bg_size = (900.500)
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("Snow Scene")
bg = pygame.image.load(bg_img)
Copy the code

The width and height of the window are set according to the size of the background.

Then we will implement the effect of falling snow, first to define a snowflake list, the code implementation is as follows:

snow_list = []
for i in range(150):
    x_site = random.randrange(0, bg_size[0] # select * from random. Randrange ()0, bg_size[1X_shift = random. Randint ()- 1.1Radius = random. Randint ()4.6# radius and y week drop snow_list.append([x_site, y_site, X_shift, radius])
Copy the code

Then to achieve the snowflake position update, to achieve the effect of dynamic snow, the code is implemented as follows:

for i in range(len(snow_list)): # Draw a snowflake, color, position, size pygame.draw. Circle (screen, (255.255.255), snow_list[i][:2], snow_list[i][3] - 3Snow_list [I][snow_list[I][0] += snow_list[i][2]
 snow_list[i][1] += snow_list[i][3If snow falls off the screen, reset the positionif 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])
Copy the code

Because we want to achieve the effect of continuous falling snow, so to set a loop to constantly refresh the screen, the code implementation is as follows:

While not done: # message event loopfor event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.blit(bg, (0.0) # snowflake list loopfor i in range(len(snow_list)): # Draw a snowflake, color, position, size pygame.draw. Circle (screen, (255.255.255), snow_list[i][:2], snow_list[i][3] - 3Snow_list [I][snow_list[I][0] += snow_list[i][2]
        snow_list[i][1] += snow_list[i][3If snow falls off the screen, reset the positionif 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()20)
Copy the code

Finally, we will add a piece of music effect for the snow map, the code implementation is as follows:

track = pygame.mixer.music.load('my.mp3') # loading music file pygame. Mixer. Music. Play () # play music flow pygame. Mixer. Music. Fadeout (100000) # Set the end time of the musicCopy the code

Here we have completed the snow map drawing work, let’s enjoy the GIF effect:

If you want to see (listen to) the snow map with music, you can run the program to feel, there is no video for you. \

conclusion

In this article we show you a way to create a snow scene with music in Python. If you are interested, you can try it out yourself.

PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan!

Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!

[Code access ****]

Identify the qr code at the end of the article, reply: 1202