pygame 音乐粒子特效

代码

python 复制代码
import pygame
import numpy as np
import pymunk
from pymunk import Vec2d
import random
import librosa
import pydub

# 初始化pygame
pygame.init()

# 创建屏幕
screen = pygame.display.set_mode((1920*2-10, 1080*2-10))
clock = pygame.time.Clock()

# 加载音乐文件
audio_file = '周杰伦-周大侠.flac'
audio = pydub.AudioSegment.from_file(audio_file)
audio = audio.set_channels(1)  # 确保音乐是单声道的
audio.export('temp.wav', format='wav')  # 转换为wav格式,因为pygame不支持mp3

# 播放音乐
pygame.mixer.init()
pygame.mixer.music.load('temp.wav')
pygame.mixer.music.play(-1)  # -1表示无限循环播放

# 转换为波形数组
y, sr = librosa.load('temp.wav')

# 提取节奏
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
beat_times = librosa.frames_to_time(beat_frames, sr=sr)

# 创建pymunk空间
space = pymunk.Space()
space.gravity = (0, 0)  # 设置重力

# 添加边界
border_thickness = 10
border_offset = border_thickness / 2

# 创建边界的四个顶点
bodies = [pymunk.Body(body_type=pymunk.Body.STATIC) for _ in range(4)]
segments = [
    pymunk.Segment(bodies[0], (border_offset, border_offset), (screen.get_width() - border_offset, border_offset), border_thickness),
    pymunk.Segment(bodies[1], (screen.get_width() - border_offset, border_offset), (screen.get_width() - border_offset, screen.get_height() - border_offset), border_thickness),
    pymunk.Segment(bodies[2], (screen.get_width() - border_offset, screen.get_height() - border_offset), (border_offset, screen.get_height() - border_offset), border_thickness),
    pymunk.Segment(bodies[3], (border_offset, screen.get_height() - border_offset), (border_offset, border_offset), border_thickness)
]

# 为边界形状添加到空间中
for body, segment in zip(bodies, segments):
    space.add(body, segment)  # 同时添加刚体和形状到空间中
    segment.elasticity = 1.0
    segment.friction = 0.0


# 粒子类
class Particle:
    def __init__(self, space, position):
        self.body = pymunk.Body(1, float('inf'))
        self.body.position = position
        self.shape = pymunk.Circle(self.body, 10)
        self.shape.elasticity = 1.0
        self.shape.friction = 0.0
        space.add(self.body, self.shape)
        self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    def draw(self, screen):
        pos = int(self.body.position.x), int(self.body.position.y)
        pygame.draw.circle(screen, self.color, pos, int(self.shape.radius))

# 创建粒子
particles = [Particle(space, (random.randint(100, 700), random.randint(100, 500))) for _ in range(1000)]

# 主循环
running = True
frame = 0
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 0, 0))

    # 更新pymunk空间
    space.step(1/60.0)

    # 根据音乐节奏更新粒子速度
    if frame < len(beat_times) and pygame.time.get_ticks() / 1000 > beat_times[frame]:
        for particle in particles:
            impulse = Vec2d(random.uniform(-50, 50), random.uniform(-50, 50))
            particle.body.apply_impulse_at_local_point(impulse)
        frame += 1

    # 绘制粒子
    for particle in particles:
        particle.draw(screen)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

解释

这段代码是一个使用Pygame和Pymunk库创建的音乐可视化效果。它加载一个音乐文件,提取音乐的节奏,并根据节奏在屏幕上创建和更新粒子的位置。以下是代码的详细解释:

  1. 导入所需的库:pygame用于图形和声音,numpy用于数学运算,pymunk用于物理模拟,random用于生成随机数,librosapydub用于处理音乐文件。
  2. 初始化Pygame并创建一个屏幕。
  3. 加载音乐文件,将其转换为单声道,并导出为WAV格式,因为Pygame不支持MP3格式。
  4. 使用pygame.mixer播放音乐。
  5. 使用librosa将音乐文件转换为波形数组,并提取音乐的节奏。
  6. 创建一个Pymunk空间,设置重力为0,并添加边界。
  7. 定义一个Particle类,用于创建和绘制粒子。
  8. 创建一个粒子列表,每个粒子都有一个随机位置。
  9. 主循环:处理事件,更新Pymunk空间,根据音乐节奏更新粒子速度,绘制粒子,并更新屏幕。
  10. 退出Pygame。
    这个代码的主要特点是使用音乐的节奏来控制粒子的运动,创造出一种动态的音乐可视化效果。
相关推荐
nimadan125 分钟前
**手机小说扫榜工具2025推荐,精准追踪榜单动态与题材风向
python·智能手机
编程武士7 分钟前
Python 各版本主要变化速览
开发语言·python
傻啦嘿哟21 分钟前
Python中的@property:优雅控制类成员访问的魔法
前端·数据库·python
sky17201 小时前
VectorStoreRetriever 三种搜索类型
python·langchain
旦莫1 小时前
Python测试开发工具库:日志脱敏工具(敏感信息自动屏蔽)
python·测试开发·自动化·ai测试
唐叔在学习1 小时前
Python自动化指令进阶:UAC提权
后端·python
旺仔小拳头..1 小时前
Java ---变量、常量、类型转换、默认值、重载、标识符、输入输出、访问修饰符、泛型、迭代器
java·开发语言·python
wujj_whut2 小时前
【Conda实战】从0到1:虚拟环境创建、多Python版本管理与环境切换全指南
开发语言·python·conda
geoqiye2 小时前
2026官方认证:贵阳宠物行业短视频运营TOP5评测
大数据·python·宠物
龙腾AI白云2 小时前
AI智能体搭建(3)深度搜索智能体如何搭建与设计 Agent#智能体搭建#多智能体#VLA#大模型
python·django·virtualenv·scikit-learn·tornado