import pygame
import random
import math
class Particle:
def __init__(self, x, y):
self.x = x
self.y = y
self.size = random.randint(2, 5)
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.velocity_x = random.uniform(-3, 3)
self.velocity_y = random.uniform(-7, -3)
self.gravity = 0.1
self.life_span = 50
def update(self):
self.velocity_y += self.gravity
self.x += self.velocity_x
self.y += self.velocity_y
self.life_span -= 1
def draw(self, screen):
if self.life_span > 0:
alpha = int((self.life_span / 50) * 255)
surface = pygame.Surface((self.size*2, self.size*2), pygame.SRCALPHA)
pygame.draw.circle(surface, color_with_alpha, (self.size, self.size), self.size)
screen.blit(surface, (int(self.x-self.size), int(self.y-self.size)))
def create_firework(x, y):
particles = []
for _ in range(random.randint(80, 120)):
particles.append(Particle(x, y))
return particles
pygame.init()
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
fireworks = []
running = True
while running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mx, my = pygame.mouse.get_pos()
fireworks.extend(create_firework(mx, my))
for firework in fireworks[:]:
firework.update()
firework.draw(screen)
if firework.life_span <= 0:
fireworks.remove(firework)
pygame.display.flip()
clock.tick(60)
pygame.quit()将这段代码修改让其可以在processing中运行