Code archives/Graphics/Simple particle engine

This code has been declared by its author to be Public Domain code.

Download source code

Simple particle engine by Andres2006
image% - either singleframed or multiframed image handle
x#, y# - location of the particle
xs#, ys# - velocity of the particle
duration% - duration of the particle in millisecs
frames% - if does then how many frames particle has (default: 1 frame)
Global Inerts# - Will be multiplied with velocity in every update
Type particle
	Field image%, x#, y#, xs#, ys#, w%, h%, time%, duration%, frames%
End Type

Global Inerts# = .9

Function CreateParticle(image%, x#, y#, xs#, ys#, duration%, frames% = 1)
	this.particle = New particle
		this\image% = CopyImage(image%)
		this\x# = x#
		this\y# = y#
		this\xs# = xs#
		this\ys# = ys#
		this\w% = ImageWidth(this\image%)
		this\h% = ImageHeight(this\image%)
		this\time% = MilliSecs()
		this\duration% = duration%
		this\frames% = frames%
End Function

Function DrawParticles()
	For par.particle = Each particle
		If par\frames% = 1
			DrawImage par\image%, par\x% - par\w% / 2, par\y% - par\h% / 2
		Else
			frame% = (par\frames% - 1) * (Float (MilliSecs() - par\time%) / par\duration%)
			If frame% > par\frames% Then frame% = par\frames%
			DrawImage par\image%, par\x% - par\w% / 2, par\y% - par\h% / 2, frame%
		EndIf
	Next
End Function

Function UpdateParticles()
	For par.particle = Each particle
		If MilliSecs() - par\time% < par\duration% Then
			par\x# = par\x# + par\xs#
			par\y# = par\y# + par\ys#
			
			par\xs# = par\xs# * Inerts#
			par\ys# = par\ys# * Inerts#
		Else
			FreeImage par\image%
			Delete par
		EndIf
	Next
End Function

Comments

None.

Code Archives Forum