Thursday, December 15, 2016

Monkey-X - Example - Recursive Backtracking 2d Maze - code example


Import mojo

Class maze
    Field sw:Int,sh:Int
    Field mw:Int,mh:Int
    Field tmw:Int,tmh:Int
    Field tw:Float,th:Float
    Field mazex:Stack<Int> = New Stack<Int>
    Field mazey:Stack<Int> = New Stack<Int>    
    Field tilemap:Int[][]
    Field map:Int[][]
    Field map2:Int[][][]
    Method New(sw:Int,sh:Int,mw:Int,mh:Int)
        Self.sw = sw
        Self.sh = sh
        Self.mw = mw
        Self.mh = mh    
        tmw = (mw*2)+1
        tmh = (mh*2)+1
        tw = Float(sw)/((Float(tmw)))
        th = Float(sh)/((Float(tmh)))
        map2 = New Int[mw][][]
        map = New Int[mw][]
        tilemap = New Int[tmw][]
        For Local i = 0 Until mw
            map[i] = New Int[mh]
            map2[i] = New Int[mh][]
            For Local z = 0 Until mh
                map2[i][z] = New Int[4]
            Next
        Next
        For Local i = 0 Until tmw
            tilemap[i] = New Int[tmh]
        Next    
        makemaze()
    End Method 
    Method makemaze()
        Local ax:Int[] = [0,1,0,-1]
        Local ay:Int[] = [-1,0,1,0]
        mazex.Push(Rnd(mw))
        mazey.Push(Rnd(mh))                                
        While mazex.IsEmpty = False            
            Local x:Int=mazex.Top
            Local y:Int=mazey.Top
            Local d:Int[] = New Int[4]
            Local deadend:Bool=True
            For Local i:=0 Until ax.Length
                Local x2:Int=x+ax[i]
                Local y2:Int=y+ay[i]
                If x2>=0 And x2<mw And y2>=0 And y2<mh
                    If map[x2][y2] = 0 
                        d[i] = 1
                        deadend=False
                    End If
                End If
            Next
            
            If deadend = False                
                Local eloop:Bool=False
                While eloop = False
                    Local r:Int=Rnd(0,4)
                    If d[r] = 1 Then                        
                        eloop = True
                        Local nx:Int=x+ax[r]
                        Local ny:Int=y+ay[r]
                        map2[x][y][r] = 1                                                                        
                        mazex.Push(nx)
                        mazey.Push(ny)                        
                        map[x][y] = 1
                        map[nx][ny] = 1
                    End If
                Wend
            Else    ' if nothing happened than backtrace
                mazex.Pop()
                mazey.Pop()
            End If
        Wend        
        ' convert the map into a tilemap
        For Local y:=0 Until mh
        For Local x:=0 Until mw
            Local x2:Int=x*2
            Local y2:Int=y*2
            If map[x][y] = 1 Then tilemap[x2+1][y2+1] = 1
            If map2[x][y][0] = 1 Then tilemap[x2+1][y2] = 1
            If map2[x][y][1] = 1 Then tilemap[x2+2][y2+1] = 1
            If map2[x][y][2] = 1 Then tilemap[x2+1][y2+2] = 1
            If map2[x][y][3] = 1 Then tilemap[x2][y2+1] = 1            
        Next
        Next
    End Method
    Method draw()
        Cls 0,0,0
        SetColor 200,100,0
        For Local y:Float=0 Until tmh Step 1
        For Local x:Float=0 Until tmw Step 1
            Local x2:Float=(x*tw)
            Local y2:Float=(y*th)
            If tilemap[x][y] = 1 Then 
                DrawRect(x2,y2,tw,th)
            End If
        Next
        Next    
    End Method    
End Class

Global mymaze:maze

