import pygame
import numpy as np
import math
import random
import colorsys
import sys
from pygame.locals import *
# 初始化pygame和音频
pygame.init()
pygame.mixer.init()
# 设置窗口
WIDTH, HEIGHT = 1400, 900
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.SRCALPHA)
pygame.display.set_caption("⚡ 量子矩阵 | 3D粒子音频可视化 ⚡")
# 创建字体
font_large = pygame.font.Font(None, 72)
font_medium = pygame.font.Font(None, 36)
font_small = pygame.font.Font(None, 24)
# 创建半透明表面
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
matrix_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
# 生成随机矩阵字符
def generate_matrix_chars():
chars = "アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
return [random.choice(chars) for _ in range(200)]
matrix_chars = generate_matrix_chars()
# 3D粒子类
class Particle3D:
def __init__(self):
self.x = random.uniform(-2, 2)
self.y = random.uniform(-2, 2)
self.z = random.uniform(-2, 2)
self.vx = random.uniform(-0.01, 0.01)
self.vy = random.uniform(-0.01, 0.01)
self.vz = random.uniform(-0.01, 0.01)
self.color = self.get_random_color()
self.size = random.uniform(1, 3)
self.trail = []
self.max_trail = 15
self.type = random.choice(["sphere", "cube", "pyramid"])
def get_random_color(self):
hue = random.random()
r, g, b = colorsys.hsv_to_rgb(hue, 0.9, 1.0)
return (int(r*255), int(g*255), int(b*255))
def update(self, time, audio_energy=0):
# 添加一些随机的运动
self.x += self.vx + math.sin(time*0.001 + self.x) * 0.01
self.y += self.vy + math.cos(time*0.001 + self.y) * 0.01
self.z += self.vz + math.sin(time*0.001 + self.z*0.5) * 0.01
# 音频能量影响
self.x += math.sin(time*0.002) * audio_energy * 0.05
self.y += math.cos(time*0.002) * audio_energy * 0.05
# 边界检查
if abs(self.x) > 2:
self.vx *= -1
if abs(self.y) > 2:
self.vy *= -1
if abs(self.z) > 2:
self.vz *= -1
# 更新轨迹
self.trail.append((self.x, self.y, self.z))
if len(self.trail) > self.max_trail:
self.trail.pop(0)
def project_3d_to_2d(self, x, y, z, time):
# 3D到2D投影,添加旋转效果
angle_x = time * 0.0005
angle_y = time * 0.0003
angle_z = time * 0.0004
# 旋转矩阵
cos_x, sin_x = math.cos(angle_x), math.sin(angle_x)
cos_y, sin_y = math.cos(angle_y), math.sin(angle_y)
cos_z, sin_z = math.cos(angle_z), math.sin(angle_z)
# 应用旋转
y_rot = y * cos_x - z * sin_x
z_rot = y * sin_x + z * cos_x
x_rot = x * cos_y + z_rot * sin_y
z_final = -x * sin_y + z_rot * cos_y
x_final = x_rot * cos_z - y_rot * sin_z
y_final = x_rot import pygame
import math
import random
import colorsys
import sys
# 初始化pygame
pygame.init()
# 设置窗口尺寸
WIDTH, HEIGHT = 1200, 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("炫酷螺旋粒子场 - Python动态可视化")
# 设置字体用于显示标题
try:
font = pygame.font.Font(None, 48)
small_font = pygame.font.Font(None, 28)
except:
font = pygame.font.SysFont('arial', 48)
small_font = pygame.font.SysFont('arial', 28)
# 粒子类
class Particle:
def __init__(self, x, y):
self.x = x
self.y = y
self.base_x = x
self.base_y = y
self.size = random.uniform(2, 5)
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.speed = random.uniform(0.02, 0.08)
self.angle = random.uniform(0, 2 * math.pi)
self.radius = random.uniform(20, 150)
self.time_offset = random.uniform(0, 10)
self.trail = []
self.max_trail = 10
def update(self, time, mouse_x, mouse_y, effect_strength):
# 计算粒子在螺旋中的位置
self.angle += self.speed
spiral_factor = 0.1
spiral_x = self.radius * math.cos(self.angle + self.time_offset) * (1 + spiral_factor * self.angle)
spiral_y = self.radius * math.sin(self.angle + self.time_offset) * (1 + spiral_factor * self.angle)
# 添加鼠标交互效果
dx = mouse_x - self.base_x
dy = mouse_y - self.base_y
dist = math.sqrt(dx*dx + dy*dy)
if dist < 200:
force = (200 - dist) / 200
spiral_x += dx * force * 0.1 * effect_strength
spiral_y += dy * force * 0.1 * effect_strength
# 更新粒子位置
self.x = self.base_x + spiral_x
self.y = self.base_y + spiral_y
# 更新轨迹
self.trail.append((self.x, self.y))
if len(self.trail) > self.max_trail:
self.trail.pop(0)
# 动态颜色变化
hue = (time * 0.001 + self.time_offset * 0.1) % 1.0
r, g, b = colorsys.hsv_to_rgb(hue, 0.8, 1.0)
self.color = (int(
点赞 (0)
回复