Monday, February 20, 2017

Monkey-X - Wall Tracing on Random Maps - code example


' Wall Tracing from the book Ai For Game Developers.
' Random Maps based on code from Rogue Basin.
'

Import mojo

'
'this is the wall tracing entity
Class entity
    Field tilex:Int,tiley:Int
    Field direction:Int=8
    Field speed:Int
    Method New()
        findstartposition
    End Method
    Method update()
        walltrace
    End Method
    Method walltrace()
        If direction = 4
            If mymap.map[tilex-1][tiley] = 1
                tilex -= 1
                direction = 2
            Elseif mymap.map[tilex][tiley+1] = 1
                tiley += 1
                direction = 4
            Elseif mymap.map[tilex+1][tiley] = 1
                tilex += 1
                direction = 6
            Elseif mymap.map[tilex][tiley-1] = 1
                tiley -= 1
                direction = 8
            End If
        Elseif direction = 6
            If mymap.map[tilex][tiley+1] = 1
                tiley += 1
                direction = 4
            Elseif mymap.map[tilex+1][tiley] = 1
                tilex += 1
                direction = 6
            Elseif mymap.map[tilex][tiley-1] = 1
                tiley -= 1
                direction = 8
            Elseif mymap.map[tilex-1][tiley] = 1
                tilex -= 1
                direction = 2
            End If
        Elseif direction = 8
            If mymap.map[tilex+1][tiley] = 1
                tilex += 1
                direction = 6
            Elseif mymap.map[tilex][tiley-1] = 1
                tiley -= 1
                direction = 8
            Elseif mymap.map[tilex-1][tiley] = 1
                tilex -= 1
                direction = 2
            Elseif mymap.map[tilex][tiley+1] = 1
                tiley += 1
                direction = 4
            End If
        Elseif direction = 2
            If mymap.map[tilex][tiley-1] = 1
                tiley -= 1
                direction = 8
            Elseif mymap.map[tilex-1][tiley] = 1
                tilex -= 1
                direction = 2
            Elseif mymap.map[tilex][tiley+1] = 1
                tiley += 1
                direction = 4
            Elseif mymap.map[tilex+1][tiley] = 1
                tilex += 1
                direction = 6
            End If
        End If


    End Method
    Method findstartposition()
        ' for tracing the wall we need to be sure
        ' that the entity is spawned against a wall
        ' here we check for a random pos on the map
        ' on the floor(1) with a wall(2) at y+1
        Local exitloop:Bool=False
        While exitloop = False
            Local x:Int=Rnd(3,mymap.mapwidth-3)
            Local y:Int=Rnd(3,mymap.mapheight-3)
            If mymap.map[x][y] = 1
            If mymap.map[x][y+1] = 2 
                exitloop = True
                tilex = x
                tiley = y
            End If
            End If
        Wend
    End Method    
    Method draw()
        SetColor 255,0,0
        DrawOval     tilex*mymap.tilewidth,
                    tiley*mymap.tileheight,
                    mymap.tilewidth+1,
                    mymap.tileheight+1
    End Method
End Class

