iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >怎么用Python实现雪夜烟花景
  • 450
分享到

怎么用Python实现雪夜烟花景

2023-06-29 01:06:22 450人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

这篇文章主要为大家展示了“怎么用python实现雪夜烟花景”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么用Python实现雪夜烟花景”这篇文章吧。运行截图运行效果:什么?你说你看不清烟花?那

这篇文章主要为大家展示了“怎么用python实现雪夜烟花景”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么用Python实现雪夜烟花景”这篇文章吧。

运行截图

运行效果:

怎么用Python实现雪夜烟花景

什么?你说你看不清烟花?那我换一种颜色,请点开看。

怎么用Python实现雪夜烟花景

实现过程

准备工作

使用语言和框架:python、pygame。
安装pygame:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn pygame

你需要知道的基础知识

首先,pygame渲染是同步的,所以同屏渲染的点过多之后,就会造成卡顿的情况。
其次,pygame的代码逻辑是,周期性渲染一系列的屏,从而产生连续的动画。

你需要掌握的框架基础知识:

初始化过程

import pygamepygame.init()pygame.mixer.init()pygame.font.init()

获取字体

myfont = pygame.font.SysFont('simHei', 30)textsurface = myfont.render(a[i], False, random_color(150, 255))screen.blit(textsurface, (80, 30))

画圈

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)

加载背景音乐

screen = pygame.display.set_mode(bg_size)pygame.display.set_caption("新年快乐")bg = pygame.image.load(bg_img)pygame.mixer.music.load('D:\\CloudMusic\\小时姑娘 - 霞光-《精灵世纪》片尾曲.mp3')

核心代码

基础架子

首先,需要实现一个基础的事件循环的架子,如下:

def main():    global show_n    global fk_list    bg_size = (WIN_W, WIN_H)    screen = pygame.display.set_mode(bg_size)    pygame.display.set_caption("新年快乐")    pygame.mixer.music.load('D:\\CloudMusic\\小时姑娘 - 霞光-《精灵世纪》片尾曲.mp3')    font_values = ['新年快乐']    grand_has = set()    clock = pygame.time.Clock()    while True:        if not pygame.mixer.music.get_busy():            pygame.mixer.music.play()        for event in pygame.event.get():            if event.type == pygame.QUIT:                exit()        screen.fill((0, 0, 0))        ... ...        pygame.display.update()        time_passed = clock.tick(50)if __name__ == '__main__':    main()

下雪的过程

现在,需要实现下雪的过程,首先,考虑定义定义一堆初始化的下雪点

def 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)        snow_list.append([x_site, y_site, X_shift, radius, 255, 255, 255])    return snow_list

然后实现渲染雪的过程

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

集成到上面的架子中,效果如下:

怎么用Python实现雪夜烟花景

不过目前的下雪没有质感,可以考虑在底部堆一些雪,只需要在雪落到地上做特判即可。

雪落到地上堆起来的过程

在前面的下雪过程的代码中,我们维护了一个Grand_list的数组,目的就是维护堆雪的效果

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)

怎么用Python实现雪夜烟花景

效果图如上。

实现烟花的过程

首先定义出烟花类:

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

然后,我们需要画出烟花释放前上升的过程点,这部分与下雪的初始化差不多。

def init_yanhua(bg_size):    yanhua_list = []    for i in range(5):        x_site = random.randrange(0, WIN_W)  # 雪花圆心位置        y_site = WIN_H  # 雪花圆心位置        X_shift = 0  # x 轴偏移量        radius = random.randint(6, 10)  # 半径和 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_list

然后是画上升过程

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(0, bg_size[0])        if yanhua_list[i][1] <= random.randint(200, 400):            # todo 放烟花            fk = Fireworks(yanhua_list[i][0], yanhua_list[i][1], -20, n=300, color=red_random(1, 150), v=10)            fk_list.append(fk)            yanhua_list[i][1] = WIN_H            yanhua_list[i][0] = random.randrange(0, bg_size[0])

效果图如下:

怎么用Python实现雪夜烟花景

