Re: Black Hole effect using sprites / 5.20 beta required
Posted: Mon Jul 08, 2013 12:48 pm
cool demo
couldn't resist and made a BeagleVersion
couldn't resist and made a BeagleVersion
Code: Select all
; BlackHole - BasicallyPure
; 7.5.2013
; PureBasic 5.20 beta <--- YOU NEED THIS
; BeagleVersion
EnableExplicit
#PIx2 = #PI * 2
#Stars = 250
#BlackHoleSpeed = 24 ; larger = slower
Declare Error(message$)
ExamineDesktops()
Define.i dw = DesktopWidth(0), xMid = dw / 2
Define.i dh = DesktopHeight(0), yMid = dh / 2
Define.i dd = DesktopDepth(0)
Define.i a, r, g, b, z, sprite, spriteColor, frameCount
Define.f angle, BH_angle = #PI
Define.i xRadius = dw / 3
Define.i yRadius = dh / 3
Define.i myBeagle
Structure starType
x.f
y.f
speed.f
nSprite.i
EndStructure
Define blackHole.Point
blackHole\x = xMid - xRadius
blackHole\y = yMid
Dim star.starType(#Stars)
For a = 0 To #Stars
star(a)\x = Random(dw - 1)
star(a)\y = Random(dh - 1)
star(a)\nSprite = Random(13)
star(a)\speed = star(a)\nSprite / 2.0 + 1
Next
If InitSprite() And InitKeyboard() And OpenScreen(dw, dh, dd, "Black Hole")
Else : Error("Failed To 'InitSprite/InitKeyboard/OpenScreen'")
EndIf
For sprite = 0 To 13
r = Random(255) : b = Random(255) : g = 255 - (r + b) >> 1
spriteColor = RGB(r, g, b)
If CreateSprite(sprite, 16, 16) = 0 : Error("CreateSprite") : EndIf
StartDrawing(SpriteOutput(sprite))
Ellipse(7,7,6,3,$75CFFF)
Ellipse(7,7,7,1,$003E65)
StopDrawing()
z = 6 + sprite * 2
ZoomSprite(sprite, z, z) ; <-- does not belong inside start/stop drawing block!
Next sprite
myBeagle=CreateSprite(#PB_Any,20,20)
StartDrawing(SpriteOutput(myBeagle))
Ellipse(10,10,5,9,$75CFFF)
Ellipse(10,4,7,2,$75CFFF)
Ellipse(2,10,2,6,$003D66)
Ellipse(18,10,2,6,$003D66)
Circle(10,7,2,#Black);nose
Line(6,6,4,-3,#Black);Eye
Line(14,6,-4,-3,#Black);Eye
Ellipse(10,14,2,4,#Black);Mouth
StopDrawing()
ZoomSprite(myBEagle,50,50)
Repeat ; animation loop
ClearScreen(#Black)
DisplayTransparentSprite(mybeagle,blackHole\x-10,blackhole\y-20)
;StartDrawing(ScreenOutput()) ; <--- don't need this!
For a = 0 To #Stars
With star(a)
angle = ATan2(blackHole\x - \x, blackHole\y - \y)
\x + \speed * Cos(angle)
\y + \speed * Sin(angle)
If Abs(\x - blackHole\x) < 5 And Abs(\y - blackHole\y) < 5
Select Random(4, 1)
Case 1 : \x = Random(dw - 1) : \y = 0
Case 2 : \x = Random(dw - 1) : \y = dh - 1
Case 3 : \x = 0 : \y = Random(dh - 1)
Case 4 : \x = dw - 1 : \y = Random(dh - 1)
EndSelect
EndIf
DisplayTransparentSprite(\nSprite,\x,\y)
EndWith
Next
If frameCount < #BlackHoleSpeed : frameCount + 1
Else : frameCount = 0
BH_angle + Radian(1)
If BH_angle > #PIx2 : BH_angle - #PIx2 : EndIf
blackHole\x = Cos(BH_angle) * xRadius + xMid
blackHole\y = Sin(BH_angle) * yRadius + yMid
EndIf
;StopDrawing()
FlipBuffers()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape) : End : EndIf
ForEver
Procedure Error(message$)
MessageRequester("Fatal Error!", message$) : End
EndProcedure