explode pixel

BlitzPlus Forums/BlitzPlus Programming/explode pixel

hub(Posted 2003) [#1]
Hi !

How to do an pixels explosion effect in my retro game when my player (1 pixel) die !!!

Thanks !!!


Ross C(Posted 2003) [#2]
here you go. point the mouse where you want the explosion then press the left mouse button to your hearts content :)

Graphics 800,600
SetBuffer BackBuffer()



Type particle
	Field x#,y#
	Field speed#
	Field angle#
	Field time
	Field timer
End Type



While Not KeyHit(1)

	Cls

	If MouseHit(1) Then
		For loop=1 To (Int(Rnd(25,49)))
			create_particles(MouseX(),MouseY())
		Next
	End If
	


	update_particle()
	Rect MouseX(),MouseY(),2,2
	Flip
Wend
End





Function create_particles(x,y)
	p.particle=New particle
	p\angle=Rnd(0,359)
	p\x=x
	p\y=y
	p\speed=Rnd(0.2,1.3)
	p\time=MilliSecs()+Int(Rnd(500,3000))
End Function


Function update_particle()
	For p.particle=Each particle
		Color Int(Rnd(20,250)),Int(Rnd(50,250)),Int(Rnd(50,250))
		Rect p\x,p\y,1,1
		p\x=p\x+(Sin(p\angle)*p\speed)
		p\y=p\y+(Cos(p\angle)*p\speed)
		If MilliSecs()>=p\time Then
			Delete p.particle
		End If
	Next
End Function


to make this useful for you ship thing, when your ship is destoryed, call
create_particles(x,y)

x and y being the location of the ship.

and put
update_particle()

before flip

just remember to put in the functions into your code. and the code that sets up the types.
Function create_particles(x,y)
	p.particle=New particle
	p\angle=Rnd(0,359)
	p\x=x
	p\y=y
	p\speed=Rnd(0.2,1.3)
	p\time=MilliSecs()+Int(Rnd(500,3000))
End Function


Function update_particle()
	For p.particle=Each particle
		Color Int(Rnd(20,250)),Int(Rnd(50,250)),Int(Rnd(50,250))
		Rect p\x,p\y,1,1
		p\x=p\x+(Sin(p\angle)*p\speed)
		p\y=p\y+(Cos(p\angle)*p\speed)
		If MilliSecs()>=p\time Then
			Delete p.particle
		End If
	Next
End Function


hope this helps :o)


Ross C(Posted 2003) [#3]
slightly updated, with some color changing effects. Pixels now start off yellow and fade to red.

Graphics 800,600
SetBuffer BackBuffer()



Type particle
	Field x#,y#
	Field speed#
	Field angle#
	Field time#
	Field timer#
End Type



While Not KeyHit(1)

	Cls

	If MouseDown(1) Then
		For loop=1 To (Int(Rnd(25,49)))
			create_particles(MouseX(),MouseY())
		Next
	End If
	

		If MilliSecs()<timer+1000 Then
								frame=frame+1
	Else
								fps=frame
								frame=0
								timer=MilliSecs()
	End If


	update_particle()
	Rect MouseX(),MouseY(),2,2
	Text 0,0,"fps="+fps
	Flip
Wend
End





Function create_particles(x,y)
	p.particle=New particle
	p\angle=Rnd(0,359)
	p\x=x
	p\y=y
	p\speed=Rnd(0.2,1.3)
	p\timer=Int(Rnd(500,3000))
	p\time=MilliSecs()+p\timer
End Function


Function update_particle()
	For p.particle=Each particle
		Color 200+55*(1-(MilliSecs()-(p\time-p\timer))/(p\timer)),220*(1-(MilliSecs()-(p\time-p\timer))/(p\timer)),5
		Rect p\x,p\y,1,1
		p\x=p\x+(Sin(p\angle)*p\speed)
		p\y=p\y+(Cos(p\angle)*p\speed)
		If MilliSecs()>=p\time Then
			Delete p.particle
		End If
	Next
End Function



hub(Posted 2003) [#4]
Many thanks Joker for this piece of code ! Nice effect !


Perturbatio(Posted 2003) [#5]
Another attempt at it, using an image to determine the pixel colour (a bit of a mess but it works).

Graphics 800,600, 16, 2 

Global colours = LoadImage("colours.png")
Global NumCols = ImageWidth(colours)-1
Dim ColArray(NumCols*3)
LoadCols()

SetBuffer BackBuffer() 


Const GRAVITY# = 0.009
Global LIFESPAN% = (numcols/25);3
Const MIN_PARTICLES = 10
Const MAX_PARTICLES = 40

Type particle 
	Field x#,y# 
	Field speed# 
	Field angle# 
	Field time 
	Field timer 
	Field Col
End Type 


While Not KeyHit(1) 

Cls 

If MouseDown(1) Then 
For loop=1 To (Int(Rnd(MIN_PARTICLES,MAX_PARTICLES))) 
create_particles(MouseX(),MouseY()) 
Next 
End If 



update_particle() 
Rect MouseX(),MouseY(),2,2 
Flip 
Wend 
End 



Function create_particles(x,y) 
	p.particle=New particle 
	p\angle=Rnd(0,359) 
	p\x=x 
	p\y=y 
	p\speed=Rnd(0.2,1.3) 
	p\time=MilliSecs()+ (NumCols * LIFESPAN) 
	p\col = 0
End Function 


Function update_particle() 

	For p.particle=Each particle
		
		Color ColArray(p\col), ColArray(p\col+1), ColArray(p\col+2) 			;Int(Rnd(20,250)),Int(Rnd(50,250)),Int(Rnd(50,250)) 
		p\col = p\col + 3
		If p\col > NumCols Then p\col = NumCols
		
		Rect p\x,p\y,1,1 
		
		p\x=p\x+(Sin(p\angle)*p\speed) 
		p\y=p\y+(Cos(p\angle)*p\speed) + Int((GRAVITY# * Float(p\col)))
		
		If MilliSecs()>=p\time Then 
			Delete p.particle 
		End If 
	Next 
	
End Function 

Function LoadCols()

OldBuffer = GraphicsBuffer()
SetBuffer(ImageBuffer(colours))

For iLoop = 0 To NumCols  Step 3 
	GetColor(iLoop,0)
	ColArray(iLoop) = ColorRed()
	ColArray(iLoop+1) = ColorGreen()
	ColArray(iLoop+2) = ColorBlue()
Next

SetBuffer(OldBuffer)

End Function




The image I used:



Ross C(Posted 2003) [#6]
hey, that's a pretty cool way of doing it :)