In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Python to achieve fireworks effect", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to use Python to achieve fireworks effect"!
The effect is as follows:
Here is the code to wish you all a happy New year.
Import pygameimport randomimport mathpygame.init () pygame.mixer.init () pygame.font.init () WIN_W = 1420WIN_H = 900t1 = 0.18 # time flow rate show_n = 0show_frequency = 0.0015 # fireworks frequency The higher the number, the higher the frequency color_list = 400shan_max_height = 150yanhua_times = 30yanhua_size = 900yanhua_num = 5yanhua_map = {} fk_list = [] a = 30gift = 1class Fireworks (): is_show = False x, y = 0,0 vy = 0 p_list = [] color = [0,0,0] v = 0 cnt = 0 def _ init__ (self, x, y, vy, nasty 300) Color= [0,255,0], vault 10): self.x = x self.y = y self.vy = vy self.color = color self.v = v self.cnt = 0 self.p_list = [] 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.98 # TODO will get bigger and bigger If you open it, it will become more and more light # light or thick 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 * t1def random_color (l, r): return [random.randint (l, r), random.randint (l, r), random.randint (l, r)] def red_random (l, r): return [255, random.randint (l, r) Random.randint (l, r)] def green_random (l, r): return [random.randint (l, r), 255,255, random.randint (l, r)] def init_yanhua (bg_size): yanhua_list = [] for i in range (yanhua_num): x_site = random.randrange Y_site = WIN_H # Snow Center position X_shift = 0 # x axis offset radius = random.randint (4,6) # Radius and y cycle rise and fall xxxxx = random_color (150,255) red = xxxxx [0] green = xxxxx [1] blue = xxxxx [2] yanhua_list.append ([x_site] Y_site, X_shift, radius, red, green, blue]) return yanhua_listdef init_xue (bg_size): snow_list = [] for i in range (0): 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) # red = xxxxx [0] # green = xxxxx [1] # blue = xxxxx [2] snow_list.append ([x_site, y_site, X_shift, radius, 255,255) ) return snow_listdef init_shan (bg_size): shan_list = [] # [x shan_min_height, green_random (1,150)] shan_list.append ([0, shan_min_height, color (1,150)]) shan_list.append ([200,200, shan_max_height, green_random (1,150)]) shan_list.append ([400,400, shan_min_height, green_random (1,150)]) shan_list.append Shan_max_height, green_random (1,150)) shan_list.append ([1000, shan_min_height, green_random (1,150)]) shan_list.append ([1000, shan_max_height, green_random (1,150)]) shan_list.append ([1200, shan_min_height, green_random (1,150)]) shan_list.append ([1400, shan_max_height, green_random (1) ) shan_list.append ([1600, shan_min_height, green_random (1,150)]) return shan_listdef draw_shan (shan_list: [], screen): n = len (shan_list) for i in range (1, n): pygame.draw.line (screen, shan_ list [I-1] [2], (shan_ list [I-1] [0], shan_ list [I-1] [1])) (shan_ list [I] [0], shan_ list [I] [1]), width=3) def draw_xue (snow_list: [], screen, bg_size: [], grand_has: set, grand_list: []): # Snowflake list cycle # Snow for i in range (len (snow_list)): # draw snowflakes in the air 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]] def show_yanhua (fk, screen, n): global show_n # if not fk.is_show: # fk.is_show = False # if random.random ()
< show_frequency * (n - show_n): # show_n += 1 # fk.again() 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]) # x, y = fk.x, fk.y if random.random() < 0.055: screen.set_at((int(x), int(y)), (int(fk.color[0]), int(fk.color[1]), int(fk.color[2]))) # 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.color[2])))def draw_yanhua(yanhua_list: [], screen, bg_size: []): global fk_list for i in range(len(yanhua_list)): # 绘制雪花,颜色、位置、大小 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][1] -= yanhua_list[i][3] # 如果雪花落出屏幕,重设位置 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 show_gift (flag, screen, bg_size: list, a): if flag = = 1: pygame.draw.rect (screen, red_random (1,150), (175,bg_size [1]-105) (200,100)) pygame.draw.line (screen, (255,225,79), (200,225,79), (200,225,79), width=3) pygame.draw.line (screen, (255,225,79), (350,225,79), (350,350bg_size [1]-5), width=3) pygame.draw.line (screen, (255,225) Pygame.draw.line (screen, (255,225,79), (350,225,79), (350,350,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79), (width=3) pygame.draw.line (screen, (255,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79), (350,225,79 A / 2) def show_gift2 (flag, screen, bg_size: list, a): if flag = = 1: pygame.draw.rect (screen, red_random (1,150), (1044, 793), (1241)) pygame.draw.line (screen, (255,225,79), (bg_size [0]-200, bg_size [1]-1241), (bg_size [0]-200) Bg_size [1]-5), width=3) pygame.draw.line (screen, (255,225,79), (bg_size [0]-350, bg_size [1]-105), (bg_size [0]-350, bg_size [1]-5), width=3) pygame.draw.line (screen, (255,225,79), (bg_size [0]-200, bg_size [1]-105) (bg_size [0]-350, bg_size [1]-5), width=3) pygame.draw.line (screen, (255,225,79), (bg_size [0]-350, bg_size [1]-105), (bg_size [0]-200, bg_size [1]-5), width=3) pygame.draw.circle (screen, (255,225,79), (bg_size [0]-275) Bg_size [1]-55), a / 2) def draw_yueliang (screen): pygame.draw.circle (screen, (200,200,200), (1350, 50), 100100) 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\\ Zuhai-good luck .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) shan_list = init_shan (bg_size) # Game main cycle while True: show_n = 0 if not pygame.mixer.music.get_busy (): pygame.mixer.music.play () for Event in pygame.event.get (): if event.type = = pygame.MOUSEBUTTONUP: print (event) if event.type = = pygame.QUIT: exit () screen.fill (0 0 0) flag = 0 min_height = 100000 # todo snow cover for i in range (len (grand_list)): if grand_ list [I] [0] < 375 and grand_ list [I] < bg_size [1]-gift: flag = 1 if grand_ list [I] [0] < 375: min_height = min (min_height Grand_ list [I] [1]) # if len (fk_list)! = 0: # print (len (fk_list)) # # fireworks show_shi (font_values, 0, screen) draw_yueliang (screen) draw_shan (shan_list, screen) if flag = 1: draw_yanhua (yanhua_list, screen) Bg_size) 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]) screen.set_at ((int (x), int (y)) (int (fk.color [0]), int (fk.color [1]), int (fk.color [2])) fk.cnt = fk.cnt + 1 tmp = [] for fk in fk_list: if fk.cnt
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.