-
-
Save SebastianJarsve/8e6c52930c8bb11fac7c to your computer and use it in GitHub Desktop.
Particle_Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- Coding: UTF-8 -*- | |
#Written by Sebastian Jarsve | |
from scene import * | |
from random import * | |
class Particle(object): | |
def __init__(self, wh): | |
self.w = wh.w | |
self.h = wh.h | |
self.x = randint(0, self.w) | |
self.y = randint(0, self.h) | |
self.vx = randint(-10, 20) | |
self.vy = randint(-10, 20) | |
self.colour = Color(random(), random(), random()) | |
def update(self): | |
self.x += self.vx | |
self.y += self.vy | |
self.vx *= 0.98 | |
self.vy *= 0.98 | |
if self.x > self.w: | |
self.x = self.w | |
self.vx *= -1 | |
if self.x < 0: | |
self.x = 0 | |
self.vx *= -1 | |
if self.y > self.h: | |
self.y = self.h | |
self.vy *= -1 | |
if self.y < 0: | |
self.y = 0 | |
self.vy *= -1 | |
def draw(self): | |
fill(*self.colour) | |
rect(self.x, self.y, 8, 8) | |
class MyScene(Scene): | |
def setup(self): | |
self.particles = [] | |
for p in xrange(200): | |
self.particles.append(Particle(self.size)) | |
def draw(self): | |
background(0.00, 0.05, 0.20) | |
for p in self.particles: | |
p.update() | |
p.draw() | |
for t in self.touches.values(): | |
for p in self.particles: | |
tx, ty = t.location.x, t.location.y | |
d = (p.x - tx)*(p.x - tx)+(p.y - ty)*(p.y - ty) | |
d = sqrt(d) | |
p.vx = p.vx - 5/d*(p.x-tx) | |
p.vy = p.vy - 5/d*(p.y-ty) | |
p.colour = Color(random(), random(), random()) | |
run(MyScene()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment