Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of making snow scenes by Python?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge about "Python's method of making snow scene map". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

achieve

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

Find a picture you like as a background.

Added snow falling effect

Add music effects

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

bg_img = "bg.jpeg"bg_size = (900, 500)screen = pygame.display.set_mode(bg_size)pygame.display.set_caption("Snow View Map")bg = pygame.image.load(bg_img)

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

Then we will achieve the effect of snowflakes falling, 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]) #Snowflake center position y_site = random.randrange(0, bg_size[1]) #Snowflake center position X_shift = random.randint(-1, 1) #x-axis offset radius = random.randint(4, 6) #Radius and y-cycle drop snow_list.append([x_site, y_site, X_shift, radius])

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

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 snow_list[i][0] += snow_list[i][2] snow_list [i][1] += snow_list[i][3] #Reset position if snow falls off screen if snow_list[i][1] > bg_size[1]: snow_list[i][1] = random. random (-50, -10) snow_list[i][0] = random. random (0, bg_size[0])

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

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 cycle takes effect) snow_list[i][0] += snow_list[i][2] snow_list[i][1] += snow_list[i][3] #Reset position if snow falls off screen 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(20)

Finally, we add a musical effect to the snow scene. The code is as follows:

track = pygame.mixer.music.load ('my.mp3 ') #load music file pygame. mixer. music. play () #play music stream pygame.mixer.music.fadeout(100000) #set music end time "Python how to make snow map" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report