怎么用Python实现烟花效果
发表于:2025-01-16 作者:千家信息网编辑
千家信息网最后更新 2025年01月16日,本篇内容主要讲解"怎么用Python实现烟花效果",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么用Python实现烟花效果"吧!效果如下:这里给出代码,
千家信息网最后更新 2025年01月16日怎么用Python实现烟花效果
本篇内容主要讲解"怎么用Python实现烟花效果",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么用Python实现烟花效果"吧!
效果如下:
这里给出代码,祝各位老哥新年快乐。
import pygameimport randomimport mathpygame.init()pygame.mixer.init()pygame.font.init()WIN_W = 1420WIN_H = 900t1 = 0.18 # 时间流速show_n = 0show_frequency = 0.0015 # 烟花绽放频率,数值越大频率越高color_list = [ [255, 0, 0]]shan_min_height = 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, n=300, color=[0, 255, 0], v=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 去掉的话会越变越大,打开的话会越来越淡 # 淡或浓 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, random.randint(l, r)]def init_yanhua(bg_size): yanhua_list = [] for i in range(yanhua_num): x_site = random.randrange(175, 350) # 雪花圆心位置 y_site = WIN_H # 雪花圆心位置 X_shift = 0 # x 轴偏移量 radius = random.randint(4, 6) # 半径和 y 周上升降量 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(200): x_site = random.randrange(0, bg_size[0]) # 雪花圆心位置 y_site = random.randrange(0, bg_size[1]) # 雪花圆心位置 X_shift = random.randint(-1, 1) # x 轴偏移量 radius = random.randint(4, 6) # 半径和 y 周下降量 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, 255]) return snow_listdef init_shan(bg_size): shan_list = [] # [x,y,color] shan_list.append([0, shan_min_height, green_random(1, 150)]) shan_list.append([200, shan_max_height, green_random(1, 150)]) shan_list.append([400, shan_min_height, green_random(1, 150)]) shan_list.append([600, shan_max_height, green_random(1, 150)]) shan_list.append([800, 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, 150)]) 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: []): # 雪花列表循环 # todo 空中的雪 for i in range(len(snow_list)): # 绘制雪花,颜色、位置、大小 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) # 移动雪花位置(下一次循环起效) snow_list[i][0] += snow_list[i][2] snow_list[i][1] += snow_list[i][3] # 如果雪花落出屏幕,重设位置 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: # tmp = [] yanhua_list[i][1] = WIN_H yanhua_list[i][0] = random.randrange(175, 350) if yanhua_list[i][1] <= random.randint(150, 300): # todo 放烟花 fk = Fireworks(yanhua_list[i][0], yanhua_list[i][1], -20, n=yanhua_size, color=red_random(1, 150), v=10) fk_list.append(fk) yanhua_list[i][1] = WIN_H yanhua_list[i][0] = random.randrange(175, 350)def show_shi(a: list, n, screen): i = 2 * n - 1 j = 2 * n if i >= len(a): i = len(a) - 2 j = len(a) - 1 if i >= 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, bg_size[1] - 105), (200, bg_size[1] - 5), width=3) pygame.draw.line(screen, (255, 225, 79), (350, bg_size[1] - 105), (350, bg_size[1] - 5), width=3) pygame.draw.line(screen, (255, 225, 79), (200, bg_size[1] - 105), (350, bg_size[1] - 5), width=3) pygame.draw.line(screen, (255, 225, 79), (350, bg_size[1] - 105), (200, bg_size[1] - 5), width=3) pygame.draw.circle(screen, (255, 225, 79), (275, bg_size[1] - 55), 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, 893))) pygame.draw.line(screen, (255, 225, 79), (bg_size[0] - 200, bg_size[1] - 105), (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), 100, 100)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("新年快乐") # bg = pygame.image.load(bg_img) pygame.mixer.music.load('D:\\CloudMusic\\祖海 - 好运来.mp3') grand_list = [] font_values = ['新年快乐'] 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) # 游戏主循环 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 地上的积雪 for i in range(len(grand_list)): if grand_list[i][0] < 375 and grand_list[i][1] < 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)) # # 放烟花 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 <= yanhua_times: tmp.append(fk) break show_gift(flag, screen, bg_size, a) # show_gift2(flag, screen, bg_size, a) fk_list = tmp min_height = 100000 # todo 地上的积雪 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) pygame.display.update() time_passed = clock.tick(50)if __name__ == '__main__': main()
到此,相信大家对"怎么用Python实现烟花效果"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
雪花
位置
烟花
效果
圆心
循环
快乐
内容
半径
大小
屏幕
积雪
频率
颜色
偏移
学习
移动
实用
更深
代码
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
dell 塔式服务器技术
囚禁计划软件开发
分布式数据库与爬虫答辩
12312怎么服务器异常了
佛山专业软件开发靠谱吗
工具软件开发师
广东常用软件开发价格多少
禹州职业中专网络安全
泗洪环保网络技术保养
沐沐汐软件开发工作室
比特币网络需要服务器吗
东海品质网络技术创新服务
商品销售记账数据库
福州追风网络技术有限公司
医院ccn网络安全吗
无锡先导激光软件开发
杭州恒生软件开发公司
网络安全法 考试重点
稳定不抽风服务器
明日之后全部服务器升级
数据库如何查询多个手机号
河池计算机网络技术培训
数据库刷库的英文单词
道闸连接不了数据库
未来有前景的郑州软件开发
注意网络安全绘本
山东英才计算机网络技术
软件开发公司申请税收
金蝶数据库接口
数据库pl sql