from random import randint
class flake:
def __init__ (self,x,y):
self.vec = PVector(x,y)
self.z = random(0,3)
self.velY = map(self.z,0,3,0.5,1)
self.velX = 0
def show(self):
ellipse(self.vec.x,self.vec.y,map(self.z,0,3,2,5),map(self.z,0,3,2,5))
def update(self):
self.vec.y += self.velY
self.vec.x += self.velX
self.velY += 0.01
if self.velX > 0:
self.velX -= 0.01
elif self.velX < 0:
self.velX += 0.01
if self.vec.y > height:
self.vec.x = randint(0,width)
self.vec.y = randint(-500,-50)
self.velY = map(self.z,0,3,0.5,1.5)
if self.vec.x < 0:
self.vec.x = randint(350,width)
if self.vec.x > width:
self.vec.x = randint(0,100)
width = 700
height = 500
snow = []
total = 1001 #num of snowflakes
for i in range(0,total):
snow.append(flake(randint(0,width),randint(-500,50)))
def setup():
size(width,height)
noStroke()
fill(178,32,201)
def draw():
background(150)
for i in range(0,total):
snow[i].show()
snow[i].update()
if keyPressed:
if key == "w":
for i in range(0,total):
a = map(snow[i].z,0,3,0.01,0.03)
snow[i].velY -= a
if key == "s":
for i in range(0,total):
a = map(snow[i].z,0,3,0.01,0.03)
snow[i].velY += a
if key == "d":
for i in range(0,total):
a = map(snow[i].z,0,3,0.01,0.03)
snow[i].velX += a
if key == "a":
for i in range(0,total):
a = map(snow[i].z,0,3,0.01,0.03)
snow[i].velX -= a
if key == "r":
del snow [:]
for i in range(0,total):
snow.append(flake(randint(0,width),randint(-500,50)))
(I know that with rain I have less gravity acceleration limitations, but in my city snowed )
Use wasd to control snow, r to reset
(If I add more i lag )
What can I improve?