throwrockets.bmx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Const PARTICLE_GRAVITY# = 0.05
  2. Global ParticleCounter
  3. Global ParticleList:TList = New TList
  4. ' Core abstract type...
  5. Type Atom
  6. Field image
  7. Field x#
  8. Field y#
  9. Field xs#
  10. Field ys#
  11. Field ALPHA# = 1
  12. Field size#
  13. Method Update () Abstract
  14. End Type
  15. ' Generic particle type that holds creation/update functions, based
  16. ' on abstract type Atom...
  17. Type Particle Extends Atom
  18. Function Create:Particle (image, x#, y#, xs#, ys#)
  19. p:Rocket = New Rocket
  20. p.image = image
  21. p.x = x
  22. p.y = y
  23. p.xs = xs
  24. p.ys = ys
  25. ParticleList.AddLast p
  26. ParticleCounter = ParticleCounter + 1
  27. Return p
  28. End Function
  29. Function UpdateAll ()
  30. Local p:Atom
  31. For p=EachIn ParticleList
  32. p.Update ()
  33. Next
  34. End Function
  35. End Type
  36. ' Types based on Particle, all to be created using EXTENDED_TYPE.Create ()...
  37. Type Rocket Extends Particle
  38. Method Update ()
  39. If ALPHA > 0.01
  40. ALPHA = ALPHA - 0.005
  41. SetAlpha ALPHA
  42. ys = ys + PARTICLE_GRAVITY
  43. x = x + xs
  44. y = y + ys
  45. ang# = ATan2 (xs, -ys)
  46. SetRotation ang
  47. DrawImage image, x, y
  48. If x < 0 Or x > GraphicsWidth () Or y > GraphicsHeight ()
  49. ParticleList.Remove Self
  50. ParticleCounter = ParticleCounter - 1
  51. EndIf
  52. Else
  53. ParticleList.Remove Self
  54. ParticleCounter = ParticleCounter - 1
  55. EndIf
  56. End Method
  57. End Type
  58. ' --------------------------------------------------------------------------------
  59. Incbin "gfx/boing.png"
  60. Const GAME_WIDTH = 640
  61. Const GAME_HEIGHT = 480
  62. Const GRAPHICS_WIDTH = 1024
  63. Const GRAPHICS_HEIGHT = 768
  64. Graphics GRAPHICS_WIDTH,GRAPHICS_HEIGHT,32
  65. SetVirtualResolution GAME_WIDTH,GAME_HEIGHT
  66. SetClsColor 64, 96, 180
  67. SetMaskColor 255, 0, 255
  68. AutoImageFlags MASKEDIMAGE ' Disable for filtered rockets...
  69. image = LoadImage ("incbin::gfx/boing.png")
  70. MidHandleImage image
  71. lastmousex = VirtualMouseX ()
  72. lastmousey = VirtualMouseY ()
  73. Repeat
  74. x = VirtualMouseX ()
  75. y = VirtualMouseY ()
  76. mxs# = VirtualMouseXSpeed ()'# = x - lastmousex
  77. mys# = VirtualMouseYSpeed ()'# = y - lastmousey
  78. Cls
  79. xs# = mxs / 10 + Rnd (-0.1, 0.1)
  80. ys# = mys / 10 + Rnd (-0.1, 0.1)
  81. If MouseDown (1) And (mxs Or mys)
  82. Rocket.Create (image, x, y, xs, ys)
  83. EndIf
  84. SetScale 0.2, 0.2
  85. SetBlend ALPHABLEND
  86. Particle.UpdateAll ()
  87. SetScale 1, 1
  88. SetRotation 0
  89. DrawText "Click and drag mouse to throw rockets!", 10, 10
  90. DrawText "Rockets: " + ParticleCounter, 10, 25
  91. Flip
  92. lastmousex = x
  93. lastmousey = y
  94. Until KeyHit (KEY_ESCAPE)
  95. End