Class map
    Field tilewidth:Float
    Field tileheight:Float
    Field mapwidth:Int
    Field mapheight:Int
    Field screenwidth:Int
    Field screenheight:Int
    Field map:Int[][]
    Method New(    screenwidth:Int,
                screenheight:Int,
                mapwidth:Int,
                mapheight:Int)
        Self.screenwidth = screenwidth
        Self.screenheight = screenheight
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        Self.tilewidth = Float(screenwidth)/Float(mapwidth)
        Self.tileheight = Float(screenheight)/Float(mapheight)
        map = New Int[mapwidth][]
        For Local i=0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        map[mapwidth/2][mapheight/2] = 3 ' 3 is a door
        makemap
    End Method
    Method makemap()
        Local timeout:Int
        While timeout<(mapwidth*mapheight)*20
            timeout+=1
            Local x:Int=Rnd(11,mapwidth-11)
            Local y:Int=Rnd(11,mapheight-11)
            If map[x][y] = 3
                makeroom(x,y)
            End If
        Wend    
        'here we turn doors into walls
        'if they should be walls
        For Local y1=1 Until mapheight-1
        For Local x1=1 Until mapwidth-1
            If map[x1][y1] = 3
            Local cnt:Int=0
            For Local y2=y1-1 To y1+1
            For Local x2=x1-1 To x1+1
                If map[x2][y2] = 2 Then cnt+=1
            Next
            Next
            If cnt>3 Then map[x1][y1] = 2
            End If
        Next
        Next
        'here we turn doors into walls if they
        ' touch tiles that are nothing (0)
        For Local y1=1 Until mapheight-1
        For Local x1=1 Until mapwidth-1
            If map[x1][y1] = 3
            Local cnt:Int=0
            For Local y2=y1-1 To y1+1
            For Local x2=x1-1 To x1+1
                If map[x2][y2] = 0 Then cnt+=1
            Next
            Next
            If cnt>0 Then map[x1][y1] = 2
            End If
        Next
        Next        
        'here we turn the doors into floors
        For Local y1=0 Until mapheight
        For Local x1=0 Until mapwidth
            If map[x1][y1] = 3 Then map[x1][y1] = 1
        Next
        Next
    End Method
    Method makeroom(x:Int,y:Int)
        Local side:String
        If map[x][y-1] = 0
            side="up"
        Elseif map[x+1][y] = 0
            side="right"
        Elseif map[x][y+1] = 0
            side="down"
        Elseif map[x-1][y] = 0
            side="left"
        End If        
        Local w:Int=Rnd(5,10)
        Local h:Int=Rnd(5,10)
        If side="up"
            Local x1:Int=x-w/2
            Local y1:Int=y-h
            If roomfits(x1,y1,w,h)
                insertroom(x1,y1,w,h+1)
                'door up
                map[x1+Rnd(2,w-2)][y1] = 3
                ' door right
                map[x1+w-1][y1+Rnd(2,h-2)] = 3
                'door left
                map[x1][y1+Rnd(2,h-2)] = 3
            End If                        
            
        End If
        If side="right"
            Local x1:Int=x+1
            Local y1:Int=y-h/2
            If roomfits(x1,y1,w,h)
                insertroom(x1-1,y1,w,h)
                'door up
                map[x1+Rnd(2,w-2)][y1] = 3
                'door down
                map[x1+Rnd(2,w-2)][y1+h-1] = 3
                ' door right
                map[x1+w-2][y1+Rnd(2,h-2)] = 3                
            End If
        End If
        If side="left"
            Local x1:Int=x-w
            Local y1:Int=y-h/2
            If roomfits(x1,y1,w,h)
                insertroom(x1,y1,w+1,h)
                'door up
                map[x1+Rnd(2,w-2)][y1] = 3
                'door down
                map[x1+Rnd(2,w-2)][y1+h-1] = 3                
                'door left
                map[x1][y1+Rnd(2,h-2)] = 3

            End If            
        End If
        If side="down"
            Local x1:Int=x-w/2
            Local y1:Int=y+1
            If roomfits(x1,y1,w,h)
                insertroom(x1,y1-1,w,h)
                'door down
                map[x1+Rnd(2,w-2)][y1+h-2] = 3                
                'door left
                map[x1][y1+Rnd(2,h-2)] = 3                
                ' door right
                map[x1+w-1][y1+Rnd(2,h-2)] = 3                

            End If                        
        End If
    End Method
    Method insertroom(x,y,w,h)
        For Local y2=y Until y+h
        For Local x2=x Until x+w
            If map[x2][y2] <> 3 Then map[x2][y2] = 2
        Next
        Next

        For Local y2=y+1 Until y+h-1
        For Local x2=x+1 Until x+w-1
            map[x2][y2] = 1
        Next
        Next
    End Method
    Method roomfits(x:Int,y:Int,w:Int,h:Int)
        For Local y1=y Until y+h
        For Local x1=x Until x+w
            If map[x1][y1] = 1 Then Return False
        Next
        Next
        Return True
    End Method
    Method draw()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            Select map[x][y]
                Case 0
                Case 1'floor
                SetColor 100,100,100
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth+1,
                            tileheight+1
                Case 2'wall
                SetColor 200,200,200
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth+1,
                            tileheight+1
                Case 3'wall
                SetColor 244,244,0
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth+1,
                            tileheight+1

            End Select
        Next
        Next
    End Method
