Skip to content

Instantly share code, notes, and snippets.

@SebastianJarsve
Last active October 12, 2015 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SebastianJarsve/8e6c52930c8bb11fac7c to your computer and use it in GitHub Desktop.
Save SebastianJarsve/8e6c52930c8bb11fac7c to your computer and use it in GitHub Desktop.
Particle_Engine
#-*- 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