Class MyGame Extends App
    Field cnt:Int=500
    Method New()                
    End Method
    
    Method OnRender()        
        cnt+=1
        '
        If cnt>300 Or (KeyDown(KEY_SPACE) And cnt>20) Or (MouseDown(MOUSE_LEFT) And cnt>20 )Then
            cnt=0
            Seed = Millisecs()
            Local s:Int=Rnd(10,64)                
            mymaze = New maze(DeviceWidth,DeviceHeight,s,s)
        End If
        '
        mymaze.draw()
        '
        SetColor 255,255,255
        DrawText("Press space or tap for new maze",0,0)
    End Method    
    
End    Class


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - SetAlpha and DrawRect - code example


Import mojo

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        SetColor Rnd(255),0,0
        SetAlpha(Rnd(1))
        Local x:=Rnd(DeviceWidth)
        Local y:=Rnd(DeviceHeight)
        Local w:=Rnd(10,100)
        Local h:=Rnd(10,100)
        DrawRect(x,y,w,h)
        
        
    End Method
End Class


Function Main()
    New MyGame()
End function

Thursday, July 28, 2016

Monkey-X - Dungeon/Cave Generator Rectangles - code example


Import mojo

Class tilemap    
    Field tilew:Int
    Field tileh:Int
    Field width:Int
    Field height:Int
    Field map:Int[][]
    Method New(width:Int,height:Int,tilew:Int,tileh:Int)
        Self.width=width
        Self.height=height
        Self.tilew=tilew
        Self.tileh=tileh
        map = New Int[width][]
        For Local i=0 Until width
            map[i] = New Int[height]
        Next
        makemap
        settiles
    End Method
    ' Here we create the walls
    Method settiles()
        'create walls
        For Local y=1 Until height-1
        For Local x=1 Until width-1
            Local t:Int=map[x][y]
            If t=0 And map[x+1][y] = 1 Then map[x][y] = 2
            If t=0 And map[x][y+1] = 1 Then map[x][y] = 2
            If t=0 And map[x-1][y] = 1 Then map[x][y] = 2
            If t=0 And map[x][y-1] = 1 Then map[x][y] = 2
        Next
        Next
        ' get corners
        For Local y=1 Until height-1
        For Local x=1 Until width-1
            Local t:Int=map[x][y]
            If t=0 And map[x+1][y] = 2 And map[x][y+1] = 2 Then map[x][y] = 3
            If t=0 And map[x-1][y] = 2 And map[x][y+1] = 2 Then map[x][y] = 3
            If t=0 And map[x][y-1] = 2 And map[x+1][y] = 2 Then map[x][y] = 3
            If t=0 And map[x-1][y] = 2 And map[x][y-1] = 2 Then map[x][y] = 3
        Next
        Next
        'set corners to value 2
        For Local y=1 Until height-1
        For Local x=1 Until width-1
            If map[x][y] = 3 Then map[x][y] = 2
        Next
        Next
    End Method
    Method makemap()
        'here we make random rectangles and see if they 
        'are placed next to another rectangle.
        'This will grow a random level...
        Local w1:Int=Rnd(1,4)
        Local h1:Int=Rnd(1,4)
        Local x1:Int=width/2
        Local y1:Int=height/2
        
        drawmaprect(x1,y1,w1,h1)
    
    
        Local maxrooms:Int=(width*height)/30
        Local numrooms:Int=0
        Local cnt:Int=0
        While numrooms < maxrooms And cnt<15000
            cnt+=1
            Local done:Bool=False
            w1=Rnd(1,4)
            h1=Rnd(1,4)
            x1=Rnd(3,width-5)
            y1=Rnd(3,height-5)
            'check right
            If map[x1][y1] = 1 And map[x1+1][y1] = 0 And map[x1+1][y1-2] = 0 And map[x1+1][y1+2] = 0 
                drawmaprect(x1,y1,w1,h1)
                numrooms+=1
                done=True
            End If
            'check bottom
            If done=False And map[x1][y1] = 1 And map[x1][y1+1] = 0 And map[x1-2][y1+1] = 0 And map[x1+2][y1+1] = 0
                drawmaprect(x1,y1,w1,h1)
                numrooms+=1
                done=True
            End If
            'check top
            If done=False And y1+h1<height-1
                If map[x1][y1+h1+1] = 1 And map[x1][y1+h1] = 0 And map[x1-2][y1+h1] = 0  And map[x1+2][y1+h1] = 0
                    drawmaprect(x1,y1,w1,h1)
                    numrooms+=1
                    done=True
                End If
            End If
            'check left
            If done=False And x1+w1<width-1
                If map[x1+w1+1][y1] = 1 And map[x1+w1][y1] = 0 And map[x1+w1][y1-2] = 0 And map[x1+w1][y1+2] = 0
                    drawmaprect(x1,y1,w1,h1)
                    numrooms+=1                    
                End If
            End If
        Wend

    End Method
    Method drawmaprect(x:Int,y:Int,w:Int,h:Int)
        For Local y1=y To y+h
        For Local x1=x To x+w
            If x1>-1 And y1>-1 And x1<width And y1<height
                map[x1][y1] = 1
            End If
        Next
        Next
    End Method
    Method draw()
        SetColor 255,255,255
        For Local y=0 Until height
        For Local x=0 Until width
            Local t:Int=map[x][y]
            Select t
                Case 1 'wall
                SetColor 100,100,100
                DrawRect x*tilew,y*tileh,tilew,tileh
                Case 2 'wall
                SetColor 200,200,200
                DrawRect x*tilew,y*tileh,tilew,tileh                
            End Select            
        Next
        Next
    End Method