End Class

Global mymap:map
Global myentity:List<entity> = New List<entity>

Class MyGame Extends App
    Field cnt:Int
    Method OnCreate()
        SetUpdateRate(5)
        Seed = GetDate[4]+GetDate[5]
        newmap
    End Method
    Method OnUpdate()    
        cnt+=1
        For Local i:=Eachin myentity
            i.update
        Next
        If KeyHit(KEY_SPACE) Or cnt>200
            cnt=0
            newmap
        End If    
    End Method
    Method OnRender()
        Cls 0,0,0 
        mymap.draw
        For Local i:=Eachin myentity
            i.draw
        Next
        SetColor 255,255,255
        DrawText "Monkey-X - Wall Tracing on Random Maps",10,10
    End Method
End Class


Function Main()
    New MyGame()
End Function


Function newmap()
    Local s:Int=Rnd(40,100)
    mymap = New map(DeviceWidth,DeviceHeight,s,s)
    myentity = New List<entity>
    For Local i=0 Until Rnd(1,s/3)
        myentity.AddLast(New entity())
    Next
End Function

Monkey-X - Path Following - code example


' Path following - from the book - ai for game developers
Import mojo

Class entity
    Field tilex:Int
    Field tiley:Int
    Field entwidth:Int
    Field entheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Field mapwidth:Int
    Field mapheight:Int    
    Field direction:Int=1 ' 1 is up, 2 is upright ect clockwise
    Method New()
        mapwidth = mypathfollowing.mapwidth
        mapheight = mypathfollowing.mapheight
        entwidth = mypathfollowing.tilewidth
        entheight = mypathfollowing.tileheight
        tilewidth = mypathfollowing.tilewidth
        tileheight = mypathfollowing.tileheight
        findstartpos        
    End Method
    Method update()
        ' terrain analysis
        ' here we check clockwise around the entity its position
        ' and fill the ta(terrainanalysis) array with 0 if there is no read there
        ' and 10 if there is a road
        Local ta:Int[] = New Int[9]
        ta[1] = mypathfollowing.map[tiley-1][tilex]
        ta[2] = mypathfollowing.map[tiley-1][tilex+1]
        ta[3] = mypathfollowing.map[tiley][tilex+1]
        ta[4] = mypathfollowing.map[tiley+1][tilex+1]
        ta[5] = mypathfollowing.map[tiley+1][tilex]
        ta[6] = mypathfollowing.map[tiley+1][tilex-1]
        ta[7] = mypathfollowing.map[tiley][tilex-1]
        ta[8] = mypathfollowing.map[tiley-1][tilex-1]
        For Local i = 1 To 8
            If ta[i] = 0 
                ta[i] = 0
            Else
                ta[i] = 10
            End If
        Next
        ' weighted direction analysis
        ' here we use the current direction and add
        ' up the current direction with 2 and left and
        ' right from there with +1 and the opposite
        ' direction of the entity direction -1        
        If direction = 1
            ta[1] += 2
            ta[2] += 1
            ta[8] += 1
            ta[5] -= 1
        End If
        If direction = 2
            ta[2] += 2
            ta[3] += 1
            ta[1] += 1
            ta[6] -= 1
        End If
        If direction = 3
            ta[3] += 2
            ta[4] += 1
            ta[2] += 1
            ta[7] -= 1
        End If
        If direction = 4
            ta[4] += 2
            ta[5] += 1
            ta[3] += 1
            ta[8] -= 1
        End If
        If direction = 5
            ta[5] += 2
            ta[6] += 1
            ta[4] += 1
            ta[1] -= 1
        End If
        If direction = 6
            ta[6] += 2
            ta[7] += 1
            ta[5] += 1
            ta[2] -= 1
        End If
        If direction = 7
            ta[7] += 2
            ta[8] += 1
            ta[6] += 1
            ta[3] -= 1
        End If
        If direction = 8
            ta[8] += 2
            ta[1] += 1
            ta[7] += 1
            ta[4] -= 1
        End If
        ' choosing direction
        ' here we get the highest weight to get the next direction
        Local maxterrain:Int
        Local maxindex:Int
        For Local i=1 To 8
            If ta[i] > maxterrain
                maxterrain = ta[i]
                maxindex = i
            End If
        Next
        ' update position
        ' here we use the new direction and move the entity and set
        ' the new or last direction
        If maxindex = 1
            direction = 1
            tiley -= 1
        End If
        If maxindex = 2
            direction = 2
            tilex += 1
            tiley -= 1
        End If
        If maxindex = 3
            direction = 3
            tilex += 1
        End If
        If maxindex = 4
            direction = 4
            tilex += 1
            tiley += 1
        End If
        If maxindex = 5
            direction = 5
            tiley += 1
        End If
        If maxindex = 6
            direction = 6
            tilex -= 1
            tiley += 1
        End If
        If maxindex = 7
            direction = 7
            tilex -= 1
        End If
        If maxindex = 8
            direction = 8
            tilex -= 1
            tiley -= 1
        End If
    End Method
    Method findstartpos()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If mypathfollowing.map[y][x] = 1
                tilex = x
                tiley = y
                Return
            End If
        Next
        Next
    End Method
    Method draw()
        SetColor 255,255,0
        DrawRect tilex*tilewidth,tiley*tileheight,entwidth,entheight
    End Method
