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

How to use Python to realize snowy night fireworks

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to use Python to achieve snowy night fireworks", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Python to achieve snowy night fireworks" this article.

Run screenshot

Running effect:

What? You said you couldn't see the fireworks clearly? Then I'll change my color. Please click on it.

Realization process

Preparatory work

Use languages and frameworks: python, pygame.

Install pygame:

Pip install-I https://pypi.tuna.tsinghua.edu.cn/simple/-- trusted-host pypi.tuna.tsinghua.edu.cn pygame

The basics you need to know

First of all, pygame rendering is synchronized, so if there are too many points rendered on the same screen, it will cause stutters.

Second, the code logic of pygame is to periodically render a series of screens to produce continuous animation.

The basics of the framework you need to master:

Initialization procedure import pygamepygame.init () pygame.mixer.init () pygame.font.init ()

Get font

Myfont = pygame.font.SysFont ('simHei', 30) textsurface = myfont.render (a [I], False, random_color (150,255)) screen.blit (textsurface, (80,30))

Draw a circle

Pygame.draw.circle (screen, (snow_ list [I] [4], snow_ list [I] [5], snow_ list [I] [6]), snow_ list [2], snow_ list [I] [3]-3)

Load background music

Screen = pygame.display.set_mode (bg_size) pygame.display.set_caption ("Happy New year") bg = pygame.image.load (bg_img) pygame.mixer.music.load ('D:\\ CloudMusic\\ hour Girl-Xiaguang-"Elf Century" finale .mp3') Core Code

Basic shelf

First, you need to implement a framework for a basic event loop, as follows:

Def main (): global show_n global fk_list bg_size = (WIN_W WIN_H) screen = pygame.display.set_mode (bg_size) pygame.display.set_caption ("Happy New year") pygame.mixer.music.load ('D:\\ CloudMusic\\ hour girl-Xiaguang-ending song of "Elf Century" .mp3') font_values = ['Happy New year'] grand_has = set () clock = pygame.time.Clock () while True: If not pygame.mixer.music.get_busy (): pygame.mixer.music.play () for event in pygame.event.get (): if event.type = = pygame.QUIT: exit () screen.fill (0 0, 0). Pygame.display.update () time_passed = clock.tick (50) if _ _ name__ = ='_ main__': main () the process of snow

Now, you need to implement the process of snow. first, consider defining a bunch of initialized snow points.

Def init_xue (bg_size): snow_list = [] for i in range: 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-week drop xxxxx = random_color (150,255) snow_list.append ([x_site, y_site, X_shift, radius, 255,255,255]) return snow_list

Then realize the process of rendering snow

Def draw_xue (snow_list: [], screen, bg_size: [], grand_has: set, grand_list: []): # snowflake list cycle # todo Snow for i in range (len (snow_list)): # draw snowflake Color, position, size pygame.draw.circle (screen, (snow_ list [I] [4], snow_ list [I] [5], snow_ list [I] [6]), snow_ list [I] [: 2] Snow_ list [I] [3]-3) # move snowflake position (the next cycle takes effect) snow_ list [I] [0] + = snow_ list [I] [2] snow_ list [I] + = snow_ list [I] [3] # if the snowflake falls off the screen Reset location if snow_ list [I] [1] > bg_size [1]: # tmp = [] snow_ list [I] [1] = random.randrange (- 50,-10) snow_ list [I] [0] = random.randrange (0 Bg_size [0]) x = snow_ list [I] [0] y = bg_size [1] while (grand_has.__contains__ (x * 10000 + y)): y = y-snow_ list [I] [3] grand_has.add (x * 10000 + y) grand_list.append ([x, y) Snow_ list [I] [2], snow_ list [I] [3], snow_ list [I] [4], snow_ list [I] [5], snow_ list [I] [6]]

Integrated into the shelf above, the effect is as follows:

However, there is no texture in the current snow, you can consider making some snow at the bottom, just need to make a special order when the snow falls to the ground.

The process of piling up snow when it falls to the ground

In the previous code for the snow process, we maintained an array of Grand_list to maintain the effect of making snow.

Min_height = 10000cm todo snow cover for i in range (len (grand_list)): if grand_ list [I] [0]

< 375: min_height = min(min_height, grand_list[i][1]) 然后进入维护程序: draw_xue(snow_list, screen, bg_size, grand_has, grand_list) 最后再将雪画出来 for i in range(len(grand_list)): pygame.draw.circle(screen, (grand_list[i][4], grand_list[i][5], grand_list[i][6]), grand_list[i][:2], grand_list[i][3] - 3) 效果图如上。 实现烟花的过程 首先定义出烟花类: class Fireworks(): is_show = False x, y = 0, 0 vy = 0 p_list = [] color = [0, 0, 0] v = 0 def __init__(self, x, y, vy, n=300, color=[0, 255, 0], v=10): self.x = x self.y = y self.vy = vy self.color = color self.v = v for i in range(n): self.p_list.append([random.random() * 2 * math.pi, 0, v * math.pow(random.random(), 1 / 3)]) def run(self): global show_n for p in self.p_list: p[1] = p[1] + (random.random() * 0.6 + 0.7) * p[2] p[2] = p[2] * 0.97 if p[2] < 1.2: self.color[0] *= 0.9999 self.color[1] *= 0.9999 self.color[2] *= 0.9999 if max(self.color) < 10 or self.y >

