Wednesday, December 31, 2014

Monkey-X - CreateImage, MidHandle, DrawImage scale and rotation code example


Below the code creates an image of 16*16 and sets the handle to the centre. Then the code
draws the image to the screen with a different scale and with a rotation.

Code Below:

Import mojo

Global im1data:Int[][] = [    [0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0],
                            [0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0],
                            [0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
                            [0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
                            [0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
                            [0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
                            [0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0],
                            [0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,0] ]

    Global rotation:Int = 0              

Class MyGame Extends App
    Field image:Image

    Method OnCreate()
        SetUpdateRate(60)
        image = CreateImage(16, 16,1,image.MidHandle)
        Local pixels:Int[16 * 16]
        
        For Local i:Int = 0 Until 16 * 16
            Local x:Int = i Mod 16
            Local y:Int = i / 16
            If im1data[y][x] = 0
                pixels[i] = argb(0,0,0,0)
            Elseif im1data[y][x] = 1
                pixels[i] = argb(10,200,25)
            Elseif im1data[y][x] = 2
                pixels[i] = argb(210,200,25)
            End If
        Next
        image.WritePixels(pixels, 0, 0, 16, 16, 0)
    End Method
    Method OnUpdate()
        rotation+=1
        If rotation>360 Then rotation = 0  
    End Method
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local i = 0 To 10
            DrawImage image,100+Rnd(32)-16,100+Rnd(32)-16,0,3,3
        Next
        DrawImage image,100,200,rotation,2,2
    End
End

Function Main()
    New MyGame()
End

Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255)
        Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b          
End Function

Monkey-X - Creating and drawing an image from array code example


Here some code that shows how to create images/sprites using arrays and also draw them on the screen.

Code below :

Import mojo


Class MyGame Extends App

    Field image:Image

    Global im1data:Int[][] = [    [0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0],
                                [0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0],
                                [0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
                                [0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
                                [0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
                                [0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
                                [0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0],
                                [0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,0] ]
    
    Method OnCreate:Int()
        SetUpdateRate(60)
        
        image = CreateImage(16, 16)
        Local pixels:Int[16 * 16]
        
        For Local i:Int = 0 Until 16 * 16
            Local x:Int = i Mod 16
            Local y:Int = i / 16
            If im1data[y][x] = 0
                pixels[i] = argb(0,0,0,0)
            Elseif im1data[y][x] = 1
                pixels[i] = argb(10,200,25)
            Elseif im1data[y][x] = 2
                pixels[i] = argb(210,200,25)
            End If
        Next
        image.WritePixels(pixels, 0, 0, 16, 16, 0)
    End
    
    Method OnRender:Int()
        Cls()
        For Local x = 0 Until DeviceWidth Step 16
        For Local y = 0 Until DeviceHeight Step 16
            DrawImage(image, x,y)
        Next
        Next
        
    End
    
End

Function Main:Int()
    New MyGame()
End

Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255)
        Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b          
End Function

Monkey-X - WritePixel argb function and CreateImage code Example


The code below shows you how to create images and how to draw pixels into them. There is a function in the code that allows you to draw using r g b. The alpha value to 255 means not transparent. 0 alpha value is transparent.

Code below :

Import mojo


Class MyGame Extends App

    Field image:Image
    
    Method OnCreate:Int()
        SetUpdateRate(15)
        
        image = CreateImage(320, 200)
        Local pixels:Int[320 * 200]
        
        For Local i:Int = 0 Until 320 * 200
            pixels[i] = argb(255,255,255)
        Next
        image.WritePixels(pixels, 0, 0, 320, 200, 0)
    End
    
    Method OnRender:Int()
        Cls()
        
        DrawImage(image, 10, 10)
        
    End
    
End

Function Main:Int()
    New MyGame()
End

Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255)
        Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b          
End Function

Tuesday, December 30, 2014

Monkey-X - DrawPoly and DrawPoly using array code example


The Drawpoly command can also contain a Image that will be scaled with the poly coordinatesI believe. This I will probably post later. Here below is how to use the command DrawPoly with and without an array.


Code Below :

Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Local pol:Float[6]
        pol[0] = 200
        pol[1] = 200
        pol[2] = 250
        pol[3] = 200
        pol[4] = 200
        pol[5] = 250
        Cls(0,0,0)
        SetColor(255,255,255)
        DrawPoly([100.0,100.0,150.0,150.0,100.0,150.0])
        DrawPoly(pol)
    End
End

Function Main()
    New MyGame()
End

Monday, December 29, 2014

Monkey-X - Draw Boxed Rectangle Code Example


Monkey can not draw Rectangles that are not filled so you need to create your own code to
draw that. Below is shown how I done this.


Code below :

Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        drawboxedrect(100,100,100,100)
        For Local i=0 Until 10
            drawboxedrect Rnd(100)+200,Rnd(200)+100,Rnd(50)+50,Rnd(50)+50
        Next
    End
End

Function drawboxedrect:Void(x:Int,y:Int,w:Int,h:Int)
    DrawLine x,y,x+w,y
    DrawLine x,y,x,y+h
    DrawLine x,y+h,x+w,y+h
    DrawLine x+w,y,x+w,y+h
End Function

Function Main()
    New MyGame()
End

Saturday, December 27, 2014

Monkey-X - Millisecs, Width, Height and Date code example


Below some code that shows you how to do DeviceWidth and DeviceHeight and Millisecs and Date.

Code below :

Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Local date:=GetDate()
        Cls(0,0,0)
        SetColor(255,255,255)
        DrawText "Millisecs command, milliseconds from start of app : "+Millisecs,10,10
        DrawText "DeviceWidth and DeviceHeight command : "+DeviceWidth +","+DeviceHeight,10,20
        DrawText "Date Year :"+date[0]+" Month :"+date[1]+" Day:"+date[2],10,30        
    End
End

Function Main()
    New MyGame()
End

Monkey-X - Stack Pop and Push and Length and IsEmpty code example


This code shows how to set up a stack and how to add and remove items from it. Also Length and IsEmpty is used.

Code below : (Copy code and pate it into a empty Monkey-X project)


Import mojo

Class s
    Field txt:Int
    Method New(val)
        txt = val
    End Method
End Class

Class MyGame Extends App
    Field st:=New Stack<s>

    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
        If KeyHit(KEY_1)
            If st.Length() < 10
                st.Push New s(Rnd(100))
            End If
        End If
        If KeyHit(KEY_2)
            If Not st.IsEmpty()
                st.Pop                        
            End If
        End If
    End Method
    Method OnRender()
        Local h:Int = 0
        Cls(0,0,0)
        SetColor(255,255,255)

        For Local i:=Eachin st
            DrawText "Value in Stack : "+i.txt,10,40+h*10
            h+=1
        Next
        DrawText "Press 1 to add to the stack",10,10
        DrawText "Press 2 to remove from the stack",10,20
        DrawText st.Length()+" items in stack",320,10
    End
End

Function Main()
    New MyGame()
End

Friday, December 26, 2014

Monkey-X - 2D Sidescrolling map code Example.


The code below shows you how to create a 2d sidescrolling map. I did not add a player to keep the code small.

Code Below : (Copy and paste it into a empt project in your Monkey editor)

Import mojo

' The most left and most right tiles do not get drawn.
Global level:Int[][] = [    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
                            [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]

' Level array x location (left of the screen)
Global mapx:Int = 20
' Scrolling offset
Global mapsx:Int = 0

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
        If KeyDown(KEY_LEFT)
            If mapx > 0 Then mapsx+=1
            If mapsx > 31
                mapsx = 0
                If mapx > 0
                    mapx -= 1
                End If
            End If
        End If
        If KeyDown(KEY_RIGHT) 
            If mapx < 29 Then mapsx-=1
            If mapsx < 1
                mapsx = 32
                If mapx < 29
                    mapx += 1
                End If
            End If
        End If
    End Method
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local y=0 Until 10
        For Local x=0 Until 21
                If level[y][x+mapx] = 1
                    DrawRect x*32+mapsx-32,y*32,32,32
                End If
        Next
        Next
        DrawText "mapsx:"+mapsx+" mapx:"+mapx,10,10
        DrawText "Use cursor Left and Right to Scroll the map.",10,20
    End
End

Function Main()
    New MyGame()
End

Monkey-X - String Slicing [0..4] (mid$) code Example


Here some code that shows you how to slice strings. Slicing in Monkey is what mid$ is in Basic. 

Code Below : (Cope and Paste the code in a empty Monkey Project)

Import mojo

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()       
    End Method
    Method OnRender()
        Local test:String="Part of a string."
        Cls(0,0,0)
        SetColor(255,255,255)
        DrawText "Complete string : ",10,10
        DrawText "Sliced part of the String [0..4]: ",10,20
        DrawText test,DeviceWidth*.5,10
        DrawText test[0..4],DeviceWidth*.5,20
    End
End

Function Main()
    New MyGame()
End

Thursday, December 25, 2014

Monkey-X - 2d Fireworks using Lists code example


I tried doing this with stacks but it did not work. I also could not find a lot of examples for stacks so I decided to do this with lists.

Code below :

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

Monkey-x - Space Invaders Moving Alien wave code example


Here is code that shows you how to do a part of the game space invaders. There is a wave of 10*5 aliens that are drawn at the top of the screen and that move from left to right on the screen.

Code below :

Import mojo

Global wavedirection:String = "Right"

Class alien
    Field x:Int
    Field y:Int
    Field alive:Bool
    Field hitcount:Int
    Method New(x1,y1)
        x = x1
        y = y1
        alive = True
        hitcount = 3
    End Method
End Class

Global alienlist:List<alien> = New List<alien>

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        For Local x=0 Until 10
        For Local y=0 Until 5
            alienlist.AddLast(New alien(x*48,y*48))
        End For
        End For
    End Method
    Method OnUpdate()
        For Local i:alien = Eachin alienlist
            If i.x+48 > DeviceWidth Then wavedirection = "Left" ; Exit
            If i.x < 0 Then wavedirection = "Right" ; Exit
        End For
        For Local i:alien = Eachin alienlist
            If wavedirection = "Left"
                i.x -= 1
            End If
            If wavedirection = "Right"
                i.x += 1
            End If
        End For
    End Method
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local i:alien = Eachin alienlist
            DrawRect i.x+8,i.y+8,32,32
        End For
    End
End

Function Main()
    New MyGame()
End

Monkey - CircleOverlap x1,y1,r1,x2,y2,r2 function code example


Below a example of how to do circle to circle collision. There is a function in the code that does it. You can copy it out and paste it in your own code.

Code below :

Import mojo

Class bubble
    Field x:Float
    Field y:Float
    Field incx:Float
    Field incy:Float
    Method New()
        x = Rnd(640)
        y = Rnd(480)
        incx = Rnd(-1,1)
        incy = Rnd(-1,1)
    End
End

Global blist:List<bubble> = New List<bubble>

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        For Local i:Int = 0 Until 10
            blist.AddLast(New bubble)
        End
    End
    Method OnUpdate()
        For Local i:bubble = Eachin blist
            i.x+=i.incx
            i.y+=i.incy
            If i.x<0 Then i.x = 640
            If i.x>640 Then i.x = 0
            If i.y<0 Then i.y = 480
            If i.y>480 Then i.y = 0
        End
        For Local i:bubble = Eachin blist
        For Local ii:bubble = Eachin blist
            If i<>ii
            If circleoverlap(i.x,i.y,10,ii.x,ii.y,10) = True
                blist.Remove ii
            End
            End
        End
        End

    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local i:bubble = Eachin blist
            DrawCircle i.x,i.y,10
        End
    End
End

Function Main()
    New MyGame()
End

Function circleoverlap:Bool(x1:Int,y1:Int,r1:Int,x2:Int,y2:Int,r2:Int)
    Local dx:Int = x1-x2
    Local dy:Int = y1-y2
    Local r:Int = r1+r2
    If dx*dx+dy*dy <= r*r Then Return True Else Return False
End

Wednesday, December 24, 2014

Monkey - 2D Player jumping code example


The code below is an example where a rectangle can jump with gravity. The space bar is used to trigger the jump if on the ground.

Code below :

Import mojo

Global px:Float = 320
Global py:Float = 240
Global playerjump:Bool = False
Global pincy:Float = 0

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
        ' If the player is on the ground and the space bar is pressed
        If playerjump = False And KeyDown(KEY_SPACE) = True
            pincy = -3
            playerjump = True
        End
        'If the player is in the jump
        If playerjump = True
            pincy += 0.1
            'if the player is going up
            If pincy <=0
                For Local i:Int = 0 Until Abs(pincy)
                    py -= 1
                End
            End
            ' if the player if going down
            If pincy > 0
                For Local i:Int = 0 Until pincy
                    py += 1
                    'if the player touches the ground
                    If py > 240 Then 
                        playerjump = False
                        py = 240
                        Exit
                    End
                End
            End
        End
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        DrawRect px,py,32,32
        DrawText "Press space bar to jump the rectangle player",10,10
    End
End

Function Main()
    New MyGame()
End

Monkey - Asteroid field with Class and List code example.


Import mojo

Class asteroid
    Field x:Float
    Field y:Float
    Field incx:Float
    Field incy:Float
    Method New()
        x = Rnd(640)
        y = Rnd(480)
        incx = Rnd(-1,1)
        incy = Rnd(-1,1)
    End
End

Global mylist:List<asteroid> = New List<asteroid>


Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        For Local i:Int = 0 Until 10
            mylist.AddLast(New asteroid)
        End

    End
    Method OnUpdate()
        ' if outside the window then go back into the window from the other side
        For Local i:asteroid = Eachin mylist
            i.x+=i.incx
            i.y+=i.incy
            If i.x<0 Then i.x = 640
            If i.x>640 Then i.x = 0
            If i.y<0 Then i.y = 480
            If i.y>480 Then i.y = 0
        End
        For Local i:asteroid = Eachin mylist
        For Local ii:asteroid = Eachin mylist
            If i<>ii
            If rectsoverlap(i.x,i.y,10,10,ii.x,ii.y,10,10) = 1
                mylist.Remove ii
            End
            end
        End
        End
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local i:asteroid = Eachin mylist
            DrawRect i.x,i.y,10,10
        End
    End
End

Function Main()
    New MyGame()
End

Function rectsoverlap:Int(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
    If x1 > (x2 + w2) Or (x1 + w1) < x2 Then Return False
    If y1 > (y2 + h2) Or (y1 + h1) < y2 Then Return False
    Return True
End

Monkey - Player Class and drawing player class variables to the screen code example.


Import mojo

Class player
    Field x:Int = 10
    Field y:Int = 20
    Field lives:Int = 3
End

Global p:player = New player

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)        
    End
    Method OnUpdate()
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        DrawText "Player x :" + p.x,10,10
        DrawText "Player y :" + p.y,10,20
        DrawText "Player Lives : " + p.lives,10,30
    End
End

Function Main()
    New MyGame()
End

Monkey - Drawing a rectangle below the mouse pointer code example.


The code below shows you how to draw a rectangle below the mouse. MouseX and MouseY is used for this.

Code below :

Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        DrawRect(MouseX(),MouseY(),16,16)
        DrawText("Move the mouse across the window.",10,10)
    End
End

Function Main()
    New MyGame()
End

Monkey 2d topdown moving the player with the cursor keys code example


The code below draws a rectangle that can be moved with the cursor keys. The rectangle can not get outside of the screen.

Code below :

Import mojo

Global mleft:Bool = False
Global mright:Bool = False
Global mup:Bool = False
Global mdown:Bool = False
Global px:Int = 320
Global py:Int = 220

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
        If KeyDown(KEY_LEFT) Then mleft = True Else mleft = False
        If KeyDown(KEY_RIGHT) Then mright = True Else mright = False
        If KeyDown(KEY_UP) Then mup = True Else mup = False
        If KeyDown(KEY_DOWN) Then mdown = True Else mdown = False
        If mleft And px > 0 Then px-=1
        If mright And px <640-16 Then px+=1
        If mup And py > 0 Then py-=1
        If mdown And py <480-16 Then py+=1
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        DrawRect(px,py,16,16)
        DrawText("Use cursors to move player",10,10)
    End
End

Function Main()
    New MyGame()
End

Monkey getting started - Setting up and drawing a tilemap code example.


Below sourcecode on how to create a tilemap in Monkey. The map is drawn on the screen in the OnRender method.

Code below:


Import mojo


Global layer1:Int[][] = [    [1,1,1,1,1,1,1,1,1,1],
                            [1,0,0,0,0,0,0,0,0,1],
                            [1,0,0,0,0,0,0,0,0,1],
                            [1,0,0,0,0,0,1,1,1,1],
                            [1,0,0,0,0,0,0,0,0,1],
                            [1,0,0,0,0,0,0,0,0,1],
                            [1,0,0,0,0,0,0,0,0,1],
                            [1,1,1,1,0,0,0,0,0,1],
                            [1,0,0,0,0,0,0,0,0,1],
                            [1,1,1,1,1,1,1,1,1,1] ]
Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local y:Int = 0 Until 10
        For Local x:Int = 0 Until 10
            If layer1[y][x] = 1 Then DrawRect(x*32,y*32,32,32)
        End
        End
        DrawText("Tilemap Example",640-200,10)
    End
End

Function Main()
    New MyGame()
End

Monkey getting started - Drawing and updating a 2d starfield code example


This is a good example to learn coding. A 2d starfield. In the code you set up the stars on random locations on the screen. In the update section you loop through each star and move it a step to the left. When it is outside of the screen you place the star beack to the right side of the screen.

Code below :

Import mojo

Global starx:Float[100]
Global stary:Float[100]
Global stars:Float[100]

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        For Local i:Int = 0 Until 100
            starx[i] = Rnd(640)
            stary[i] = Rnd(480)
            stars[i] = Rnd(3)+1
        End
    End
    Method OnUpdate()
        For Local i:Int = 0 Until 100
            starx[i] -= stars[i]
            If starx[i] < 0 Then starx[i] = 640
        End
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local i:Int = 0 Until 100
            DrawRect(starx[i],stary[i],1,1)
        End
    End
End

Function Main()
    New MyGame()
End

Monkey getting started - Random rectangles using for loop example code


The code below fills a section of the screen with red rectangles. The rectangles only get drawn when the random value is 1. Two for loops are used.

Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,0,0)
        For Local y:Int = 3 To 10
        For Local x:Int = 3 To 10
            If Int(Rnd(2)) = 1 Then DrawRect(x*16,y*16,16,16)
        End
        End
    End
End

Function Main()
    New MyGame()
End

Monkey getting started - Drawing a red rectangle which moves code example


The code below shows you how to draw a rectangle to the screen. The rectangle moves up and down.

Code below : (paste it into the monkey editor and run it)

Import mojo

Global x:Int = 320
Global y:Int = 100
Global inc:Int = 1

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
        y+=inc
        If y>200 Then inc=-1
        If y<100 Then inc=1
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,0,0)
        DrawRect(x,y,16,16)
        
    End
End

Function Main()
    New MyGame()
End

Monkey Getting started - Cls example code (clearing the screen with a color)


I figured out how to create a simple graphical window. This is the code I got for it.

Code below :

Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnRender()
        Cls(100,100,100)
    End
End

Function Main()
    New MyGame()
End

Monkey getting started - simple Rectangle Overlap example code


Function Main:Int()
    If rectsoverlap(10,10,10,10,12,12,10,10) = False Then
    Print("No Collision")
    Else
    Print("Collision")
    End If
    Return 0
End Function

Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
    If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False
    If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False
    Return True
End

Monkey getting started - Printing Constant variables example code

I made this example while watching the youtube tutorials for monkey.


This is the outputted result :
10
1.1
hello

This is the code :

Const con1:Int = 10
Const con2:Float = 1.1
Const con3:String = "hello"

Function Main:Int()
    Print(con1)
    Print(con2)
    Print(con3)
    Return 0
End Function

Monkey getting started - Printing Global variables Example code


I am following the youtube tutorials for beginners for monkey and then I made this.

Code below ( Copy and paste it into notepad and then copy paste it into the mokey editor and run it.

This is the outputted result. As you can see the floating value is not what you might of expected,

100
1.1000000238418579
Hello


Global val1:Int = 100
Global val2:Float = 1.1
Global txt1:String = "Hello"

Function Main:Int()
    Print(val1)
    Print(val2)
    Print(txt1)
    Return 0
End Function

Monkey getting started - Adding up value through function example code




I started doing some programming in Monkey. I have not programmed in it a lot before so I need to learn a lot. There is not a lot of example code available afaik so I will put my own code on a blog so I can let others learn from it and so that I can get back into it when I have not done anything in it before. It is also a good way to backup code.

The code below shows a function that adds up two values. In the main function the value is printed to the debug screen. The result 20 will be shown.

afaik the code works both as compiled with html5 mode or desktop mode.

Function add:Int(val1:Int,val2:Int)
    Return val1+val2
End Function

Function Main:Int()
    Print(add(10,10))
    Return 0
End Function