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


  include "$/inc/minmidi.inc"
  '
  sys hmo,er
  '
  function SetupMidi()
  ====================
  er=midiOutOpen(hmo, 0, 0, 0, CALLBACK_NULL) '0=synth a 2=soft synth
  midiOutSetVolume(sys hmo,0xffffffff) 'STEREO WORD VALS: LOWER=LEFT UPPER=RIGHT
  'midiOutShortMsg(hmo, 0x0DC0)        '0D Xylophone Bells assign channel 0
  'print hmo "  " er
  end function

  function CloseMidi()
  ====================
  midiOutClose hmo
  end function

  function ding(sys v,n)
  ======================
  'sys v=127
  if v>127 then v=127
  if v<10 then v=10
  n+=30
  'midiOutShortMsg(hmo, v*0x10000+n*0x100+0x89) 'channel 9 percussion off
  'midiOutShortMsg(hmo, 0x4008B9) 'BALANCE
  'midiOutShortMsg(hmo, 0x7007B9) 'MAIN VOLUME
  'BYTE 1: COMMAND : CHANNEL
  'BYTE 2: PITCH
  'BYTE 3: VOLUME
  midiOutShortMsg(hmo, v*0x10000+n*0x100+0x99) 'channel 9 percussion on
  end function

  ==============
  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
    ding 0x50,i
  elseif x<=0
    xv=-xv : x=0
    ding 0x50,i   
  end if
  if y>=yscreen
    yv=-yv : y=yscreen-1
    ding 0x50,i  
  elseif y<=0
    yv=-yv : y=0
    ding 0x50,i 
  end if
  end method

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

  end class 'Particle


  SetupMidi

  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
  CloseMidi
  WinEnd

