python画烟花_python下pygame的烟花放送及代码解析

烟花

b64042b1-309a-4fc5-85a4-a3ac3958e470

第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)

效果图

0625a3ad0a474e0cb0a4d3daf1bcfa01

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Python 代码,可以用来绘制静态烟花: ```python import turtle import random turtle.speed(0) turtle.hideturtle() for i in range(50): turtle.penup() x = random.randint(-200, 200) y = random.randint(-200, 200) turtle.goto(x, y) turtle.pendown() turtle.dot(random.randint(10, 50), random.choice(["red", "orange", "yellow", "green", "blue", "purple", "pink"])) turtle.done() ``` 这段代码使用了 turtle 模块来绘制烟花,通过循环随机生成烟花的坐标和大小,然后随机选择颜色进行绘制。 如果你想要绘制动态烟花,可以使用 Pygame 或者 Turtle 的 tracer() 方法来实现。以下是一个使用 Pygame 的例子: ```python import pygame import random pygame.init() screen_width = 500 screen_height = 500 screen = pygame.display.set_mode((screen_width, screen_height)) clock = pygame.time.Clock() class Particle: def __init__(self, x, y, size, color, speed): self.x = x self.y = y self.size = size self.color = color self.speed = speed self.angle = random.uniform(0, 2 * math.pi) self.vx = self.speed * math.cos(self.angle) self.vy = -self.speed * math.sin(self.angle) self.gravity = 0.2 def move(self): self.x += self.vx self.y += self.vy self.vy += self.gravity def draw(self): pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size) particles = [] while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if random.random() < 0.2: x = random.randint(100, screen_width - 100) y = random.randint(100, screen_height - 100) size = random.randint(5, 20) color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) speed = random.uniform(1, 5) particle = Particle(x, y, size, color, speed) particles.append(particle) for particle in particles: particle.move() particle.draw() pygame.display.update() screen.fill((0, 0, 0)) clock.tick(60) ``` 这段代码使用了 Pygame 来创建窗口,并且定义了一个 Particle 类来表示烟花粒子。在主循环中,每隔一段时间就会生成一个新的烟花粒子,并且将它们添加到一个列表中。然后,在每一帧中,对列表中的所有粒子进行移动和绘制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值