End Class

Global m:tilemap

Class MyGame Extends App

    Method OnCreate()
        Seed = GetDate[5]
        SetUpdateRate(60)
        createrandommap()
    End Method
    Method OnUpdate()  
        If KeyHit(KEY_SPACE) Or MouseHit(MOUSE_LEFT)
            createrandommap()
        End If      
    End Method
    Method OnRender()
        Cls 0,0,0 
        m.draw
        SetColor 255,255,255
        DrawText "Press Space or Tab to create new level",0,0
    End Method
End Class

Function createrandommap()
    Local r:Int=Rnd(0,3)
    Local w:Int,h:Int,tw:Int,th:Int
    Select r
    Case 0 
        w=20;h=15;tw=32;th=32
    Case 1 
        w=20*2;h=15*2;tw=16;th=16
    Case 2 
        w=20*4;h=15*4;tw=8;th=8
    End Select
    m = New tilemap(w,h,tw,th)
End Function

Function Main()
    New MyGame()
End Function

Wednesday, July 20, 2016

Monkey-X - Grey Rnd Poly grid backgrounds - code example


Import mojo

Class gridbackground
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Field mapdepth:Int
    Field mapx:Float[][]
    Field mapy:Float[][]
    Method New(mapwidth:Int,mapheight:Int)
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        tilewidth = DeviceWidth()/Float(mapwidth)
        tileheight = DeviceHeight()/Float(mapheight)
        mapx = New Float[mapwidth][]
        mapy = New Float[mapwidth][]
        For Local i = 0 Until mapwidth
            mapx[i] = New Float[mapheight]
            mapy[i] = New float[mapheight]
        Next 
        makegrid    
    End Method
    Method makegrid()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            mapx[x][y] = x*tilewidth + Rnd(-tilewidth/3,tilewidth/3)
            mapy[x][y] = y*tileheight + Rnd(-tileheight/3,tileheight/3)
        Next
        Next
    End Method
    Method draw()
        For Local y=0 Until mapheight-1
        For Local x=0 Until mapwidth-1
            Local pol:Float[8]
            pol[0] = mapx[x][y]
            pol[1] = mapy[x][y]
            pol[2] = mapx[x+1][y]
            pol[3] = mapy[x+1][y]
            pol[4] = mapx[x+1][y+1]
            pol[5] = mapy[x+1][y+1]
            pol[6] = mapx[x][y+1]
            pol[7] = mapy[x][y+1]
            Local a:Int=Rnd(0,256)
            SetColor a,a,a
            DrawPoly pol
        Next
        Next    
    End Method