圈出来的就是上升过程的烟花。
最后就是绽放部分,其实在上升过程的代码中有维护,如果超过某个随机高度,就会生成一个烟花,只是没有渲染,现在我们把渲染加上。

 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])                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])))        tmp = []        for fk in fk_list:            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 y < WIN_H - 1000:                    tmp.append(fk)                    break        fk_list = tmp

最终的运行效果就如最顶上的效果一样。

完整代码

将上述过程进行组合,结果如下,感兴趣的朋友可以按自己的需求进行优化

import pygameimport randomimport mathpygame.init()pygame.mixer.init()pygame.font.init()WIN_W = 2200WIN_H = 1300t1 = 0.18  # 时间流速show_n = 0show_frequency = 0.0015  # 烟花绽放频率,数值越大频率越高color_list = [    [255, 0, 0]]yanhua_map = {}fk_list = []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 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 * 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 init_yanhua(bg_size):    yanhua_list = []    for i in range(5):        x_site = random.randrange(0, WIN_W)  # 雪花圆心位置        y_site = WIN_H  # 雪花圆心位置        X_shift = 0  # x 轴偏移量        radius = random.randint(6, 10)  # 半径和 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 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 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(0, bg_size[0])        if yanhua_list[i][1] <= random.randint(200, 400):            # todo 放烟花            fk = Fireworks(yanhua_list[i][0], yanhua_list[i][1], -20, n=300, color=red_random(1, 150), v=10)            fk_list.append(fk)            yanhua_list[i][1] = WIN_H            yanhua_list[i][0] = random.randrange(0, bg_size[0])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 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)    # 游戏主循环    while True:        if not pygame.mixer.music.get_busy():            pygame.mixer.music.play()        for event in pygame.event.get():            if event.type == pygame.QUIT:                exit()        screen.fill((0, 0, 0))        draw_yanhua(yanhua_list, screen, bg_size)        if len(fk_list) != 0:            print(len(fk_list))        # # 放烟花        show_shi(font_values, 0, screen)        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])                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])))        tmp = []        for fk in fk_list:            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 y < WIN_H - 1000:                    tmp.append(fk)                    break        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实现雪夜烟花景”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网Python频道!

--结束END--

本文标题: 怎么用Python实现雪夜烟花景

