Advertisement
Guest User

Codea/codify side effect snow

a guest
Nov 21st, 2011
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. --snowflakes
  2. function setup()
  3.     --put anywhere in setup
  4.     flakes = {}
  5.     for i = 1, 20 do
  6.         flakes[i] = {cx=math.random(WIDTH),
  7.                     cy=math.random(HEIGHT),
  8.                     l=math.random(3,30),
  9.                     r=math.random(60),
  10.                     w=math.random(20)+3}
  11.     end
  12. end
  13.  
  14. function draw()
  15.     --this section for demo only
  16.     sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2)
  17.     sprite("Small World:Church",100,100)
  18.     sprite("Small World:Court",200,300)
  19.     sprite("Small World:Explosion",300,500)
  20.     sprite("Small World:Tower",400,150)
  21.     --place at end of draw loop   
  22.     stroke(255, 255, 255, 26)
  23.     for k, v in pairs(flakes) do
  24.         strokeWidth(flakes[k].w)
  25.         draw_flake(flakes[k])
  26.         flakes[k].cy = flakes[k].cy - math.random()
  27.         --borrowed from bit invader
  28.         sideMove = vec2( math.sin(flakes[k].cy * 0.02), 0 )
  29.         flakes[k].cx = flakes[k].cx + sideMove.x
  30.         flakes[k].r = flakes[k].r + math.random()
  31.         if (flakes[k].cy + flakes[k].l) < 0 then
  32.             flakes[k].l = math.random(3,30)
  33.             flakes[k].cy = HEIGHT + flakes[k].l
  34.             flakes[k].cx = math.random(WIDTH)
  35.         end    
  36.     end
  37. end
  38.  
  39. --required
  40. function draw_flake(fl)
  41.     local f = vec2(0,fl.l)
  42.     f = f:rotate(math.rad(fl.r))
  43.     for i = 1, 6 do
  44.         line(fl.cx,fl.cy,fl.cx+f.x,fl.cy+f.y)
  45.         f = f:rotate(math.rad(60))
  46.     end
  47. end
  48.  
  49. --alternatives: convert this to a class, isolate with pushStyle and pushMatrix
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement