python绘制春节烟花的示例代码

 更新时间:2024年02月11日 11:18:26   作者:尘中928  
这篇文章主要介绍了使用python 实现的简单春节烟花效果的示例代码,请注意,运行本文的代码之前,请确保计算机上已经安装了Pygame库,需要的朋友可以参考下

一、Pygame库春节烟花示例

下面是一个使用Pygame实现的简单春节烟花效果的示例代码。请注意,运行下面的代码之前,请确保计算机上已经安装了Pygame库。

import pygame
import random
import math
from pygame.locals import *
 
# 初始化pygame
pygame.init()
 
# 设置屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
 
# 设置标题
pygame.display.set_caption('春节烟花')
 
# 定义烟花参数
firework_speed = 5
firework_radius = 2
firework_explosion_radius = 60
colors = [
    (255, 0, 0),  # Red
    (0, 255, 0),  # Green
    (0, 0, 255),  # Blue
    (255, 255, 0),  # Yellow
    (255, 165, 0),  # Orange
    (255, 255, 255)  # White
]
 
# 定义Firework类
class Firework:
    def __init__(self, x, y, color, exploded=False):
        self.x = x
        self.y = y
        self.color = color
        self.exploded = exploded
        self.particles = []
 
    def move(self):
        if not self.exploded:
            self.y -= firework_speed
 
    def explode(self):
        if not self.exploded:
            for angle in range(0, 360, 5):
                dir_x = math.cos(math.radians(angle))
                dir_y = math.sin(math.radians(angle))
                self.particles.append((self.x, self.y, dir_x, dir_y, self.color))
            self.exploded = True
 
    def update(self):
        if self.exploded:
            for particle in self.particles:
                index = self.particles.index(particle)
                particle_x, particle_y, dir_x, dir_y, color = particle
                particle_x += dir_x * 2
                particle_y += dir_y * 2
                self.particles[index] = (particle_x, particle_y, dir_x, dir_y, color)
                if self.distance(particle_x, particle_y) > firework_explosion_radius:
                    self.particles.pop(index)
 
    def show(self):
        if not self.exploded:
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), firework_radius)
        else:
            for particle in self.particles:
                particle_x, particle_y, dir_x, dir_y, color = particle
                pygame.draw.circle(screen, color, (int(particle_x), int(particle_y)), firework_radius)
    
    def distance(self, x, y):
        return math.sqrt((self.x - x) ** 2 + (self.y - y) ** 2)
 
fireworks = [Firework(random.randint(0, screen_width), screen_height - 10, random.choice(colors))]
 
# 游戏主循环
running = True
while running:
    screen.fill((0, 0, 0))  # use a dark sky background
 
    # 执行事件循环
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
 
    # 更新和显示烟花
    for firework in fireworks:
        if not firework.exploded and firework.y < screen_height / 2 + random.randint(-100, 100):
            firework.explode()
        firework.move()
        firework.update()
        firework.show()
 
    # 随机发射新的烟花
    if random.randint(0, 20) == 1:
        fireworks.append(Firework(random.randint(0, screen_width), screen_height - 10, random.choice(colors)))
 
    # 删除已完成的烟花
    for firework in fireworks:
        if firework.exploded and len(firework.particles) == 0:
            fireworks.remove(firework)
 
    pygame.display.flip()
    pygame.time.Clock().tick(30)  # 控制游戏最大帧率为30fps
 
pygame.quit()

这个脚本创建了一些简单的烟花效果,它们会随机地在底部生成,并上升到屏幕的一半高度左右时爆炸。

二、在Windows 11上安装Pygame库

在Windows 11上安装Pygame库需要先确保电脑上有Python环境。Pygame是一个用Python语言编写的跨平台的游戏开发库。以下是在Windows 11上安装Pygame的一般步骤:

1. 安装Python:

如果电脑还没有安装Python,可以从Python官网下载安装包安装。地址是:https://www.python.org/downloads/。下载适合Windows的版本,运行安装程序,并确保在安装过程中选中了“Add Python to PATH”这个选项,以便在命令行中使用`python`命令。

2. 打开命令提示符(CMD)或 PowerShell:

安装了Python之后,按下Windows键,输入`cmd`或`PowerShell`,然后选择“命令提示符”或“Windows PowerShell”。确保以管理员身份运行它。

3. 更新pip(可选,但推荐):

虽然这一步不是必需的,但建议将pip更新到最新版本,以确保无缝安装库。在命令提示符或PowerShell中输入以下命令:

python -m pip install --upgrade pip

4. 安装Pygame:

现在,可以通过pip安装Pygame。在命令提示符或PowerShell中输入以下命令:

python -m pip install pygame

注意:如果电脑安装了多个Python版本,使用`python3`或者`py`命令替换`python`。

5. 验证安装:

为了验证Pygame是否成功安装,可以输入以下命令来导入Pygame,并查看其版本号:

python -c "import pygame; print(pygame.ver)"

