Starfield...

Monkey Forums/Monkey Code/Starfield...

diemar(Posted 2016) [#1]
 ' starflight demo by dieter marfurt, oct 2016,
' a pseudo-3D starfield with steering, speedcontrol and simple motion blur
' note: very BASIC coding style.
Import monkey.random
Import mojo

Global framerate:Float=20
Global fpsflex:Float=1.0
Global csp_msstart,csp_ms,csp_mslast:Int=0
Global w:Int = 640
Global h:Int = 480

Global xymax:Int = 150 'n stars
Global sp:Float = 1
Global spx:Float = 1

Global img_cockpit:Image

Global xy:Float[][]
Global xy2:Float[][]


Class StarFlightClass Extends App
		Method OnCreate()					Local n:Int = 0		For n = 0 To xymax		 xy[n]=New Float[5]		 xy2[n]=New Float[5]		 xy[n][0]=Rnd(-320,320)'x		 xy[n][1]=Rnd(-240,240)'y		 xy[n][2]=Rnd(0.00,0.05)'speed		 xy[n][3]=Rnd(100,254)'fade in color goal		 xy[n][4]=0' fade in color state
		 xy2[n][0]=xy[n][0]' last position etc., for the motion blur effect		 xy2[n][1]=xy[n][1]		Next
		Print  "move mouse on canvas to steer"
		csp_msstart=Millisecs()		SetUpdateRate 20	End Method

		Method OnUpdate()
#rem' code for realtime correction of speed, tho causes flicker artefacts with this' motion blur effect.csp_mslast=csp_mscsp_ms=Millisecs()fpsflex=( csp_ms-csp_mslast)/(1000.0/framerate)If fpsflex<0.1 Then  fpsflex=0.1EndifIf fpsflex>10.0 Then  fpsflex=10.0Endif#end			Local n:Int = 0		Local b:Float = 0		Local c:Float = 0
		' some highly unscientific, mathematicly incorrect steering/speedcontrol code...			sp=fpsflex*(MouseY/200.0)		spx=fpsflex*-((MouseX -320)/300.0) 		c=(1+sp)*spx		For n = 0 To xymax '  move fwd.
		 xy2[n][0]=xy[n][0]		 xy2[n][1]=xy[n][1]
		 xy[n][0]*=(1+(xy[n][2] *sp))		 xy[n][1]*=(1+(xy[n][2] *sp))		 If (xy[n][0]<-320) Or (xy[n][0]>320) Or (xy[n][1]<-240) Or (xy[n][1]>240)		  xy[n][0]=Rnd(-320,320)		  xy[n][1]=Rnd(-240,240)		  xy[n][4]=0		  xy2[n][0]=xy[n][0]		  xy2[n][1]=xy[n][1]
		 Endif		 b=(1+xy[n][2])*(1+sp) ' steer left right...		  xy[n][0]+=c*b		  If xy[n][0]<-320		   xy[n][0]+=640		   xy2[n][0]=xy[n][0]		  Endif		  If xy[n][0]>319		   xy[n][0]-=640		   xy2[n][0]=xy[n][0]		  Endif		Next	End Method


	Method OnRender()

		Cls 0,0,0		Local c:Int = 0.0		Local sp2:Float = 1+sp		Local x1,y1,x2,y2:Int		For Local n:Int = 0 To xymax		 c=xy[n][4] ' manage color fade in		 If c<xy[n][3]		  c+=(10*sp2)		  If c>xy[n][3]		   c=xy[n][3]		  Endif		  xy[n][4]=c		 Endif		 SetColor c,c,c
		 x1=320+xy[n][0]		 y1=240+xy[n][1]		 x2=320+xy2[n][0]		 y2=240+xy2[n][1]
' seems like there's a bug in Drawline when coord1 and coord2 are same (vanishes?), therefor:' (may be caused by antialiased line, would explain it. May be machine dependent)		 If (x1=x2) And (y1=y2)		  DrawRect x1,y1,1,1'		  DrawRect x1,y1,2,2' nice variant, twinkles, probably too much.		 Else'		  DrawRect x1,y1,1,1 ' the old standard version without motion blur		  DrawLine x1,y1,x2,y2  ' the simple motion blur version		 Endif		Next		SetColor 255,255,255	End Method


End Class


Function Main()	New StarFlightClassEnd

 




diemar(Posted 2016) [#2]
I don't know what's wrong, it's messing up the linebreaks, but looks ok before posting, has CRLF win linebreaks.
Edit: I uploaded it as zip to my adhoc code archive, at http://anticrap.byethost3.com/

It's a free host without ads, whether they insert sniffing scripts I don't know.


Pakz(Posted 2016) [#3]
Have you tried pasting it into notepad before pasting it in here? Might also be useful to use a hex editor to see if there are weird numbers in the file.
I had some trouble with the forum here not working as it should. I did report it.