fireworks.bmx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ' Global variables tracking number of Firework objects and
  2. ' number of Particle objects. These are increased and decreased
  3. ' as objects are created/destroyed.
  4. Global Fireworks, Particles
  5. ' Force pulling particles down...
  6. Global Gravity# = 0.025
  7. ' Global list of Particle objects...
  8. Global ParticleList:TList = New TList
  9. ' Particle object definition ('class')...
  10. Type Particle
  11. ' Particle properties...
  12. Field x# ' x position
  13. Field y# ' y position
  14. Field xs# ' x speed
  15. Field ys# ' y speed
  16. Field size ' particle size (size x size)
  17. Field r# ' particle colour (red component)
  18. Field g# ' particle colour (green component)
  19. Field b# ' particle colour (blue component)
  20. Field ditch
  21. ' Particle actions...
  22. ' The function below is a 'constructor'. Methods work on
  23. ' existing objects, so we can't use a method to create an
  24. ' object. Instead, we use a function belonging to this object
  25. ' type, and call it like so to return a Particle object:
  26. ' p:Particle = Particle.Create (blah blah)...
  27. Function Create:Particle (x#, y#, xs#, ys#, size, r#, g#, b#)
  28. Local p:Particle = New Particle
  29. p.x = x
  30. p.y = y
  31. p.xs = xs
  32. p.ys = ys
  33. p.size = size
  34. p.r = r
  35. p.g = g
  36. p.b = b
  37. ParticleList.Addlast p
  38. Particles = Particles + 1
  39. Return p
  40. End Function
  41. ' This function updates all particles by iterating through the
  42. ' global Particle list (ParticleList) and calling the Update
  43. ' method on each one...
  44. Function UpdateAll ()
  45. For p:Particle = EachIn ParticleList
  46. p.Update
  47. Next
  48. End Function
  49. ' Updates current particle...
  50. Method Update ()
  51. ApplyForces
  52. Draw
  53. End Method
  54. ' Apply x and y speeds, apply gravity and apply position limits...
  55. Method ApplyForces ()
  56. x = x + xs
  57. ys = ys + Gravity * size
  58. y = y + ys
  59. LimitParticle
  60. End Method
  61. ' Draws the particle. This has been kept separate so it can be
  62. ' 'over-ridden' in the Firework type below...
  63. Method Draw ()
  64. SetColor r, g, b
  65. DrawRect x, y, size, size
  66. End Method
  67. ' Apply limits (if the particle goes off the left or right of
  68. ' the screen, we reverse its direction, and if it goes off the
  69. ' bottom (as it must, since gravity is pulling it down), we
  70. ' remove the particle from the global list. We're also fading
  71. ' the particle to black by reducing r, g and b; once they all
  72. ' reach zero, we remove it from the list.
  73. Method LimitParticle ()
  74. If x < 0 Or x + size > GraphicsWidth ()
  75. xs = -xs
  76. x = x + xs
  77. EndIf
  78. If y + size > GraphicsHeight ()
  79. ParticleList.Remove Self
  80. Particles = Particles - 1
  81. Else
  82. r = r - 2; If r < 0 Then r = 0
  83. g = g - 2; If g < 0 Then g = 0
  84. b = b - 2; If b < 0 Then b = 0
  85. If r + b + g = 0 ParticleList.Remove Self; Particles = Particles - 1
  86. EndIf
  87. End Method
  88. End Type
  89. ' This object definition takes the 'Particle' definition and 'extends' it,
  90. ' meaning that it has all of the same fields and methods/functions as the
  91. ' Particle type, but you can add new fields and 'over-ride' methods by
  92. ' simply redefining them...
  93. Type Firework Extends Particle
  94. ' Here, I've over-ridden the Create function to return a
  95. ' Firework type. Note that the parameters must be the same
  96. ' as for Particle.Create and that it is added to the global
  97. ' list of Particle objects; this is possible because Firework
  98. ' objects are still Particle objects, just more souped-up!
  99. Function Create:Firework (x#, y#, xs#, ys#, size, r#, g#, b#)
  100. Local p:Firework = New Firework
  101. p.x = x
  102. p.y = y
  103. p.xs = xs
  104. p.ys = ys
  105. p.size = size
  106. p.r = r
  107. p.g = g
  108. p.b = b
  109. ParticleList.Addlast p
  110. Fireworks = Fireworks + 1
  111. Return p
  112. End Function
  113. ' Here I've over-ridden the Update method so that when a
  114. ' Firework starts to fall (ys > 0.5) it's deleted and spawns
  115. ' a random number of normal Particle objects. Note the use of
  116. ' the ApplyForces and Draw methods that are 'inherited' from
  117. ' the Particle definition (as are the fields such as x, y,
  118. ' xs, ys, etc)...
  119. Method Update ()
  120. If ys > 0.5
  121. ParticleList.Remove Self
  122. Fireworks = Fireworks - 1
  123. For p = 1 To Rand (100,1000)'50, 150)
  124. Particle.Create (x, y, Rnd (-4, 4), Rnd (0, -4), Rnd (1, 2), Rand (120, 255), Rand (120, 255), Rand (120, 255))
  125. Next
  126. Else
  127. ApplyForces
  128. Draw
  129. EndIf
  130. End Method
  131. ' This version of LimitParticle over-rides that defined in the
  132. ' plain Particle type. It's interesting to note that although
  133. ' the Update method above calls the original ApplyForces method
  134. ' defined in the Particle type, that actually calls this over-ridden
  135. ' version of LimitParticle.
  136. Method LimitParticle ()
  137. If x < 0 Or x + size > GraphicsWidth ()
  138. xs = -xs
  139. x = x + xs
  140. EndIf
  141. End Method
  142. End Type
  143. ' D E M O . . .
  144. Graphics 640, 480
  145. SetClsColor 1, 1, 10
  146. astep# = 2 ' Used for the positioning of the spawn point, x, below...
  147. Repeat
  148. Cls
  149. ' This is plotting a circle but only using the x position...
  150. ang# = ang + astep; If ang > (360 - astep) Then ang = 0
  151. x# = (GraphicsWidth () / 2) + (GraphicsWidth () / 2) * Sin (ang)
  152. ' No timers in Blitz Max yet!
  153. If Rand (0, 1000) > 800
  154. Firework.Create (x, GraphicsHeight (), Rnd (-1, 1), Rnd (-4, -12), 4, 255, 255, 255)
  155. EndIf
  156. ' Update all particle (both Particle and Firework objects from
  157. ' the global list)...
  158. Particle.UpdateAll ()
  159. SetColor 255, 255, 255
  160. DrawText "Fireworks: " + Fireworks, 20, 20
  161. DrawText "Particles: " + Particles, 20, 40
  162. Flip
  163. Until KeyHit(KEY_ESCAPE)
  164. End