End Class

Class pathfollowing
Global map: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,1,1,1,1,1,1,0,0,0,0,0,0,0],
[0,0,0,1,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,1,1,1,1,1,1,1,1,0,0],
[0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0],
[0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0],
[0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0],
[0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
[0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
[0,0,0,0,0,1,1,1,1,1,1,1,1,1,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]]    
Field screenwidth:Int
    Field screenheight:Int
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Method New(width:Int,height:Int)
        mapwidth = map.Length
        mapheight = map[0].Length
        screenwidth = width
        screenheight = height
        tilewidth = Float(screenwidth)/Float(mapwidth)
        tileheight = Float(screenheight)/Float(mapheight)
    End Method
    Method draw()
        SetColor 200,100,0
        For Local y = 0 Until mapheight
        For Local x = 0 Until mapwidth
            If map[y][x] = 1
                DrawRect     x*tilewidth,y*tileheight,
                            tilewidth+1,tileheight+1
            End If
        Next
        Next
    End Method
End Class

Global mypathfollowing:pathfollowing
Global myentity:entity

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(5)
        mypathfollowing = New pathfollowing(DeviceWidth,DeviceHeight)
        myentity = New entity()
    End Method
    Method OnUpdate()        
        myentity.update
    End Method
    Method OnRender()
        Cls 0,0,0 
        mypathfollowing.draw
        myentity.draw
        SetColor 255,255,255
        DrawText "Monkey-X - Path Following Example",10,10
    End Method
End Class


Function Main()
    New MyGame()
End Function

Friday, February 10, 2017

Monkey-X - Particle Attraction - code example


Import mojo
Class gravitypoint
    Field x:Int,y:Int
    Method New(x:Int,y:Int)
        Self.x = x
        Self.y = y
    End Method
    Method draw()
        SetColor 255,255,0
        DrawOval x,y,6,6
    End Method
End Class

Class particle
    Field x:Float,y:Float
    Field incx:Float
    Field incy:Float
    Method New(x:Float,y:Float)
        Self.x = x
        Self.y = y 
        incx = Rnd(-1,1)
        incy = Rnd(-1,1)
    End Method
    Method update()
        For Local i:=Eachin mygravitypoint
            Local a:Int=getangle(x,y,i.x,i.y)
            incx+=Cos(a)/2
            incy+=Sin(a)/2
        Next
        x+=incx
        y+=incy
    End Method
    Method draw()
        SetColor 255,255,255
        DrawOval x,y,6,6
    End Method
    Function getangle:Int(x1:Int,y1:Int,x2:Int,y2:Int)
         Local dx = x2 - x1
         Local dy = y2 - y1
         Return ATan2(dy,dx)+360 Mod 360
    End Function      
End Class

Global myparticle:List<particle> = New List<particle>
Global mygravitypoint:List<gravitypoint> = New List<gravitypoint>

Class MyGame Extends App
    Field counter:Int=0
    Method OnCreate()    
        Seed = GetDate[5]+GetDate[4]
        SetUpdateRate(60)
        createscene
    End Method
    Method OnUpdate()  
        counter+=1
        If counter > 300
            counter = 0
            createscene
        End If      
        For Local i:=Eachin myparticle
            i.update
        Next
    End Method
    Method OnRender()
        Cls 0,0,0 
        For Local i:=Eachin myparticle
            i.draw
        Next
        If MouseDown(MOUSE_LEFT)
            For Local i:=Eachin mygravitypoint
                  i.draw
            Next
        End If
        SetAlpha 1
        SetColor 255,255,255
        DrawText "Monkey-X - Particles with 'Attraction' Effect - Thnx to The Coding Train",0,0
        DrawText "Particles get attracted to <Left Mouse Button> points on the map",0,DeviceHeight-20
    End Method
End Class

Function createscene()
    myparticle = New List<particle>
    mygravitypoint = New List<gravitypoint>

    For Local i=0 Until Rnd(10,200)
        myparticle.AddLast(New particle(Rnd(DeviceWidth),Rnd(DeviceHeight)))
    Next
    For Local i=0 Until Rnd(1,20)
        mygravitypoint.AddLast(New gravitypoint(Rnd(DeviceWidth),Rnd(DeviceHeight)))
    Next
End Function

Function Main()
    New MyGame()
End Function

Tuesday, February 7, 2017

Monkey-X - Hill Algorithm - code example

Import mojo

Global screenwidth:Int=640
Global screenheight:Int=480

Class hill
    Field map:Int[][]
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Method New(width:Int=20,height:Int=20)
        mapwidth = width
        mapheight = height
        tilewidth=Float(screenwidth)/Float(mapwidth)
        tileheight=Float(screenheight)/Float(mapheight)
        map = New Int[mapwidth][]
        For Local i=0 Until mapwidth
            map[i] = New Int[mapheight]
        Next        
        makehill
    End Method
    Method makehill()
        ' Here we draw rectangles on the map adding by +1
        ' with the underlaying value.
        '
        Local exitloop:Bool=False
        While exitloop = False
            Local x:Int=Rnd(-mapwidth/7,mapwidth)
            Local y:Int=Rnd(-mapheight/7,mapheight)
            Local w:Int=Rnd(1,mapwidth/5)
            Local h:Int=Rnd(1,mapheight/5)
            If Rnd(2)<1.8
                w=1
                h=1
            End If
            ' if highest map value > 20 then stop drawing
            ' rectangles
            If addrect(x,y,w,h) > 24 Then exitloop = True
        Wend
    End Method
    Method addrect:Int(x:Int,y:Int,w:Int,h:Int)
        ' This function draws the rectangle and
        ' returns the highest value it makes in the map
        '
        Local highestvalue:Int
        For Local y1=y To y+h
        For Local x1=x To x+w
            If x1>=0 And x1<mapwidth And y1>=0 And y1<mapheight
                Local a:Int = map[x1][y1]
                a+=1
                If a>highestvalue Then highestvalue = a
                map[x1][y1] = a 
            End If
        Next
        Next
        Return highestvalue
    End Method
    Method draw()
        ' world map
        ' This function draws the map
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            Local c:Int=map[x][y]*10
            'water (low)
            If map[x][y] > 0 Then SetColor 0,0,100
            If map[x][y] > 5 Then SetColor 0,0,200
            If map[x][y] > 8 Then SetColor 0,0,250
            'grass (higher)
            If map[x][y] >=10 Then SetColor 0,c,0            
            'hills (higher)
            If map[x][y] >=15 Then SetColor c,c/2,0            
            'mountains (highest)
            If map[x][y] >=20 Then SetColor c,c,c                        
            DrawRect Float(x)*tilewidth,Float(y)*tileheight,tilewidth+1,tileheight+1
        Next
        Next
        'heightmap
        SetScissor 320,0,320,480
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            Local c:Int=map[x][y]*10
            SetColor c,c,c
            DrawRect Float(x)*tilewidth,Float(y)*tileheight,tilewidth+1,tileheight+1        
        Next
        Next
        SetScissor 0,0,640,480
    End Method
End Class

Global mymap:hill

Class MyGame Extends App
    Field count:Int
    Method OnCreate()
        Seed = GetDate[5]
        SetUpdateRate(1)
        mymap = New hill(50,50)
    End Method
    Method OnUpdate()        
        count+=1
        If count>2 Then
            count=0
            Local s:Int=Rnd(32,320)
            mymap = New hill(s,s)
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        mymap.draw
        SetColor 255,255,255
        DrawText "Hill Algorithm",0,0
        DrawText "World Map",0,20
        DrawText "Height map",320,20
    End Method
End Class


Function Main()
    New MyGame()
End Function

Friday, February 3, 2017

Monkey-X - Circle Packing - Code Example

Import mojo

Class circle
    Field x:Int,y:Int
    Field radius:Int
    Method New()
        radius = 2
        Local exitloop:Bool=False
        While exitloop = False
            exitloop=True
            Self.x = Rnd(DeviceWidth)
            Self.y = Rnd(DeviceHeight)
            For Local i:=Eachin mycircle
                If i.x = x And i.y = y Then
                    Else
                    If circleoverlap(    i.x,i.y,i.radius,
                                        x,y,2) = True Then
                        exitloop=False
                    End If
                End If
            Next
        Wend
    End Method
    Method grow()
        If x-radius<0 Then Return
        If x+radius>DeviceWidth Then Return
        If y-radius<0 Then Return
        If y+radius>DeviceHeight Then Return
        For Local i:=Eachin mycircle
            If i.x=x And i.y=y Then
            Else
                If circleoverlap(    x,y,radius,
                                    i.x,i.y,i.radius)
                    Return
                End If 
            End If
        Next
        radius+=1
    End Method
    Method draw()
        SetColor 255,255,255
        DrawCircle x,y,radius
        SetColor 0,0,0
        DrawCircle x,y,radius-2
    End Method
    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 Function    
End Class

Global mycircle:List<circle> = New List<circle>

Class MyGame Extends App
    Field counter:Int
    Method OnCreate()
        SetUpdateRate(5)
        mycircle.AddLast(New circle())
    End Method
    Method OnUpdate()   
        If counter>100 Then Return
        For Local i=0 Until 5
            mycircle.AddLast(New circle())
        Next
        For Local i:=Eachin mycircle
            i.grow
        Next
        counter+=1
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        For Local i:=Eachin mycircle
            i.draw
        Next
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Getting Started - Default Parameters - code example

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
        Scale 2,2
        defaultparameter(0,0)
        defaultparameter(0,20,100,200,"Not Default")
    End Method
End Class

' Like with variable declaration you can set a value to 
' a variable in a function or method.
Function defaultparameter(x:Int,y:Int,a:Int=10,b:Int=20,c:String="default")
    DrawText "a="+a+" b="+b+" c="+c,x,y
End Function

Function Main()
    New MyGame()
End Function

Monkey-X - Getting Started - Exit - code example


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
        Scale 2,2
        For Local i=0 To 10
            DrawText "value i = "+i,0,i*10
            If i>=5 Then Exit ' <<<<<<<<<< Exit here on condition
        Next
    End Method
End Class

Function Main()
    New MyGame()
End Function