birthday cool code.

Monkey Forums/Monkey Beginners/birthday cool code.

hub(Posted 2015) [#1]
Hi !

I'm searching a cool code to display some nice coloured confettis (square images) on screen (1280x720). It's for the birthday of olders persons inside our nursing home. I already display the person name at the center of the screen,an background image and play a music.

Have you some idea or code example ? (i want a short code for this, not use a huge class). I'm crap with math to code this kind of nice effects !

Thanks.


rIKmAN(Posted 2015) [#2]
I would write a simple particle emitter, and draw (or Google) for some confetti graphics to attach.

There may be some particle code on here or in the code archives, if not you could always look at some Blitz / BlitzMax code from their archives and port it over which should be pretty simple.

A quick forum search found this:
http://www.monkey-x.com/Community/posts.php?topic=5255

If you have something like Spine or Spriter you could create the animation in that and then just play it on loop using the Monkey Runtimes (not sure about Spriter but the Spine module works fine in Monkey), or one of the free frameworks might have particle stuff in it (Diddy/fantomEngine) but you would have to check that as I'm not sure if they do.


hub(Posted 2015) [#3]
I'm really searching a tiny code sample, easy to add without import !

i will code a simple class like single explosions. (cos, sin at random angle and speed, and at each x,y point a rotated and scaled image.

Finally i code my own idea :-)

Many thanks.


Capn Lee(Posted 2015) [#4]
hope this helps, there's not much maths to it as it is.

i haven't tested how much you can put on screen without causing problems but it seems to handle a thousand or so at once okay


Import mojo

Global confettiSize:Float = 12.0


Global confettiList:List<Confetti> = New List<Confetti>


Function Main()
	Local c:ConfettiApp = New ConfettiApp
End

Class ConfettiApp Extends App
	
	Method OnCreate()
		SetUpdateRate(30)
		CreateConfetti(325)
	End
	
	Method OnUpdate()
		If KeyHit(KEY_SPACE) Then KillConfetti()
		If KeyHit(KEY_ENTER) Then CreateConfetti(325)
		UpdateConfetti()
	End
	
	Method OnRender()
		Cls(0, 0, 0)
		RenderConfetti()
		DrawText("Space Removes all confetti", 8, DeviceHeight() -48, 0, 0)
		DrawText("Return throws another batch", 8, DeviceHeight() -32, 0, 0)
	End
End



Function CreateConfetti(quantity:Int)
	For Local i:Int = 0 Until quantity
		Local c:Confetti = New Confetti()
	Next
End

Function UpdateConfetti()
	For Local c:Confetti = EachIn confettiList
		c.Update()
	Next
End

Function RenderConfetti()
	For Local c:Confetti = EachIn confettiList
		c.Render()
	Next	
End

Function KillConfetti()
	confettiList.Clear()
End

Class Confetti
	
	Field xPos:Float
	Field yPos:Float
	
	Field rColor:Int
	Field gColor:Int
	Field bColor:Int
	
	Field speed:Float

	Field angle:Float
	
	Method New()
		xPos = Rnd(0, DeviceWidth())
		yPos = Rnd(-400, 0)
		RandomConfettitColor()
		speed = Rnd(5, 15)
		angle = Rnd(365)
		confettiList.AddLast(Self)
	End
	
	Method RandomConfettitColor()
		Local f:Float = Rnd()
		If f < 0.2
			'PastelBlue
			rColor = 193
			gColor = 238
			bColor = 255
		ElseIf f < 0.4
			'PastelPink
			rColor = 255
			gColor = 193
			bColor = 214
		ElseIf f < 0.6
			'PastelGreen
			rColor = 213
			gColor = 254
			bColor = 194
		ElseIf f < 0.8
			'PastelYellow
			rColor = 254
			gColor = 250
			bColor = 194
		Else
			'PastelRed
			rColor = 255
			gColor = 193
			bColor = 193
		EndIf
	End
	
	Method Update()
		yPos += speed
		If yPos > DeviceHeight() + (2 * confettiSize)
			confettiList.Remove(Self)
		EndIf
		angle += speed * 1.5
	End
	
	Method Render()
		PushMatrix()
			Translate(xPos - (confettiSize / 2), yPos - (confettiSize / 2))
			Rotate(angle)
			SetColor(rColor, gColor, bColor)
			DrawRect(-2, -2, confettiSize - 2, confettiSize - 2)
		PopMatrix()
	End
End



Pakz(Posted 2015) [#5]
Here is something I made that is short and has a party vibe to it. (fireworks/fountain) See the link to view the flash on the page running the code.

http://monkeygameprogramming.blogspot.nl/2014/12/monkey-x-2d-fireworks-using-lists-code.html

Here the code. It is one of the posts that had more then avarage amount of pageviews on my blog.

Import mojo

Class sprite
    Field x#,incx#
    Field y#,incy#
    Method New()
        x = DeviceWidth/2
        y = DeviceHeight
        incx=Rnd(1,2);If Rnd(1)>=.5 incx=-incx
        incy=Rnd(-3,-15)
    End Method
End Class

Class MyGame Extends App
    Field spritelist:List<sprite> = New List<sprite>

    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()
        spritelist.AddLast(New sprite)
        For Local i:=Eachin spritelist
            i.x += i.incx
            i.y += i.incy
            i.incy += 0.1
        Next
        For Local i:=Eachin spritelist
            If i.y > DeviceHeight Then spritelist.Remove i
        Next
        
    End Method
    Method OnRender()
        Local cnt:Int = 0
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local i:=Eachin spritelist
            DrawCircle i.x,i.y,3
            cnt+=1
        Next
        DrawText "Count: "+cnt,10,10
    End
End

Function Main()
    New MyGame()
End



hub(Posted 2015) [#6]
I've already program something with arrays. But it's interesting to read your code, especially so i can understand how to work with lists ! Many thanks for your help.