- Joined
- Jan 2, 2020
- Messages
- 1,414
The simplest menu and editor that you could ever imagine,
just to show how to use strings and keyboard.
Try to move the white text to the left par of the screen using the cursors keys.
just to show how to use strings and keyboard.
Try to move the white text to the left par of the screen using the cursors keys.
Cerberus:
Import mojo2
Function Main()
New Game
Return 0
End
Class Game Extends App
Field ascii:String
Field str:String = ""
Field screen:String[100]
Field canvas:Canvas
Field x:Int = 256
Field y:Int = 12*8
Method OnCreate()
canvas = New Canvas()
Print Len("test") ' 4
Print Mid("test",2,2) ' es
Print Left("test",2) ' te
Print Right("test",2) ' st
Print Chr(65) ' A
Print Val("A") ' 65
' Alphabet outputs 1 - 26 regardless if the letter A-Z is in uppercase or lowercase
Print Alphabet("A") ' Outputs 1 for a
Print Alphabet("a") ' Outputs 1 for a
Print Alphabet("abba",0) ' First letter, outputs 1 for a
Print Alphabet("abba",1) ' Second letter, outputs 2 for b
Print Alphabet("abba",2) ' Third letter, 2 for b
ascii = "D"
Print Val(ascii)
screen[0] = "Miami, Miami, you've got style. Blue sky, sunshine, white sand by the mile."
screen[1] = "When you live in this town, each day is sublime. The coldest of winters are warm and divine."
screen[2] = "Miami, Miami, you've got style. Blue sky, sunshine, white sand by the mile."
screen[3] = "There's ball clubs and night clubs, all within reach."
screen[4] = "Dance the samba 'till morning, then lie on the beach."
screen[5] = "Each view is a postcard, each day a great time."
screen[6] = "The cream of the crop, it's the top of the line."
screen[7] = "Miami, Miami, you've got style. Blue sky, sunshine, white sand by the mile."
screen[8] = "Miami, you've got style!"
Return 0
End
Method OnUpdate()
Local chr:Int = GetChar()
If chr >= 32 And chr < 128 Then str = str + Chr(chr)
If chr = 8 Then str = Left(str,Len(str)-1)
If chr <> 0 Then Print chr
Local oldy:Int = y
If chr = 65573 Then Print "Left" ; x = x - 8 ; If x < -16 Then x = -16
If chr = 65575 Then Print "Right" ; x = x + 8 ; If x > 640-8 Then x = 640-8
If chr = 65574 Then Print "Up" ; y = y - 12 ; If y < 0 Then y = 0
If chr = 65576 Then Print "Down" ; y = y + 12 ; If y > 480-12 Then y = 480-12
If chr = 13 Then Print "Enter" ; y = y + 12 ; If y > 480-12 Then y = 480-12
If oldy <> y Then str = screen[Floor(y/12)]
screen[Floor(y/12)] = str
End
Method OnRender()
canvas.Clear
canvas.SetColor 0,0.2,0,1
For Local i:Int = 0 To 99
canvas.DrawText screen[i],0,i * 12
Next
canvas.SetColor 1,1,1,(Millisecs()/1000.0)
canvas.DrawText "> " + str,x,y
canvas.Flush
End
End
Function Len:Int(s:String)
Return s.Length
End
Function Mid:String(s:String,p:Int,n:Int)
p=p-1
Return s[(p)..(p+n)]
End
Function Left:String(s:String,n:Int)
Return s[0..n]
End
Function Right:String(s:String,n:Int)
Return s[(s.Length()-n)..(s.Length())]
End
Function Chr:String(n:Int)
Local result:String
result = result.FromChar(n)
Return result
End
Function Val:Int(s:String)
Local chrs:Int[] = s.ToChars()
Return chrs[0]
End
Function Alphabet:Int(s:String,n:Int=0)
Local chrs:Int[] = s.ToChars()
Return chrs[n] & 31
End
Last edited: