Fireworks

BlitzMax Forums/BlitzMax Beginners Area/Fireworks

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Try this. Its a bit rushed cos I've been awake most of the last 24 hours:
Graphics 800,600

Global t:ttimer = CreateTimer(1)

Global fireworkList:TList = New TList
Global sparkList:TList = New TList

Global gravity:Float = 0.1

While Not KeyDown(key_escape)
	Cls
	UpdateFireworks()
	updateSparks()
	DrawFireworks()
	Flip
Wend

Function UpdateFireworks()
	If t.ticks() > 0
		addFirework()
		t._ticks = 0
	EndIf
	For f:firework = EachIn fireworkList
		f.ySpeed :+ gravity
		f.x :+ f.xSpeed
		f.y :+ f.ySpeed
		If f.ySpeed >=1
			CreateExplosion(f,20)
			fireworkList.remove f
		EndIf
	Next
End Function

Function updateSparks()
	For s:spark = EachIn sparkList
		s.x :+ s.xSpeed
		s.ySpeed :+ gravity
		s.y :+ s.ySpeed
		If s.y > GraphicsHeight() Or s.y < 0 Or s.y > GraphicsWidth()
			sparkList.remove s
		EndIf
	Next
End Function

Function drawFireworks()
	For f:firework = EachIn fireworkList
		DrawRect f.x-1,f.y-1,2,2
	Next
	
	For s:spark = EachIn sparkList
		DrawRect s.x,s.y,2,2
	Next
End Function

Function CreateExplosion(f:firework, parts:Int)
	For n:Int = 1 To parts
		Local s:spark = New spark
		s.x = f.x
		s.y = f.y
		s.xSpeed = Rnd(-3,3)
		s.ySpeed = Rnd(-5,3)
		sparkList.addLast s
	Next
End Function

Function addFirework()
	Local f:firework = New firework
	f.x = Rand(0,GraphicsWidth())
	f.y = GraphicsHeight()
	f.xspeed = Rnd(-2,2)
	f.yspeed = -8
	fireworkList.addLast f
End Function

Type FireWork
	Field x:Float
	Field y:Float
	Field xSpeed:Float
	Field ySpeed:Float
End Type

Type spark
	Field x:Float
	Field y:Float
	Field xSpeed:Float
	Field ySpeed:Float
End Type