
-----------------------------------
Lapsus Antepedis
Tue Oct 25, 2005 1:41 pm

Beginnings of a spray thingy... Now with fading colour!!
-----------------------------------
I got bored again, and this came out during my spare time in french class...  I'm posting it so I can get it home and try to make it into a module or something so I can have more than one running at once...

Anyway, here's my code. Let me know if you have any thoughts about it...
(I also plan to add inter-particle and object collision...)

Edit 1: Added code so origin of particles follows mouse and syntax tags...
Edit 1.5: Switched bace to code tags... (syntax does not mean size 24!!)
Edit 2: Decreased delay for smoother simulation
Edit 3: Added colour fading to indicate life of particles

type thingy : % I couldn't think of a better name...
    record
        x, y, clr, siz, life : int
        xs, ys : real
    end record

var window : int := Window.Open ("graphics:max;max, offscreenonly") %open a psudeo-fullscreen window

const grav : real := -0.1  %Force pulling the particles in the y direction...
const damp : real := 0.005 %Force pulling velocities toward zero
const minlife : int := 100 %Minimum lifespan of particles
const maxlife : int := 300 %Maximum lifespan of particles
const numball : int := 300 %Number of particles
const size : int := 5      %Size of particles
const del : int := 10       %Delay in main loop
const bounce : real := .99 %Percent of velocity kept on bounce
var ox : int := 100      %x origin of particles
var oy : int := 100      %y origin of particles
var blah : int

var ball : array 1 .. numball of thingy %Array full of particles

loop
    if maxcolour < numball + 1 then
        blah := RGB.AddColour (0, 0, 0)
    else
        exit
    end if
end loop

proc reset (n, x, y : int) %Procedure to reset specific particle to a place on screen after death
    ball (n).x := x
    ball (n).y := y
    ball (n).clr := n
    ball (n).siz := size
    ball (n).life := Rand.Int (minlife, maxlife) % Random lifespan
    ball (n).xs := Rand.Int (1000, 6000) / 1000 %My randreal thing...
    ball (n).ys := Rand.Int (1000, 6000) / 1000 %Same as above
end reset

proc colourball (i : int)
    RGB.SetColour (ball (i).clr, ball (i).life / maxlife, ball (i).life / maxlife, ball (i).life / maxlife)
end colourball

proc draw % Procedure to draw particles
    for i : 1 .. numball
        colourball (i)
        drawfilloval (ball (i).x, ball (i).y, ball (i).siz, ball (i).siz, ball (i).clr)
    end for
end draw

proc main % Main procedure
    for i : 1 .. numball
        ball (i).x += round (ball (i).xs) %Move particles
        ball (i).y += round (ball (i).ys)

        ball (i).ys += grav %Gravity

        if ball (i).xs > 0 then  %x damping
            ball (i).xs -= damp
        elsif ball (i).xs < 0 then
            ball (i).xs += damp
        end if

        if ball (i).xs > 0 then  %y damping
            ball (i).xs -= damp
        elsif ball (i).xs < 0 then
            ball (i).xs += damp
        end if

        if ball (i).y < 0 then  %Bounce off top and bottom of screen
            ball (i).ys *= -bounce
            ball (i).y := 0
        elsif ball (i).y > maxy then
            ball (i).ys *= -bounce
            ball (i).y := maxy
        end if

        if ball (i).x > maxx or ball (i).x < 0 or ball (i).life 