include "../GDIWindow/window.inc"
Window "Bounce", 640, 480, 2
sys xscreen =640, yscreen =480, i, sz
'Font 12,24,0,"courier"
sys anzahl=1000

single xmitte = xscreen/2, ymitte = yscreen/2


'=============
class Particle
'=============
  
  single x,y,xv,yv,ra
  sys    sz
  byte   red,green,blue,alpha
  sys    pob
  string sbuf

method index(sys i)
  pob=strptr(sbuf)+i*sizeof particle
end method

method populate(sys e)
  sys i,b
  sbuf=nuls e*sizeof particle
  pob=strptr sbuf
  for i=0 to <e
    rebirth
    pob+=sizeof particle
  next
end method

method rebirth()
  single r,a
  @this = pob
  x     = xmitte
  y     = ymitte
  a     = rand(1,628)*.01
  r     = rand(20,100)*.02
  xv    = r*cos(a)
  yv    = r*sin(a)
  ra    = rand(7,10)
  red   = rand(100,250)
  green = rand(100,250)
  blue  = rand(100,250)
end method

method outside() as sys
  @this=pob
  if x<0 or y<0 or x>=xscreen or y>=yscreen then return 1
end method

method move()
  @this=pob
  x += xv
  y += yv
  sz=ra*2
end method

method bounce()
  @this=pob
  if x>=xscreen
    xv=-xv : x=xscreen-1
  elseif x<=0
    xv=-xv : x=0
  end if
  if y>=yscreen
    yv=-yv : y=yscreen-1
  elseif y<=0
    yv=-yv : y=0
  end if
end method

method show()
  @this=pob
  Oval x-ra, y-ra, sz, sz, red, green, blue
end method

end class



particle pa
pa.populate anzahl

'PlayWav "cosmobumm.wav",0,1


While Key(27)=0
ClsColor 80,0,0
for i=0 to <anzahl
  pa.index i
  pa.move
  if pa.outside then
    pa.bounce
  else
    pa.show
  end if
next
'
Events
FlipBuffer
WaitFPS 80
Wend
WinEnd

