Fireworks animation

Share your advanced PureBasic knowledge/code with the community.
theteapot
User
User
Posts: 37
Joined: Fri Sep 09, 2005 7:46 am

Fireworks animation

Post 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)
Using PB 3.94 demo AND PROUD OF IT!!

*Goes back to little hole*
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Cool. I like it :)

cheers
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Fireworks animation

Post by PB »

Line 36: ClearScreen(): Incorrect number of parameters.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Nice effect, teapot :)

@PB:
For 4.0 you need to switch ClearScreen(0, 0, 0) with ClearScreen(RGB(0, 0, 0))
Intrigued
Enthusiast
Enthusiast
Posts: 501
Joined: Thu Jun 02, 2005 3:55 am
Location: U.S.A.

Post 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.
Intrigued - Registered PureBasic, lifetime updates user
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

theteapot: Nice, I made something similar:
viewtopic.php?t=19614
But you need PB4 for that to work!
theteapot
User
User
Posts: 37
Joined: Fri Sep 09, 2005 7:46 am

Post 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
Using PB 3.94 demo AND PROUD OF IT!!

*Goes back to little hole*
Post Reply