Python程序:雪花+烟花

SnowFirework

本程序非原创,借鉴于网上流传甚广的烟花与雪花代码,本人只是将两段程序结合起来,并稍微调整了一下结构使其更加清晰,仅供学习之用,如有侵权,联系删除。

本项目的github链接如下:https://github.com/Mr-Nan05/SnowFirework

接下来分三个部分简单介绍一下

基本参数

需要用到的第三方库如下

import pygame
import math
import random

一些基本参数如下,比较容易理解,就不多介绍了


WIN_W = 1000
WIN_H = 544

t1 = 0.18
show_n = 0
show_frequency = 0.002

color_list = [
                [255, 50, 50],
                [50, 255, 50],
                [50, 50, 255],
                [255, 255, 50],
                [255, 50, 255],
                [50, 255, 255],
                [255, 255, 255]
             ]

背景设置

包括背景音乐,背景图片,窗口大小等,功能已经分配清楚,注释也比较清楚,就直接放在这里了

class BackGround:
    img_path = "flower.png"

    msc_path = 'alarm.mp3'
    img_size = (WIN_W, WIN_H)

    def __init__(self):

        pygame.init()

        self.clock = pygame.time.Clock()
        self.bgp_set()
        self.bgm_set()

    def bgp_set(self):

        self.screen = pygame.display.set_mode(self.img_size)
        pygame.display.set_caption("Happy New Year!")
        self.bgp = pygame.image.load(self.img_path)

    def bgm_set(self):

        pygame.mixer.music.load(self.msc_path)
        pygame.mixer.music.play()
        pygame.mixer.music.fadeout(99999999)

烟花绽放

此部分基本完全来自与网上,具体解释参照此博客,以及感谢帮助

博客链接:https://blog.csdn.net/wulishinian/article/details/105248757

烟花的类代码附上如下,每一个烟花都是一个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 = [Fireworks(300, 300, -20, n=100, color=[0, 255, 0], v=10),
           Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=11),
           Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=12),
           Fireworks(300, 300, -20, n=500, color=[0, 0, 255], v=12),
           Fireworks(300, 300, -20, n=600, color=[0, 0, 255], v=13),
           Fireworks(300, 300, -20, n=700, color=[255, 0, 0], v=15),
           Fireworks(300, 300, -20, n=800, color=[255, 255, 0], v=18)]

控制烟花绽放的代码如下,其中background就是BackGround的实例化对象

def firework(background):

    global show_n
    global show_frequency
    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:
                background.screen.set_at((int(x), int(y)), (255, 255, 255))
            else:
                background.screen.set_at((int(x), int(y)), (int(fk.color[0]), int(fk.color[1]), int(fk.color[2])))

雪花飘落

此部分代码已经来源网络,具体代码解释参照如下博客链接,感谢兄台,如有侵权,联系删除

博客链接:https://blog.csdn.net/weixin_48923393/article/details/110508756

基本逻辑是使用一个列表存储随机产生的每一片雪花的位置与其他参数,然后做一个循环,使所有的雪花缓慢飘落

产生雪花的代码如下


snow_list = []

def snowflake(background):
    for i in range(150):
        x_site = random.randrange(0, background.img_size[0])
        y_site = random.randrange(0, background.img_size[1])
        shift = random.randint(-1, 1)
        radius = random.randint(4, 6)
        snow_list.append([x_site, y_site, shift, radius])

然后使用下面的函数遍历雪花列表,循环飘落,具体代码解释还是参照原博客

def snowing(background):

    for i in range(len(snow_list)):

        pygame.draw.circle(background.screen, (255, 255, 255), 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] > background.img_size[1]:
            snow_list[i][1] = random.randrange(-50, -10)
            snow_list[i][0] = random.randrange(0, background.img_size[0])

二者结合

做一个循环,将调用二者的方法放在一起,可以通过控制snowing方法和firework方法的调用比例来调整二者的相对速率

具体代码如下

def show(background):
    done = False
    while not done:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        background.screen.blit(background.bgp, (0, 0))

        snowing(background)

        firework(background)

        pygame.display.flip()
        background.clock.tick(20)

    pygame.quit()

最后将整个调用过程放在一起

if __name__ == "__main__":
    bg = BackGround()
    snowflake(bg)
    show(bg)

运行效果

将运行结果简单截了几张图,如果需要浏览运行视频请移步github,链接在最上方,谢谢

Python程序:雪花+烟花

Python程序:雪花+烟花
Python程序:雪花+烟花
Python程序:雪花+烟花

Original: https://blog.csdn.net/wolverine1999/article/details/112342879
Author: MrNan05
Title: Python程序:雪花+烟花

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/781722/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球