WIN_H + p [1]: show_n-= 1 self.is_show = False break self.vy + = 10 * T1 self.y + = self.vy * T1

Then, we need to draw the process point that rises before the fireworks are released, which is similar to the initialization of snow.

Def init_yanhua (bg_size): yanhua_list = [] for i in range (5): x_site = random.randrange (0, WIN_W) # snowflake center position y_site = WIN_H # snowflake center position X_shift = 0 # x axis offset radius = random.randint (6,10) # Radius and y cycle rise and fall xxxxx = random_color Red = xxxxx [0] green = xxxxx [1] blue = xxxxx [2] yanhua_list.append ([x_site, y_site, X_shift, radius, red, green, blue]) return yanhua_list

Then there is the rising process of painting.

Def draw_yanhua (yanhua_list: [], screen, bg_size: []): global fk_list for i in range (len (yanhua_list)): # draw snowflakes Color, position, size pygame.draw.circle (screen, (yanhua_ list [I] [4], yanhua_ list [I] [5], yanhua_ list [I] [6]), yanhua_ list [I] [: 2] Yanhua_ list [I] [3]-3) yanhua_ list [I] [0] + = yanhua_ list [I] [2] yanhua_ list [I]-= yanhua_ list [I] [3] if yanhua_ list [I] [1] bg_size [1]: # tmp = [] snow_list [I] [1] = random.randrange (- 50) -10) snow_ list [I] [0] = random.randrange (0) Bg_size [0]) x = snow_ list [I] [0] y = bg_size [1] while (grand_has.__contains__ (x * 10000 + y)): y = y-snow_ list [I] [3] grand_has.add (x * 10000 + y) grand_list.append ([x, y) Snow_ list [I] [2], snow_ list [I] [3], snow_ list [I] [4], snow_ list [I] [5], snow_ list [I] [6]]) def draw_yanhua (yanhua_list: [], screen, bg_size: []): global fk_list for i in range (len (yanhua_list)): # draw snowflakes Color, position, size pygame.draw.circle (screen, (yanhua_ list [I] [4], yanhua_ list [I] [5], yanhua_ list [I] [6]), yanhua_ list [I] [: 2] Yanhua_ list [I] [3]-3) # move snowflake position (the next cycle takes effect) yanhua_ list [I] [0] + = yanhua_ list [I] [2] yanhua_ list [I]-= yanhua_ list [I] [3] # if the snowflake falls off the screen Reset position if yanhua_ list [I] [1] = 0: myfont = pygame.font.SysFont ('simHei', 30) textsurface = myfont.render (a [I], False, random_color (150,255)) screen.blit (textsurface, (WIN_W / 2,30)) if j > = 0: myfont = pygame.font.SysFont (' simHei', 100) textsurface = myfont.render (a [j], False Red_random (1,1) screen.blit (textsurface, (WIN_W / 2-200,50)) def main (): global show_n global fk_list bg_size = (WIN_W WIN_H) screen = pygame.display.set_mode (bg_size) # bg_img = ". / 1.png" pygame.display.set_caption ("Happy New year") # bg = pygame.image.load (bg_img) pygame.mixer.music.load ('D:\\ CloudMusic\\ hour Girl-Xiaguang-"Elf Century" finale .mp3') grand_list = [] font_values = ['Happy New year'] grand_has = set () clock = pygame.time.Clock () yanhua_list = init_yanhua (bg_size) snow_list = init_xue (bg_size) # Game main loop while True: if not pygame.mixer.music.get_busy (): pygame.mixer.music.play () for event in pygame.event.get (): If event.type = = pygame.QUIT: exit () screen.fill (0 0,0) draw_yanhua (yanhua_list, screen, bg_size) if len (fk_list)! = 0: print (len (fk_list)) # # fireworks show_shi (font_values, 0, screen) for fk in fk_list: fk.run () for p in fk.p_list: X Y = fk.x + p [1] * math.cos (p [0]), fk.y + p [1] * math.sin (p [0]) if random.random () < 0.055: screen.set_at ((int (x), int (y), (255,255,255)) else: screen.set_at ((int (x)) Int (y)), (int (fk.color [0]), int (fk.color [1]), int (fk.color2)) tmp = [] for fk in fk_list: for p in fk.p_list: X, y = fk.x + p [1] * math.cos (p [0]) Fk.y + p [1] * math.sin (p [0]) if y < WIN_H-1000: tmp.append (fk) break fk_list = tmp min_height = 100000 # todo Snow for i in range (len (grand_list)): if grand_ list [I] [0] < 375: min_height = min (min_height Grand_ list [I] [1]) draw_xue (snow_list, screen, bg_size, grand_has, grand_list) for i in range (len (grand_list)): pygame.draw.circle (screen, (grand_ list [I] [4], grand_ list [5], grand_ list [I] [6]), grand_ list [I] [: 2] Grand_ list [I] [3]-3) pygame.display.update () time_passed = clock.tick (50) if _ _ name__ ='_ _ main__': main () above are all the contents of the article "how to use Python to realize Snow Night fireworks" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report