End Class

Global mygbg:gridbackground

Class MyGame Extends App
    
    Method OnCreate()
        SetUpdateRate(1)
        mygbg = New gridbackground(25,25)
    End Method
    Method OnUpdate() 
        mygbg = New gridbackground(Rnd(10,30),Rnd(10,30))       
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        mygbg.draw
    End Method
End Class


Function Main()
    New MyGame()
End Function

Tuesday, July 19, 2016

Monkey-X - Beginners - Sorting IntLists - code example


Import mojo

Class MyGame Extends App

    Field mylist1:List<Int> = New IntList
    Field mylist2:List<Int> = New IntList
    
    Method OnCreate()
        SetUpdateRate(60)
        mylist1.AddLast(10)
        mylist1.AddLast(50)
        mylist1.AddLast(1)                
        mylist1.AddLast(31)
        mylist1.AddLast(93)
        mylist1.AddLast(12)
        'sort low>hight
        mylist1.Sort()
        'list 2
        mylist2.AddLast(10)
        mylist2.AddLast(50)
        mylist2.AddLast(1)                
        mylist2.AddLast(31)
        mylist2.AddLast(93)
        mylist2.AddLast(12)
        'sort high>low
        mylist2.Sort(False)

    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "Sorting Lists",0,0
        Local cnt:Int=0
        For Local i:=Eachin mylist1
            DrawText i,10,cnt*10+50
            cnt+=1
        Next
        cnt=0
        For Local i:=Eachin mylist2
            DrawText i,200,cnt*10+50
            cnt+=1
        Next
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Sorting StringLists - code example


Import mojo

Class MyGame Extends App

    Field mylist1:List<String> = New StringList
    Field mylist2:List<String> = New StringList
    
    Method OnCreate()
        SetUpdateRate(60)
        mylist1.AddLast("hello")
        mylist1.AddLast("this")
        mylist1.AddLast("is")                
        mylist1.AddLast("a")
        mylist1.AddLast("test")
        mylist1.AddLast("mkay")
        'sort alphabetically
        mylist1.Sort()
        'list 2
        mylist2.AddLast("hello")
        mylist2.AddLast("this")
        mylist2.AddLast("is")                
        mylist2.AddLast("a")
        mylist2.AddLast("test")
        mylist2.AddLast("mkay")
        'sort reversed alhabetically
        mylist2.Sort(False)

    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "Sorting Lists",0,0
        Local cnt:Int=0
        For Local i:=Eachin mylist1
            DrawText i,10,cnt*10+50
            cnt+=1
        Next
        cnt=0
        For Local i:=Eachin mylist2
            DrawText i,200,cnt*10+50
            cnt+=1
        Next
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monday, July 18, 2016

Monkey-X - Beginners - SetScissor - 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
        DrawText "SetScissor Example (x,y,w,h)",10,10
        ' SetScissor x,y,w,h
        SetScissor(50,50,640-100,480-100)
        ' Here we draw a image on the entire screen
        ' and that will only be drawn inside the 
        ' scissor area.
        SetColor 50,20,0
        Local cx:Float=320
           Local cy:Float=240
        For Local angle=0 Until 360-10 Step 32
            Local x1:Float=cx+Cos(angle)*640
            Local y1:Float=cy+Sin(angle)*480
            Local x2:Float=cx+Cos(angle+10)*640
            Local y2:Float=cy+Sin(angle+10)*480
            DrawPoly([cx,cy,x1,y1,x2,y2])
        Next
        
    End Method
End Class


Function Main()
    New MyGame()
End function