Re: snow!

new topic     » goto parent     » topic index » view thread      » older message » newer message

I like your snow, Greg.  In fact it inspired me briefly to a creative
fervour.  I wondered how the effect might be made more realistic, and
the wind idea sounded good as a start..

I never have much luck hacking existing code so I began anew.  It ended
up becoming more like rain, so I pursued that concept instead.  Behold!
My two cents.

----------------------
include graphics.e
include machine.e

--Declare globals
constant spd = 1, pos = 2, col = 3
object max
sequence rain, pal
integer gust, drops, speed
atom air, wind

--<<-----------==  User parameters ==----------->>--
--number of droplets
drops = 1000
--rain falling speed
speed = 600
--wind speed (reluctance of wind to settle..)
wind = 100000
--strength of gusts
gust = 20
--<<-------------------------------------------->>--

--Delay
--tick_rate(100)
procedure delay(atom x)
  atom t  t = time()
  while time() < t + x do end while
end procedure

--Map background function
function back(sequence pos)
  atom colour
  colour = pos[2] / max[2] * 4 - 3
  colour *= (colour > 0) * 150
  return colour
end function

--Assign random variables to new droplet
function new_drop(integer init)
  atom rate
  rate = rand(speed) / 100
  return {rate, {rand(max[1]), rand(max[2]) * init}, rate / (speed /
100) * 254}
end function

--Set video mode
max = graphics_mode(#13)
max = video_config()
max = max[5..6]

--Make and set greyscale palette
pal = {}
for colour = 1 to 256 do
  pal &= {repeat(colour / 4, 3)}
end for
all_palette(pal)

--Draw background
for x = 0 to max[1] do
  for y = 0 to max[2] do
    pixel(back({x, y}), {x, y})
  end for
end for

--Prepare working variables
air = 0
rain = {}
for drop = 1 to drops do
  rain &= {new_drop(1)}
end for

while get_key() = -1 do
  --delay(.02)                                         --Delay (if
needed..)
  for drop = 1 to drops do                           --Sweep through
drips
    pixel(back(rain[drop][pos]), rain[drop][pos])    --Erase old droplet

    rain[drop][pos] += {air, 1} * rain[drop][spd]    --Move droplet
    rain[drop][pos][1] =                             --Horizontal
wraparound
      remainder(rain[drop][pos][1] + max[1], max[1])
    if rain[drop][col] <= back(rain[drop][pos]) or   --When drop hits
bottom,
       rain[drop][pos][2] > max[2] then
      rain[drop] = new_drop(0)                       -- make it new top
drip
    end if
    pixel(rain[drop][col], rain[drop][pos])          --Draw new drop
    air += ((air < 0) - (air >= 0)) / wind           --Decelerate air
    air += ((gust / 2) - rand(gust) + .5) * .001     --Add random gust
  end for
end while


--
  Nick Metcalfe <metcalfn at alphalink.com.au>
  http://www.alphalink.com.au/~metcalfn

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu