About Monkey 2 › Forums › Monkey 2 Code Library › Sine scroller
This topic contains 0 replies, has 1 voice, and was last updated by
Gwenel
1 year ago.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
April 14, 2018 at 1:11 am #14357Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485' Sine scroller example' Download https://opengameart.org/content/bitmap-font-pack' Unpack and put the files in the assets folderNamespace Myapp#Import "<std>"#Import "<mojo>"#Import "assets/"Using std..Using mojo..' Main codeFunction Main()' These are needed to trigger the Class we defined (actually extended by overwriting a few methods that Monkey2 already defined, very special methods; NEW and OnRender)New AppInstanceNew MyApp ' This triggers NewApp.Run()End' Define a class, not any class but one that extends Monkey2's functionalityClass MyApp Extends WindowField cnvs : CanvasField img : ImageField Sine : IntField ScrollText : StringField PixelPos : IntField Pos: IntField CharPos : IntField Char : Int' Overwrite a special and already-existing method in this class called new also called constructors as they are triggered whenever using the NEW keyword, it doubles as an init as it runs only once *Method New( title:String="Sine scroller",width:Int=800,height:Int=600,flags:WindowFlags=Null )Super.New( title,width,height,flags)img = New Image(944,16)cnvs = New Canvas(img)img = Image.Load("asset::kromasky_16x16.png")ScrollText = " "ScrollText = ScrollText + "A SIMPLE SINE SCROLLER ROUTINE"ScrollText = ScrollText + " "End' Overwrite a special and already-existing method in this class called OnRender. This is a native functionality that handles graphics which is triggered 60 times a second, this can be changed.Method OnRender( canvas:Canvas ) OverrideApp.RequestRender() ' call standard Monkey2 codecanvas.Clear(Color.Black)PixelPos = PixelPos + 2Sine = Sine - 2 Mod 360If PixelPos > 16 Then PixelPos = PixelPos Mod 16 ; CharPos = CharPos + 1If CharPos > ScrollText.Length - 52 Then CharPos = 1Local Adder : Int =0 ' Instead of defining a Field we can do this if we don't need something as a field.Pos = -16Pos = Pos - PixelPosRepeatChar = (ScrollText.Mid(CharPos+Adder,1)[0] - 32) * 16For Local temp:Int = 0 To 15canvas.DrawRect(Pos+temp,256+(Sin(Deg2Rad(Sine+Pos+temp)) * 32),1,16,img,Char+temp,0,1,16) ' img is the sourceimage containing font stripNextAdder = Adder + 1Pos = Pos + 16Until Pos >= 800cnvs.Flush()End' These are just handy functionsFunction Rad2Deg:Double(x:Double)Const DegreeScalar := (180.0 / Pi)Return (x * DegreeScalar)EndFunction Deg2Rad:Double(x:Double)Const RadianScalar := (Pi / 180.0)Return (x * RadianScalar)EndFunction Cosd:Double(x:Double)Return Cos(Rad2Deg(x))EndFunction Sind:Double(x:Double)Return Sin(Rad2Deg(x))EndEnd
-
AuthorPosts
Viewing 1 post (of 1 total)
You must be logged in to reply to this topic.