Author Topic: Populations  (Read 2294 times)

0 Members and 2 Guests are viewing this topic.

Charles Pegge

  • Guest
Populations
« on: May 05, 2012, 09:40:32 PM »
I have created a new folder: projects/Populations/ to for demos handling multiple objects of a similar kind. There are six new programs, evolved from BigBang, using OOP in conjunction with Peter's Windows API.

Here is one of them:

Bounce
Code: OxygenBasic
  1.  
  2. include "..\GDIWindow\window.inc"
  3. Window "Bounce", 640, 480, 2
  4. sys xscreen =640, yscreen =480, i, sz
  5. 'Font 12,24,0,"courier"
  6. sys anzahl=1000
  7.  
  8. single xmitte = xscreen/2, ymitte = yscreen/2
  9.  
  10.  
  11. '=============
  12. class Particle
  13. '=============
  14.  
  15.   single x,y,xv,yv,ra
  16.   sys    sz
  17.   byte   red,green,blue,alpha
  18.   sys    pob
  19.   string sbuf
  20.  
  21. method index(sys i)
  22.   pob=strptr(sbuf)+i*sizeof particle
  23. end method
  24.  
  25. method populate(sys e)
  26.   sys i,b
  27.   sbuf=nuls e*sizeof particle
  28.   pob=strptr sbuf
  29.   for i=0 to <e
  30.     rebirth
  31.     pob+=sizeof particle
  32.   next
  33. end method
  34.  
  35. method rebirth()
  36.   single r,a
  37.   @this = pob
  38.   x     = xmitte
  39.   y     = ymitte
  40.   a     = rand(1,628)*.01
  41.   r     = rand(20,100)*.02
  42.   xv    = r*cos(a)
  43.   yv    = r*sin(a)
  44.   ra    = rand(7,10)
  45.   red   = rand(100,250)
  46.   green = rand(100,250)
  47.   blue  = rand(100,250)
  48. end method
  49.  
  50. method outside() as sys
  51.   @this=pob
  52.   if x<0 or y<0 or x>=xscreen or y>=yscreen then return 1
  53. end method
  54.  
  55. method move()
  56.   @this=pob
  57.   x += xv
  58.   y += yv
  59.   sz=ra*2
  60. end method
  61.  
  62. method bounce()
  63.   @this=pob
  64.   if x>=xscreen
  65.     xv=-xv : x=xscreen-1
  66.   elseif x<=0
  67.     xv=-xv : x=0
  68.   end if
  69.   if y>=yscreen
  70.     yv=-yv : y=yscreen-1
  71.   elseif y<=0
  72.     yv=-yv : y=0
  73.   end if
  74. end method
  75.  
  76. method show()
  77.   @this=pob
  78.   Oval x-ra, y-ra, sz, sz, red, green, blue
  79. end method
  80.  
  81. end class
  82.  
  83.  
  84.  
  85. particle pa
  86. pa.populate anzahl
  87.  
  88. 'PlayWav "cosmobumm.wav",0,1
  89.  
  90.  
  91. While Key(27)=0
  92. ClsColor 80,0,0
  93. for i=0 to <anzahl
  94.   pa.index i
  95.   pa.move
  96.   if pa.outside then
  97.     pa.bounce
  98.   else
  99.     pa.show
  100.   end if
  101. next
  102. '
  103. Events
  104. FlipBuffer
  105. WaitFPS 80
  106. Wend
  107. WinEnd
  108.  

The technique is to create a base particle with a string buffer to hold all other particles. This eliminates the need for arrays, therebye reducing complexity in the code. The population of objects is also well encapsulated and easy to maintain.


Charles

Included in the latest Oxygen in-progress

Peter

  • Guest
Re: Populations
« Reply #1 on: May 06, 2012, 03:27:07 AM »
Hi Charles,

Nice bounce!
Another bounce  with stars here, and an OpenGl version.
Code: [Select]
include "win64.inc"  
Window "Bounce", 640, 480, 2  
sys xscreen =640, yscreen =480, i, sz  
'Font 12,24,0,"courier"  
sys anzahl=1000,ball,z, single vz
ball= LoadBmp("bmp/star",6)
  
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  
   SetBmp(ball, x-ra, y-ra, sin(rad(sz))*50, cos(rad(sz))*40, z)  

end method  
  
end class  
  
particle pa  
pa.populate anzahl  
  
  
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  
vz = vz +.2
if vz >=1
vz=0  
z=z+1
if z=6 then z=0
end if
WaitFor 80  
Wend  
WinEnd