import random
import math
particles = []
fireworks = []
class Particle:
def \__init_\_(self, x, y, color):
self.x = x
self.y = y
self.vx = random.uniform(-3, 3)
self.vy = random.uniform(-3, 3)
self.color = color
self.life = 255
self.max_life = 255
self.size = random.uniform(2, 6)
def update(self):
self.x += self.vx
self.y += self.vy
self.vy += 0.1 # 重力
self.vx \*= 0.98 # 空气阻力
self.life -= 3 # 3秒内淡出
def draw(self):
if self.life > 0:
pushStyle()
fill(red(self.color), green(self.color), blue(self.color), self.life)
noStroke()
ellipse(self.x, self.y, self.size, self.size)
popStyle()
class Firework:
def \__init_\_(self, x, y):
self.x = x
self.y = y
self.start_y = height
self.y_pos = self.start_y
self.exploded = False
self.color = color(random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
def update(self):
if not self.exploded:
self.y_pos -= 8
if self.y_pos <= self.y:
self.explode()
self.exploded = True
def explode(self):
# 创建爆炸粒子
for i in range(50):
angle = random.uniform(0, TWO_PI)
speed = random.uniform(1, 6)
px = self.x + cos(angle) \* speed
py = self.y + sin(angle) \* speed
particles.append(Particle(px, py, self.color))
def draw(self):
if not self.exploded:
pushStyle()
fill(red(self.color), green(self.color), blue(self.color))
noStroke()
ellipse(self.x, self.y_pos, 4, 4)
popStyle()
def setup():
size(800, 600)
background(20, 25, 40)
def draw():
# 绘制夜晚背景
draw_background()
# 更新和绘制烟花
for firework in fireworks\[:\]:
firework.update()
firework.draw()
if firework.exploded and firework.y_pos <= firework.y:
fireworks.remove(firework)
# 更新和绘制粒子
for particle in particles\[:\]:
particle.update()
particle.draw()
if particle.life <= 0:
particles.remove(particle)
# 绘制新年文字
draw_text()
def draw_background():
# 深蓝天色渐变
for i in range(height):
inter = map(i, 0, height, 0, 1)
c = lerpColor(color(20, 25, 40), color(10, 15, 30), inter)
stroke(c)
line(0, i, width, i)
# 绘制星星
fill(255, 255, 200, 150)
noStroke()
for i in range(50):
x = (i \* 137.5 + frameCount \* 0.1) % width
y = (i \* 89) % (height // 2)
size = sin(frameCount \* 0.05 + i) \* 2 + 3
ellipse(x, y, size, size)
# 绘制地面剪影
fill(0, 20, 10)
beginShape()
vertex(0, height)
vertex(0, height - 100)
for x in range(0, width, 50):
y = height - 100 + sin(x \* 0.01) \* 20
vertex(x, y)
vertex(width, height - 100)
vertex(width, height)
endShape(CLOSE)
# 绘制建筑物剪影
fill(0, 15, 8)
rect(100, height - 200, 80, 200)
rect(250, height - 150, 60, 150)
rect(400, height - 180, 70, 180)
rect(550, height - 120, 50, 120)
rect(680, height - 160, 90, 160)
def draw_text():
pushStyle()
textAlign(CENTER, CENTER)
textSize(48)
fill(255, 215, 0) # 金色
text("新年快乐", width/2, 80)
textSize(24)
fill(255, 182, 193) # 粉色
text("2026", width/2, 120)
textSize(16)
fill(200, 200, 255, 200)
text("点击鼠标左键放烟花", width/2, height - 30)
popStyle()
def mousePressed():
if mouseButton == LEFT:
fireworks.append(Firework(mouseX, mouseY))
1月8日 17:24