A simple particle system.
from ion import * from time import * from random import * from kandinsky import * class Clock: def __init__(self): self.ltick=monotonic() def tick(self, target): ltick=self.ltick sleep((1/target)-(monotonic()-ltick)) self.ltick=monotonic() return 1/(monotonic()-ltick) size=2 #particle size vx=0 #particle velocity along x vy=-2 #particle velocity along y x=100 #starting position along x y=100 #starting position along y disp=7 #dispersion around the starting position rand=1 #randomization of movement max_state=40 #number of steps nb=6 #number of particles generated at each step sensi=3 c=Clock() particles=[] while True: if True: if keydown(KEY_UP): y-=sensi elif keydown(KEY_DOWN): y+=sensi if keydown(KEY_LEFT): x-=sensi elif keydown(KEY_RIGHT): x+=sensi i=[] for _ in range(nb): particles+=[( x+randint(-disp,disp), y+randint(-disp,disp), 0 )] for p in range(len(particles)): xi,yi,s=particles[p] s+=1 xi+=vx+randint(-rand,rand) yi+=vy+randint(-rand,rand) if s<max_state: i+=[(xi,yi,s)] particles=i fill_rect(0,0,330,230,(255,255,255)) fill_rect(x-1,y+1,2,12,(0,0,0)) fill_rect(x-6,y+9,12,50,(220,)*3) fill_rect(x-2,y-0,4,8,(255,150,0)) for p in particles: fill_rect(int(p[0]),int(p[1]),size,size,(255-p[2]/max_state*200,)*3) c.tick(30) #target FPS