page contents

python动态烟花代码解析

本文讲述了python动态烟花代码解析!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2023-02-SlpK2G9F63ddc431c12b4.jpg

本文讲述了python动态烟花代码解析!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2023-02-eHt0O1Zk63ddc3f6cbe42.jpg

第1步:模块导入
import pygame, math, time, random, os
from sys import exit
第2步:定义相关初始
win(窗口的w=宽,h=高)
WIN_W = 2200
WIN_H = 1300
定义时间、显示、频率
t1 = 0.18 #时间流速
show_n = 0
show_frequency = 0.0015 #烟花绽放频率,数值越大频率越高
烟花颜色列表,下面随机抽出
color_list = [
[255, 50, 50],
[50, 255, 50],
[50, 50, 255],
[255, 255, 50],
[255, 50, 255],
[50, 255, 255],
[255, 255, 255]
]
初始化pygame和音乐mixer
pygame.init()
pygame.mixer.init()
创建一个窗口,pygame.RESIZABLE窗口大小可调节和标题
screen = pygame.display.set_mode((WIN_W, WIN_H),pygame.RESIZABLE, 32)
pygame.display.set_caption("五彩烟花大放送")
背景音乐,可自定义
sound_wav = pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play()
Fireworks=烟花,定义主类
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 again(self):
self.is_show = True
self.x = random.randint(WIN_W // 2 - 350, WIN_W // 2 + 350)
self.y = random.randint(int(WIN_H / 2), int(WIN_H * 3 / 5))
self.vy = -40 * (random.random() * 0.4 + 0.8) - self.vy * 0.2
self.color = color_list[random.randint(0, len(color_list) - 1)].copy()
n = len(self.p_list)
self.p_list = []
for i in range(n):
self.p_list.append([random.random() * 2 * math.pi, 0, self.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
烟花列表
fk_list = []
fk_list.append(Fireworks(300, 300, -20, n=100, color=[0, 255, 0], v=10))
fk_list.append(Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=11))
fk_list.append(Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=12))
fk_list.append(Fireworks(300, 300, -20, n=500, color=[0, 0, 255], v=12))
fk_list.append(Fireworks(300, 300, -20, n=600, color=[0, 0, 255], v=13))
fk_list.append(Fireworks(300, 300, -20, n=700, color=[255, 0, 0], v=15))
fk_list.append(Fireworks(300, 300, -20, n=800, color=[255, 255, 0], v=18))

clock = pygame.time.Clock()
烟花放送的主循环:
# 游戏主循环
while True:
if not pygame.mixer.music.get_busy():
pygame.mixer.music.play()
for event in pygame.event.get():
#pygame这个不能少,否则退出还在循环
if event.type == pygame.QUIT:
exit()

screen.fill((0, 0, 0))

# 放烟花
for i, fk in enumerate(fk_list):
if not fk.is_show:
fk.is_show = False
if random.random() < show_frequency * (len(fk_list) - show_n):
show_n += 1
fk.again()
continue
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.color[2])))

pygame.display.update()

time_passed = clock.tick(50)

好了代码写到这里就完成了。

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2023-02-04 10:34
  • 阅读 ( 532 )
  • 分类:Python开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1046 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章