这样Pygame应该就成功安装在indows 11系统上了。如果在安装过程中遇到问题,可能需要检查一下Python和pip是否正确安装以及是否已添加到系统的环境变量中。

三、turtle模块烟花示例

春节烟花通常是通过图形界面来实现的,下面用Python编写一个简单的烟花效果。我们将使用Python中的`turtle`模块来生成烟花效果。`turtle`是一个简单的图形绘制库,可以很容易地用来制作烟花动画。下面的Python代码演示了如何用`turtle`模块来绘制一个模拟烟花的图形:

import turtle
import random
 
# 设置屏幕背景
screen = turtle.Screen()
screen.bgcolor("black")
 
# 创建烟花的绘图对象
firework = turtle.Turtle()
firework.speed(0)
firework.hideturtle()
 
# 绘制烟花的方法
def draw_firework():
    colors = ["red", "yellow", "blue", "green", "orange", "purple", "white"]
    # 烟花升空
    firework.penup()
    firework.goto(random.randint(-200, 200), random.randint(-200, 0))
    firework.pendown()
    
    # 烟花爆炸
    explode_times = random.randint(5, 15)
    for i in range(explode_times):
        firework.color(random.choice(colors))
        firework.pensize(random.randint(1, 5))
        firework.speed(0)
        angle = 360 / explode_times
        firework.seth(i * angle)
        firework.forward(random.randint(20, 150))
        firework.backward(random.randint(20, 150))
 
# 重复绘制烟花
for _ in range(10):
    draw_firework()
 
# 点击屏幕后退出
screen.exitonclick()

确保有Python环境,然后运行这段代码。它将随机地在屏幕上绘制10个不同颜色和大小的烟花效果。可以通过增加循环次数或修改代码来创建更多不同的效果。

由于`turtle`库的性能限制,这个烟花动画的展示效果比较基础和有限。对于更加复杂的烟花动画,通常需要使用其他图形库,比如Pygame,或者在Web上使用JavaScript结合HTML5的Canvas。

以上就是python绘制春节烟花的示例代码的详细内容,更多关于python绘制春节烟花的资料请关注脚本之家其它相关文章!

相关文章

  • Python基础之变量的相关知识总结

    Python基础之变量的相关知识总结

    今天给大家带来的是关于Python的相关知识,文章围绕着Python变量展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • 详解Python如何利用turtle绘制中国结

    详解Python如何利用turtle绘制中国结

    春节是中国特有的传统节日,中国结是中华民族特有的纯粹的文化精髓,富含丰富的文化底蕴。本文将利用turtle绘制一个中国结,需要的可以参考一下
    2022-02-02
  • Python执行错误“由于找不到python39.dll,无法继续执行代码”解决的步骤

    Python执行错误“由于找不到python39.dll,无法继续执行代码”解决的步骤

    这篇文章主要介绍了在Python开发中遇到“找不到python39.dll”的错误,并提供了详细的解决方法,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2024-12-12
  • python 基于aiohttp的异步爬虫实战详解

    python 基于aiohttp的异步爬虫实战详解

    这篇文章主要为大家介绍了python 基于aiohttp的异步爬虫实战详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • windows安装TensorFlow和Keras遇到的问题及其解决方法

    windows安装TensorFlow和Keras遇到的问题及其解决方法

    这篇文章主要介绍了windows安装TensorFlow和Keras遇到的问题及其解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-07-07
  • Pandas时间数据处理详细教程

    Pandas时间数据处理详细教程

    日常工作中日期格式有多种表达形式,比如年份开头或是月份开头2022/6/4、6/4/2022等,通过pandas的日期数据处理,这篇文章主要给大家介绍了关于Pandas时间数据处理的相关资料,需要的朋友可以参考下
    2023-01-01
  • Python实现将JSON转换为CSV格式

    Python实现将JSON转换为CSV格式

    JSON(JavaScript  Object Notation)和 CSV(Comma-Separated Values)是数据交换和存储中最常用的两种格式,本文将详细介绍如何使用Free Spire.XLS for Python将JSON数据转换为 CSV 文件,感兴趣的小伙伴可以了解下
    2026-03-03
  • Python数据结构之队列详解

    Python数据结构之队列详解

    栈和队列是在程序设计中常见的数据类型。本节将详细介绍队列的定义及其不同实现,并且给出队列的一些实际应用,感兴趣的小伙伴可以了解一下
    2022-03-03
  • Miniconda创建环境时遇到UnsatisfiableError的问题解决

    Miniconda创建环境时遇到UnsatisfiableError的问题解决

    在使用Miniconda创建虚拟环境时,常因Python版本、缺失channel或平台不匹配导致UnsatisfiableError,通过合理指定软件源、分步安装、使用mamba加速及--dry-run预演,可高效定位并解决依赖冲突,感兴趣的可以了解一下
    2026-04-04
  • 解决在pycharm中显示额外的 figure 窗口问题

    解决在pycharm中显示额外的 figure 窗口问题

    今天小编就为大家分享一篇解决在pycharm中显示额外的 figure 窗口问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01

最新评论