help with basic particle code

Just starting out? Need help? Post your questions and find answers here.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

help with basic particle code

Post by J. Baker »

I'm not sure why this will not work correctly? Any help would be great, thanks.

Code: Select all

Structure SnowCalculations
  SnowX.l
  SnowY.l
EndStructure

Procedure SnowFlakes(SnowSprite)

  Static Dim Snow.SnowCalculations(100)
  
  SnowCount +1
  
    If SnowCount > 101
      SnowRandom = 1
    Else
      SnowRandom = 0
    EndIf

    If SnowRandom = 1
     Snow(SnowSprite)\SnowX = Random(800)
     Snow(SnowSprite)\SnowY = Random(600)
    EndIf
  
    If Snow(SnowSprite)\SnowY = 600 +32
      Snow(SnowSprite)\SnowY = -32
      Snow(SnowSprite)\SnowX = Random(800)
     Else
       DisplayTransparentSprite(SnowSprite, Snow(SnowSprite)\SnowX, Snow(SnowSprite)\SnowY)
    EndIf
    
  Snow(SnowSprite)\SnowY +1
    
EndProcedure

InitSprite()

;InitKeyboard()

UsePNGImageDecoder()

WinWidth = 800
WinHeight = 600

OpenWindow(0,0,0,WinWidth,WinHeight,"Snow Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

OpenWindowedScreen(WindowID(0),0,0,WinWidth,WinHeight,0,0,0)

 For Snow = 1 To 100
   CatchSprite(Snow, ?Pic0, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
 Next Snow

MaxSnow = 0  
quit=0
;SnowRandom = 1


Repeat
  
  Repeat
    ev=WindowEvent()
    If ev=#PB_Event_CloseWindow
      quit=1
    EndIf
  Until ev=0
  
  ;ExamineKeyboard()
  
    For Snow = 1 To 100
      
      SnowFlakes(Snow)
  
    Next Snow
  
  FlipBuffers() 
  ClearScreen(0)
  
Until quit

DataSection
Pic0: IncludeBinary "flake.png"
EndDataSection
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: help with basic particle code

Post by infratec »

Hi,

first of all I think something like

Code: Select all

Static SnowCount = 0
is missing in the SnowFlakes() procedure.

Code: Select all

SnowCount + 1 
Should be inside the else part. Else it sometimes come back to < 101

And than:
What do you want :?:

Without an exact explanation what the result should be, no one can adjust the code.

Bernd
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: help with basic particle code

Post by infratec »

Maybe you want this

Code: Select all

#WinWidth = 800
#WinHeight = 600
#MaxFlakes = 100

Structure SnowCalculations
  X.i
  Y.i
  S.i
EndStructure


Procedure SnowFlakes(SnowSprite)

  Static Dim Snow.SnowCalculations(#MaxFlakes)
  Static SnowCount = 0
  
  If SnowCount < #MaxFlakes
    Snow(SnowSprite)\X = Random(#WinWidth)
    Snow(SnowSprite)\Y = Random(#WinHeight) - #WinHeight
    Snow(SnowSprite)\S = Random(2) + 1
    SnowCount + 1
  EndIf
  
  If Snow(SnowSprite)\Y > #WinHeight + 32
    Snow(SnowSprite)\Y = -32
    Snow(SnowSprite)\X = Random(#WinWidth)
    Snow(SnowSprite)\S = Random(2) + 1
  Else
    DisplayTransparentSprite(SnowSprite, Snow(SnowSprite)\X, Snow(SnowSprite)\Y)
  EndIf
  
  Snow(SnowSprite)\Y + Snow(SnowSprite)\S
   
EndProcedure


UsePNGImageDecoder()
InitSprite()

OpenWindow(0, 0, 0,#WinWidth, #WinHeight, "Snow Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #WinWidth, #WinHeight, 0, 0, 0)

For Snow = 1 To #MaxFlakes : CatchSprite(Snow, ?Pic0, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending) : Next Snow

Quit = #False

Repeat
  
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow : Quit = #True : EndIf
  Until Event = 0
  
  FlipBuffers()
  ClearScreen(0)
  
  For Snow = 1 To #MaxFlakes : SnowFlakes(Snow) : Next Snow
  
  Delay(5)
  
Until Quit

DataSection
Pic0: IncludeBinary "flake.png"
EndDataSection
Best regards,

Bernd
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: help with basic particle code

Post by J. Baker »

Thanks infratec! Yes I was originally just trying to get them to fall properly but you also added the random speed for me. I see what I wasn't doing correctly now. Thanks again! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: help with basic particle code

Post by infratec »

Hi,

you are welcome :!:

And only for fun I added a start/stop function (cursor up/down) :mrgreen: :mrgreen: :mrgreen:

Code: Select all

#WinWidth = 800
#WinHeight = 600
#MaxFlakes = 100
#SpriteHeight = 32

Structure SnowCalculations
  X.i
  Y.i
  V.i
  S.i
EndStructure


Procedure SnowFlakes(SnowSprite, Status)

  Static Dim Snow.SnowCalculations(#MaxFlakes)
    
  If Status 
    If Not Snow(SnowSprite)\S
      Snow(SnowSprite)\X = Random(#WinWidth)
      Snow(SnowSprite)\Y = Random(#WinHeight) - #WinHeight
      Snow(SnowSprite)\V = Random(2) + 1
      Snow(SnowSprite)\S = #True
    EndIf
  Else
    If Snow(SnowSprite)\Y < #SpriteHeight * -1
      Snow(SnowSprite)\Y = #WinHeight + #SpriteHeight
    EndIf
  EndIf
  
  If Snow(SnowSprite)\Y >= #WinHeight + #SpriteHeight
    If Status
      Snow(SnowSprite)\Y = #SpriteHeight * -1
      Snow(SnowSprite)\X = Random(#WinWidth)
      Snow(SnowSprite)\V = Random(2) + 1
    Else
      Snow(SnowSprite)\S = #False
    EndIf
  Else
    DisplayTransparentSprite(SnowSprite, Snow(SnowSprite)\X, Snow(SnowSprite)\Y)
  EndIf
  
  If Snow(SnowSprite)\S : Snow(SnowSprite)\Y + Snow(SnowSprite)\V : EndIf
   
EndProcedure


UsePNGImageDecoder()
InitKeyboard()
InitSprite()

OpenWindow(0, 0, 0,#WinWidth, #WinHeight, "Snow Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #WinWidth, #WinHeight, 0, 0, 0)

For Snow = 1 To #MaxFlakes : CatchSprite(Snow, ?Pic0, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending) : Next Snow

Quit = #False

SnowStatus = #True

Repeat
  
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow : Quit = #True : EndIf
  Until Event = 0
  
  ExamineKeyboard()
  
  If KeyboardReleased(#PB_Key_Up) : SnowStatus = #False : EndIf
  If KeyboardReleased(#PB_Key_Down) : SnowStatus = #True : EndIf
  
  FlipBuffers()
  ClearScreen(0)
  
  For Flake = 1 To #MaxFlakes : SnowFlakes(Flake, SnowStatus) : Next Flake
  
  Delay(5)
  
Until Quit

DataSection
Pic0: IncludeBinary "flake.png"
EndDataSection
Have fun,

Bernd
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: help with basic particle code

Post by J. Baker »

Pretty Cool! :D

I just need to make them Sprite3D() now. So that they can be zoomed and rotated. :wink:
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: help with basic particle code

Post by J. Baker »

We now have zoomed and rotating snow based on speed. :D

Code: Select all

#WinWidth = 800
#WinHeight = 600
#MaxFlakes = 100

Structure SnowCalculations
  SnowX.i
  SnowY.i
  SnowS.i
EndStructure

Procedure SnowFlakes(SnowSprite)

  Static Dim Snow.SnowCalculations(#MaxFlakes)
  Static SnowCount = 0
 
  If SnowCount < #MaxFlakes
    Snow(SnowSprite)\SnowX = Random(#WinWidth)
    Snow(SnowSprite)\SnowY = Random(#WinHeight) - #WinHeight
    Snow(SnowSprite)\SnowS = Random(2) + 1
    SnowCount + 1
  EndIf
 
  If Snow(SnowSprite)\SnowY > #WinHeight + 32
    Snow(SnowSprite)\SnowY = -32
    Snow(SnowSprite)\SnowX = Random(#WinWidth)
    Snow(SnowSprite)\SnowS = Random(2) + 1
  Else
    Start3D()
     ZoomSprite3D(SnowSprite, Snow(SnowSprite)\SnowS * 8, Snow(SnowSprite)\SnowS * 8)
     DisplaySprite3D(SnowSprite, Snow(SnowSprite)\SnowX, Snow(SnowSprite)\SnowY)
     RotateSprite3D(SnowSprite, 0.2 * Snow(SnowSprite)\SnowS, #PB_Relative)
    Stop3D()
  EndIf
 
  Snow(SnowSprite)\SnowY + Snow(SnowSprite)\SnowS
   
EndProcedure

UsePNGImageDecoder()
InitSprite()
InitSprite3D()

OpenWindow(0, 0, 0,#WinWidth, #WinHeight, "Snow Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #WinWidth, #WinHeight, 0, 0, 0)

For Snow = 1 To #MaxFlakes
  CatchSprite(Snow, ?Pic0, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
  CreateSprite3D(Snow, Snow)
Next Snow

Quit = #False

Repeat
 
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow
      Quit = #True
    EndIf
  Until Event = 0
 
  FlipBuffers()
  ClearScreen(0)
 
  For Snow = 1 To #MaxFlakes
    SnowFlakes(Snow)
  Next Snow
 
  Delay(5)
 
Until Quit

DataSection
Pic0: IncludeBinary "flake.png"
EndDataSection
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: help with basic particle code

Post by J. Baker »

With a little more tweaking, I got pretty much what I was looking for.
http://www.flashpulse.com/purebasic/Snow%20Test.zip
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
rootuid
User
User
Posts: 48
Joined: Sat Nov 23, 2013 11:46 am

Re: help with basic particle code

Post by rootuid »

I compiled but got an error stating that start3d() is not a valid function. Is InitEngine3D the new version of this function ?
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: help with basic particle code

Post by J. Baker »

rootuid wrote:I compiled but got an error stating that start3d() is not a valid function. Is InitEngine3D the new version of this function ?
This code hasn't been updated for the newest version of PureBasic. There aren't any Sprite3D commands as they all have been updated to the normal Sprite commands. Check out the help file. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
rootuid
User
User
Posts: 48
Joined: Sat Nov 23, 2013 11:46 am

Re: help with basic particle code

Post by rootuid »

Thanks for the heads up J. Baker !
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: help with basic particle code

Post by J. Baker »

rootuid wrote:Thanks for the heads up J. Baker !
Got a little time so I quickly edited the code for PureBasic v5.21. Alter to your liking. ;)

Code: Select all

#WinWidth = 800
#WinHeight = 600
#MaxFlakes = 100

Structure SnowCalculations
  SnowX.i
  SnowY.i
  SnowS.i
EndStructure

Procedure SnowFlakes(SnowSprite)

  Static Dim Snow.SnowCalculations(#MaxFlakes)
  Static SnowCount = 0
 
  If SnowCount < #MaxFlakes
    Snow(SnowSprite)\SnowX = Random(#WinWidth)
    Snow(SnowSprite)\SnowY = Random(#WinHeight) - #WinHeight
    Snow(SnowSprite)\SnowS = Random(2) + 1
    SnowCount + 1
  EndIf
 
  If Snow(SnowSprite)\SnowY > #WinHeight + 32
    Snow(SnowSprite)\SnowY = -32
    Snow(SnowSprite)\SnowX = Random(#WinWidth)
    Snow(SnowSprite)\SnowS = Random(2) + 1
  Else
     ZoomSprite(SnowSprite, Snow(SnowSprite)\SnowS * 8, Snow(SnowSprite)\SnowS * 8)
     DisplayTransparentSprite(SnowSprite, Snow(SnowSprite)\SnowX, Snow(SnowSprite)\SnowY)
     RotateSprite(SnowSprite, 0.2 * Snow(SnowSprite)\SnowS, #PB_Relative)
  EndIf
 
  Snow(SnowSprite)\SnowY + Snow(SnowSprite)\SnowS
   
EndProcedure

UsePNGImageDecoder()
InitSprite()

OpenWindow(0, 0, 0,#WinWidth, #WinHeight, "Snow Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #WinWidth, #WinHeight, 0, 0, 0)

For Snow = 1 To #MaxFlakes
  CatchSprite(Snow, ?Pic0, #PB_Sprite_AlphaBlending)
Next Snow

Quit = #False

Repeat
 
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow
      Quit = #True
    EndIf
  Until Event = 0
 
  FlipBuffers()
  ClearScreen(0)
 
  For Snow = 1 To #MaxFlakes
    SnowFlakes(Snow)
  Next Snow
 
  Delay(5)
 
Until Quit

DataSection
  Pic0:
   IncludeBinary "flake.png"
EndDataSection
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply