【附源码】Python :简单球形粒子爆炸

系列文章目录

Python 算法学习:简单球形粒子爆炸



前言

运行程序后,可以在屏幕上点击鼠标,就会出现球形粒子爆炸效果。每次点击都会生成一些随机颜色、大小、速度和生命值的粒子,它们会在屏幕上飘动一段时间后消失


一、项目需求

用python写一个球形粒子爆炸效果,要求点击鼠标产生爆炸粒子效果

二、项目源码

1.引入库

代码如下(示例):

import pygame
import random
import math

2.定义粒子类

代码如下(示例):

# 定义粒子类
class Particle(pygame.sprite.Sprite):
    def __init__(self, pos, angle, speed, color):
        super().__init__()
        # 设置粒子的位置、速度和颜色
        self.pos = pos
        self.vel = [speed * math.cos(angle), speed * math.sin(angle)]
        self.color = color
        # 设置粒子的大小和生命值
        self.size = random.randint(5, 10)
        self.life = 100

    def update(self):
        # 更新粒子的位置和生命值
        self.pos[0] += self.vel[0]
        self.pos[1] += self.vel[1]
        self.life -= 1
        # 如果粒子的生命值小于等于0,则从精灵组中删除
        if self.life <= 0:
            self.kill()

    def draw(self, screen):
        # 在屏幕上绘制粒子
        pygame.draw.circle(screen, self.color, [int(self.pos[0]), int(self.pos[1])], self.size)

3.初始化与界面布局

代码如下(示例):

# 初始化Pygame
pygame.init()

# 设置屏幕大小和标题
screen_width, screen_height = 600, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Particle Explosion")

4.对象创建

代码如下(示例):

# 创建精灵组和时钟对象
particles = pygame.sprite.Group()
clock = pygame.time.Clock()

5.事件编写

代码如下(示例):

# 注册鼠标点击事件
def explode(x, y):
    # 在鼠标点击位置创建粒子
    for i in range(50):
        angle = random.uniform(0, math.pi * 2)
        speed = random.uniform(1, 5)
        color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        particle = Particle([x, y], angle, speed, color)
        particles.add(particle)

6.程序运行

代码如下(示例):

pygame.QUIT
# 运行程序
running = True
while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 在鼠标点击位置创建粒子
            explode(*pygame.mouse.get_pos())

    # 更新精灵组中的所有粒子
    particles.update()

    # 清空屏幕
    screen.fill((0, 0, 0))

    # 在屏幕上绘制所有粒子
    for particle in particles:
        particle.draw(screen)

    # 更新屏幕
    pygame.display.flip()

    # 控制帧率
    clock.tick(60)

# 退出Pygame
pygame.quit()

三、源码展示

1.源码展示

import pygame
import random
import math

# 定义粒子类
class Particle(pygame.sprite.Sprite):
    def __init__(self, pos, angle, speed, color):
        super().__init__()
        # 设置粒子的位置、速度和颜色
        self.pos = pos
        self.vel = [speed * math.cos(angle), speed * math.sin(angle)]
        self.color = color
        # 设置粒子的大小和生命值
        self.size = random.randint(5, 10)
        self.life = 100

    def update(self):
        # 更新粒子的位置和生命值
        self.pos[0] += self.vel[0]
        self.pos[1] += self.vel[1]
        self.life -= 1
        # 如果粒子的生命值小于等于0,则从精灵组中删除
        if self.life <= 0:
            self.kill()

    def draw(self, screen):
        # 在屏幕上绘制粒子
        pygame.draw.circle(screen, self.color, [int(self.pos[0]), int(self.pos[1])], self.size)

# 初始化Pygame
pygame.init()

# 设置屏幕大小和标题
screen_width, screen_height = 600, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Particle Explosion")

# 创建精灵组和时钟对象
particles = pygame.sprite.Group()
clock = pygame.time.Clock()

# 注册鼠标点击事件
def explode(x, y):
    # 在鼠标点击位置创建粒子
    for i in range(50):
        angle = random.uniform(0, math.pi * 2)
        speed = random.uniform(1, 5)
        color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        particle = Particle([x, y], angle, speed, color)
        particles.add(particle)

pygame.QUIT
# 运行程序
running = True
while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 在鼠标点击位置创建粒子
            explode(*pygame.mouse.get_pos())

    # 更新精灵组中的所有粒子
    particles.update()

    # 清空屏幕
    screen.fill((0, 0, 0))

    # 在屏幕上绘制所有粒子
    for particle in particles:
        particle.draw(screen)

    # 更新屏幕
    pygame.display.flip()

    # 控制帧率
    clock.tick(60)

# 退出Pygame
pygame.quit()


2.效果展示

爆炸效果

总结

以上就是一个使用Python的Pygame库实现的简单球形粒子爆炸效果程序。它使用了Pygame的精灵(Sprite)和粒子(Particle)类来创建和管理粒子,使用了随机数生成器来控制粒子的位置、速度和颜色。运行程序后,可以在屏幕上点击鼠标,就会出现球形粒子爆炸效果。每次点击都会生成一些随机颜色、大小、速度和生命值的粒子,它们会在屏幕上飘动一段时间后消失。如果你觉得文章不错,给作者点个大大的赞吧!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃饭团的饭桶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值