Page 1 of 1

Fireworks animation

Posted: Sun Feb 26, 2006 6:34 am
by theteapot
Code updated For 5.20+

I wrote this while looking for a fireworks simulator thing. It can be modified very easily to let the user place the fireworks wherever they want.

I thought that it was good enough to post on the forum.

TheTeapot

Code: Select all

;;;;;;;;;TheTeapot's FireWorks Thingy;;;;;;;;;;;;;;;;
Structure shl
 x.l
 y.l
 vY.l
 str.l
 clr.l
 stp.l
EndStructure
Structure dot
 x.f
 y.f
 angle.l
 speed.f
 g.f
 clr.l
EndStructure

NewList Shell.shl()
NewList Proj.dot()

InitSprite()
InitKeyboard()
InitMouse()

If OpenScreen(1024, 768, 32, "FireWorks") = 0
 MessageRequester("FireWorks", "Direct X not initialised.")
EndIf

ChangeGamma(0, 0, 0)

SetFrameRate(24)
Repeat
 ExamineKeyboard()
 ExamineMouse()
 ClearScreen(RGB(0, 0, 0))

 If Bias = 1        ;  Red
  R = Random(128) + 128
  G = Random(128)
  B = Random(128)
 ElseIf Bias = 2   ;  Green
  R = Random(128)
  G = Random(128) + 128
  B = Random(128)
 ElseIf Bias = 3   ;  Blue
  R = Random(128)
  G = Random(128)
  B = Random(128) + 128
 EndIf
   
 Bias = Random(2) + 1
  
 If Random(40) = 1
  AddElement(Shell())
  Shell()\x = Random(1024);MouseX()
  Shell()\y = 768
  Shell()\vY = 4
  Shell()\str = 5 * 360
  Shell()\clr = RGB(R, G, B)
  Shell()\stp = Random(768)
 EndIf
   
 ForEach Shell()
  If Shell()\stp > Shell()\y
   For pCount = 1 To Shell()\str
    AddElement(Proj())
    Proj()\x = Shell()\x
    Proj()\y = Shell()\y
    Proj()\angle = pCount
    Proj()\speed = (Random(20) + 20) / Random(40)
    Proj()\clr = Shell()\clr
    Proj()\g = 1.1
   Next
   DeleteElement(Shell(), 1)
  EndIf
 Next
 
 StartDrawing(ScreenOutput())
  ;Circle(MouseX(), MouseY(), 1, RGB(255, 128, 128))
  ForEach Shell()
   Circle(Shell()\x, Shell()\y, 2, RGB(255,255,255))
   Shell()\y = Shell()\y - Shell()\vY
   If Shell()\y <= 1
    DeleteElement(Shell())
   EndIf
  Next
    
  ForEach Proj()
   angle = Proj()\angle
   Proj()\x = Proj()\x + (Cos(angle * 3.141592/180) * Proj()\speed)
   Proj()\y = Proj()\y + (Sin(angle * 3.141592/180) * Proj()\speed) + Proj()\g
   Proj()\g = Proj()\g + 0.1
   Proj()\speed = Proj()\speed + 0.001
   
   clr = Proj()\clr
   cR = Red(clr)
   cG = Green(clr)
   cB = Blue(clr)
   If cR > cG And cR > cB
;   cR = cR - 1
    cG = cG + 1
    cB = cB + 1
   ElseIf cG > cR And cG > cB
    cR = cR + 1
;   cG = cG - 1
    cB = cB + 1
   ElseIf cB > cR And cB > cG
    cR = cR + 1
    cG = cG + 1
;     cB = cB - 1
   EndIf
   Proj()\clr = RGB(cR, cG, cB)
   
   If Proj()\x >= 1023 Or Proj()\x <= 0 Or Proj()\y >= 767 Or Proj()\y <= 0
    DeleteElement(Proj(), 1)
   Else
    Plot(Proj()\x, Proj()\y, Proj()\clr)
   EndIf
  Next
 
 StopDrawing()
 FlipBuffers()
 
 framecount + 1
 Delay(1)
Until KeyboardReleased(#PB_Key_Escape);MouseDeltaX() <> 0 Or MouseDeltaY() <> 0 Or KeyboardPushed(#PB_Key_All)

Posted: Sun Feb 26, 2006 6:52 am
by rsts
Cool. I like it :)

cheers

Re: Fireworks animation

Posted: Sun Feb 26, 2006 11:33 am
by PB
Line 36: ClearScreen(): Incorrect number of parameters.

Posted: Sun Feb 26, 2006 2:36 pm
by thefool
Nice effect, teapot :)

@PB:
For 4.0 you need to switch ClearScreen(0, 0, 0) with ClearScreen(RGB(0, 0, 0))

Posted: Sun Feb 26, 2006 4:56 pm
by Intrigued
Also I changed the last line of code to work as stated:

Code: Select all

Until KeyboardReleased(#PB_Key_Escape) Or MouseDeltaX() <> 0 Or MouseDeltaY() <> 0 Or KeyboardPushed(#PB_Key_All)
Note: I took out the semi-colon and put in that first Or.

Posted: Sun Feb 26, 2006 5:11 pm
by Joakim Christiansen
theteapot: Nice, I made something similar:
viewtopic.php?t=19614
But you need PB4 for that to work!

Posted: Mon Feb 27, 2006 3:44 am
by theteapot
Yes, it's written for PB 3.94. As thefool said, you need to have ClearScreen(RGB(0, 0, 0)) for PB 4.

Intrigued: I had that line there for when I compiled it as a screensaver.
The code posted is for a screensaver, but when you want the user-interactive version, you need only the escape key.
It all depends on what the user expects.

Thanks for all your feedback!

TheTeapot