|
| 1 | +import pygame |
| 2 | +import random |
| 3 | +import math |
| 4 | +import pmma |
| 5 | + |
| 6 | +# Initialize Pygame |
| 7 | +pygame.init() |
| 8 | +pmma.init() |
| 9 | + |
| 10 | +# Screen dimensions |
| 11 | +WIDTH, HEIGHT = 1920, 1080 |
| 12 | +screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN) |
| 13 | +pygame.display.set_caption("Particle System") |
| 14 | + |
| 15 | +# Colors |
| 16 | +BLACK = (0, 0, 0) |
| 17 | +WHITE = (255, 255, 255) |
| 18 | + |
| 19 | +class Wipe: |
| 20 | + def __init__(self): |
| 21 | + self.position = [WIDTH / 2, HEIGHT / 2] |
| 22 | + self.radius = 0 |
| 23 | + self.do_wipe = False |
| 24 | + |
| 25 | + def draw(self): |
| 26 | + if self.do_wipe: |
| 27 | + pygame.draw.circle(screen, BLACK, (int(self.position[0]), int(self.position[1])), int(self.radius)) |
| 28 | + pygame.draw.circle(screen, WHITE, (int(self.position[0]), int(self.position[1])), int(self.radius), width=10) |
| 29 | + d = 60 - clock.get_fps() |
| 30 | + if d > 2: |
| 31 | + self.radius += d*1.5 |
| 32 | + else: |
| 33 | + self.radius += 2 |
| 34 | + |
| 35 | + if self.radius > 2250: |
| 36 | + self.do_wipe = False |
| 37 | + self.radius = 0 |
| 38 | + |
| 39 | +# Particle class |
| 40 | +class Particle: |
| 41 | + def __init__(self, x, y, size=1, vx=0, vy=0): |
| 42 | + self.x = x |
| 43 | + self.y = y |
| 44 | + self.size = size |
| 45 | + self.vx = vx |
| 46 | + self.vy = vy |
| 47 | + self.mass = size # Mass is proportional to size |
| 48 | + color = pmma.ColorConverter() |
| 49 | + self.color = color.generate_random_color(format=pmma.Constants.RGB) |
| 50 | + self.explosion_size = random.randint(100, 250) |
| 51 | + |
| 52 | + def move(self): |
| 53 | + self.x += self.vx |
| 54 | + self.y += self.vy |
| 55 | + |
| 56 | + # Bounce off the edges of the screen |
| 57 | + if self.x - self.size < 0 or self.x + self.size > WIDTH: |
| 58 | + self.vx = -self.vx |
| 59 | + if self.y - self.size < 0 or self.y + self.size > HEIGHT: |
| 60 | + self.vy = -self.vy |
| 61 | + |
| 62 | + def draw(self, screen): |
| 63 | + pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size) |
| 64 | + |
| 65 | + def attract(self, other, G=0.5): |
| 66 | + # Calculate distance and direction |
| 67 | + dx = other.x - self.x |
| 68 | + dy = other.y - self.y |
| 69 | + distance = math.sqrt(dx**2 + dy**2) |
| 70 | + |
| 71 | + if distance > 0 and distance < 1920: # Limit the interaction range |
| 72 | + force = G * (self.mass * other.mass) / distance**2 |
| 73 | + fx = force * (dx / distance) |
| 74 | + fy = force * (dy / distance) |
| 75 | + |
| 76 | + # Apply force to velocities |
| 77 | + self.vx += fx / self.mass |
| 78 | + self.vy += fy / self.mass |
| 79 | + other.vx -= fx / other.mass |
| 80 | + other.vy -= fy / other.mass |
| 81 | + |
| 82 | + def combine(self, other): |
| 83 | + # Check for collision (simple circle overlap) |
| 84 | + dx = other.x - self.x |
| 85 | + dy = other.y - self.y |
| 86 | + distance = math.sqrt(dx**2 + dy**2) |
| 87 | + |
| 88 | + if distance < self.size + other.size: |
| 89 | + # Combine particles |
| 90 | + total_mass = self.mass + other.mass |
| 91 | + self.vx = (self.vx * self.mass + other.vx * other.mass) / total_mass |
| 92 | + self.vy = (self.vy * self.mass + other.vy * other.mass) / total_mass |
| 93 | + self.size = int(math.sqrt(self.size**2 + other.size**2)) |
| 94 | + self.mass = total_mass |
| 95 | + red = self.color[0] + other.color[0] |
| 96 | + green = self.color[1] + other.color[1] |
| 97 | + blue = self.color[2] + other.color[2] |
| 98 | + self.color = (red/2, green/2, blue/2) |
| 99 | + return True |
| 100 | + return False |
| 101 | + |
| 102 | +# Initialize particles |
| 103 | +particles = [ |
| 104 | + Particle(random.randint(50, WIDTH - 50), random.randint(50, HEIGHT - 50), |
| 105 | + size=random.randint(1, 5), |
| 106 | + vx=random.uniform(-1, 1), vy=random.uniform(-1, 1)) |
| 107 | + for _ in range(250) |
| 108 | +] |
| 109 | + |
| 110 | +# Main loop |
| 111 | +running = True |
| 112 | +clock = pygame.time.Clock() |
| 113 | + |
| 114 | +surface = pygame.Surface((WIDTH, HEIGHT)) |
| 115 | +surface.set_alpha(250) |
| 116 | +wiper = Wipe() |
| 117 | +while running: |
| 118 | + surface.blit(screen, (0, 0)) |
| 119 | + screen.fill(BLACK) |
| 120 | + screen.blit(surface, (0, 0)) |
| 121 | + |
| 122 | + for event in pygame.event.get(): |
| 123 | + if event.type == pygame.QUIT: |
| 124 | + running = False |
| 125 | + |
| 126 | + # Update particles |
| 127 | + for i, p1 in enumerate(particles): |
| 128 | + for p2 in particles[i + 1:]: |
| 129 | + p1.attract(p2) |
| 130 | + if p1.combine(p2): |
| 131 | + particles.remove(p2) |
| 132 | + p1.move() |
| 133 | + p1.draw(screen) |
| 134 | + |
| 135 | + if p1.mass > 100: |
| 136 | + particles.remove(p1) |
| 137 | + # Explosion logic |
| 138 | + num_new_particles = int(p1.explosion_size / 3) # Number of smaller particles |
| 139 | + for _ in range(num_new_particles): |
| 140 | + angle = random.uniform(0, 2 * math.pi) # Random angle for direction |
| 141 | + speed = random.uniform(1, 3) # Speed of the smaller particles |
| 142 | + new_vx = math.cos(angle) * speed |
| 143 | + new_vy = math.sin(angle) * speed |
| 144 | + particles.append(Particle(p1.x + 20 * math.cos(angle), p1.y + 20 * math.sin(angle), size=random.randint(1, 5), vx=new_vx, vy=new_vy)) |
| 145 | + |
| 146 | + if ((p1.x - WIDTH/2)**2 + (p1.y - HEIGHT/2)**2)**0.5 < wiper.radius and wiper.do_wipe: |
| 147 | + try: |
| 148 | + particles.remove(p1) |
| 149 | + except: |
| 150 | + pass |
| 151 | + |
| 152 | + while len(particles) < 75 and wiper.do_wipe is False: |
| 153 | + particles.append( |
| 154 | + Particle(random.randint(50, WIDTH - 50), random.randint(50, HEIGHT - 50), |
| 155 | + size=random.randint(1, 5), |
| 156 | + vx=random.uniform(-1, 1), vy=random.uniform(-1, 1)) |
| 157 | + ) |
| 158 | + |
| 159 | + wiper.draw() |
| 160 | + |
| 161 | + pygame.display.flip() |
| 162 | + clock.tick(60) |
| 163 | + if clock.get_fps() < 10: |
| 164 | + wiper.do_wipe = True |
| 165 | + |
| 166 | +pygame.quit() |
0 commit comments