#!/usr/bin/python import pygame import random import math SCRN_SCALE = 2 DISP_SIZE = (320, 240) SCRN_SIZE = (DISP_SIZE[0] * SCRN_SCALE, DISP_SIZE[1] * SCRN_SCALE) def get_rand_col(): #return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) return (random.randint(100, 150), random.randint(150, 200), random.randint(200, 255)) def norm(x, y): h = math.hypot(x, y) if h!=0: return (x/h, y/h, h) return (0.0, 0.0, 1.0) class Particle: def __init__(self): self.x = float(random.randint(0, DISP_SIZE[0])) self.y = float(random.randint(0, DISP_SIZE[1])) self.vx = (random.random() - 0.5) * 10.0 self.vy = (random.random() - 0.5) * 10.0 self.c = get_rand_col() def update(self, td): #mx = mouse_pos[0] / SCRN_SCALE #my = mouse_pos[1] / SCRN_SCALE for mx,my in attractor: v = norm(mx - self.x, my - self.y) if v[2] > 4: self.vx += v[0] * (5000/v[2]**2) * td self.vy += v[1] * (5000/v[2]**2) * td else: h = math.hypot(self.vx, self.vy) self.vx -= v[0] * h self.vy -= v[1] * h self.x = mx - v[0] * 4 self.y = my - v[1] * 4 for mx,my in repulsor: v = norm(mx - self.x, my - self.y) self.vx -= v[0] * (5000/v[2]**2) * td self.vy -= v[1] * (5000/v[2]**2) * td #self.vx -= self.vx * (td * 0.01) #self.vy -= self.vy * (td * 0.01) self.x += self.vx * td self.y += self.vy * td if (self.x < 0) or (self.x >= DISP_SIZE[0]): self.x -= self.vx * td self.vx = -self.vx self.vx -= self.vx * td * 50 if (self.y < 0) or (self.y >= DISP_SIZE[1]): self.y -= self.vy * td self.vy = -self.vy self.vy -= self.vy * td * 50 def draw(self): #pygame.draw.rect(disp, self.c, (int(self.x), int(self.y), 1, 1)) disp.fill(self.c, (int(self.x), int(self.y), 1, 1)) screen = pygame.display.set_mode(SCRN_SIZE, pygame.DOUBLEBUF) disp = pygame.surface.Surface(DISP_SIZE) disp.fill((0,0,0)) fade = pygame.surface.Surface(DISP_SIZE) fade.fill((1,1,1)) particles = [Particle() for i in xrange(10)] attractor = [] repulsor = [] done = False while not done: m = pygame.mouse.get_pos() mouse_pos = (random.gauss(m[0], 10.0), random.gauss(m[1], 10.0)) #disp.fill((10,10,10), None, pygame.BLEND_ADD) #disp.fill((255,255,255)) disp.blit(fade, (0,0), None, pygame.BLEND_SUB) for p in particles: p.update(0.015) p.draw() for a in attractor: pygame.draw.circle(disp, (0, 100, 200), a, 3) for a in repulsor: pygame.draw.circle(disp, (0, 0, 100), a, 3) #pygame.draw.line(disp, (0,0,0), (m[0]/SCRN_SCALE, m[1]/SCRN_SCALE), (mouse_pos[0]/SCRN_SCALE, mouse_pos[1]/SCRN_SCALE)) pygame.transform.scale(disp, SCRN_SIZE, screen) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if event.button==1: attractor.append((int(m[0] / SCRN_SCALE), int(m[1] / SCRN_SCALE))) if event.button==3: repulsor.append((int(m[0] / SCRN_SCALE), int(m[1] / SCRN_SCALE))) if event.button==4: particles.append(Particle()) if event.button==5: if len(particles) > 0: del particles[0] if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: done = True if event.key == pygame.K_SPACE: attractor = [] repulsor = [] if event.type == pygame.QUIT: done = True