本文链接: https://www.lsjlt.com/news/321736.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么用Python实现雪夜烟花景
    这篇文章主要为大家展示了“怎么用Python实现雪夜烟花景”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么用Python实现雪夜烟花景”这篇文章吧。运行截图运行效果:什么?你说你看不清烟花?那...
    99+
    2023-06-29
  • PythonPygame制作雪夜烟花景
    目录运行截图实现过程核心代码基础架子下雪的过程雪落到地上堆起来的过程实现烟花的过程完整代码运行截图 运行效果: 什么?你说你看不清烟花?那我换一种颜色,请点开看。 实现过程 准备...
    99+
    2024-04-02
  • Python Pygame如何制作雪夜烟花景
    这篇文章将为大家详细讲解有关Python Pygame如何制作雪夜烟花景,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。运行截图运行效果:什么?你说你看不清烟花?那我换一种颜色,请点开看。实现过程...
    99+
    2023-06-29
  • 新年到教你如何用Python实现雪夜烟花景
    运行截图 运行效果: 什么?你说你看不清烟花?那我换一种颜色,请点开看。 实现过程 准备工作 使用语言和框架:python、pygame。安装pygame: pip instal...
    99+
    2024-04-02
  • 怎么用Python实现烟花效果
    本篇内容主要讲解“怎么用Python实现烟花效果”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python实现烟花效果”吧!效果如下:这里给出代码,祝各位老哥新年快乐。import&nbs...
    99+
    2023-06-29
  • 怎么使用JS+Canvas实现雪花纷飞的场景
    这篇文章主要介绍“怎么使用JS+Canvas实现雪花纷飞的场景”,在日常操作中,相信很多人在怎么使用JS+Canvas实现雪花纷飞的场景问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么使用JS+Canvas...
    99+
    2023-07-04
  • python代码实现烟花实例
    实现代码如下: # -*- coding: utf-8 -*- import math, random,time import threading import tkinter ...
    99+
    2024-04-02
  • 怎么用Python代码实现最炫的烟花
    这篇文章主要介绍“怎么用Python代码实现最炫的烟花”,在日常操作中,相信很多人在怎么用Python代码实现最炫的烟花问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Python代码实现最炫的烟花”的疑...
    99+
    2023-06-29
  • MyBatis使用雪花ID怎么实现
    这篇文章主要介绍了MyBatis使用雪花ID怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇MyBatis使用雪花ID怎么实现文章都会有所收获,下面我们一起来看看吧。一、实现MyBatis ID构建接口@...
    99+
    2023-06-29
  • 用python实现漂亮的烟花demo
    link import tkinter as tk from PIL import Image, ImageTk from time import time, sleep from random import choice, unifo...
    99+
    2023-01-31
    烟花 漂亮 python
  • 怎么用PHP实现雪花算法
    本篇内容主要讲解“怎么用PHP实现雪花算法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用PHP实现雪花算法”吧!<phpclass SnowFlake{ &nbs...
    99+
    2023-06-21
  • 含源码,用Python实现浪漫烟花
    目录 前言 环境准备 代码编写 效果展示 前言 Python实现浪漫的烟花特效 现在很多地方都不能放烟花了,既然看不到, 那作为程序猿的我们还不能自己用代码做一个吗? 今天就带大家用代码做一个烟花特效吧。 环境准备 这里使用到的...
    99+
    2023-09-12
    python pandas
  • 怎么使用canvas实现烟花特效
    小编给大家分享一下怎么使用canvas实现烟花特效,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!canvas可以实现不同动画效果,本文主要记录几种不同节日烟花效果...
    99+
    2023-06-09
  • python实现烟花的实例代码怎么编写
    本篇文章给大家分享的是有关python实现烟花的实例代码怎么编写,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。实现代码如下:# -*- coding:&nb...
    99+
    2023-06-22
  • 基于Python实现加强版烟花
    上一篇实现的烟花里面预留了很多bug,今天过年,把代码给重新规划了一下。 效果如下: 远处是山和月亮,近处是雪和烟花。 由于忙着吃年夜饭,暂时就不详细解释代码了,空下来了再补上。 ...
    99+
    2024-04-02
  • Python实现新年烟花秀的代码怎么写
    今天就跟大家聊聊有关Python实现新年烟花秀的代码怎么写,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。 先介绍下 Pygame 绘制烟花的基本原理,烟花从发射到绽放一共分...
    99+
    2023-06-22
  • Python 浪漫烟花实现代码全解
    1 旖旎风景 马上虎年了,也是我的生肖年,很激动!(不小心暴露了年龄,哈哈哈......),这里先给大家拜年啦,祝大家虎年快乐,虎年爆富!  首先一首原创诗分享给大家,...
    99+
    2024-04-02
  • Python如何实现浪漫烟花效果
    这篇文章主要介绍了Python如何实现浪漫烟花效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1 旖旎风景马上虎年了,也是我的生肖年,很激动!(不小心暴露了年龄,哈哈哈.....
    99+
    2023-06-29
  • Python粒子烟花动态效果实现
    目录效果展示实现代码剩下代码跨年倒计时还有18天?我已经开始整烟花了,虽然不是很好看吧,但是也能将就看看 这个的背景图,音乐,还有文字都是可以自己修改的哦 效果展示 依次导入本次...
    99+
    2023-01-03
    Python粒子烟花 Python动态烟花 Python烟花
  • python实现跨年烟花动态效果
    目录Pygame 绘制烟花的基本原理用Python和Tkinter设计烟花将烟花绽放转译成代码使用Tkinter模拟朋友们,有多久没放烟花了?今年你所在的地方允许放烟花么?既然我们不...
    99+
    2023-01-03
    python实现跨年烟花 python实现烟花效果 python烟花
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作