Sunday, March 22, 2015

Monkey-X - Beginners - Scroll Text - Code example


Here a simple example that shows you how to make a scrolltext.

Code below :

Import mojo

Class scrolltext
    Field x:Int,y:Int
    Field stext:String 'the text
    Field lx:Float 'location in the scroll
    Field scrollwidth:Int 'how many characters drawn to the screen
    Method New()
        stext="                    "
        stext+="This is a scrolltext made in Monkey-X. It is made inside a class. "
        stext+="the text will repeat itself after this text has ended.           "
    End Method
    Method update()
        'increase the scroll position
        lx+=.1
        If lx>=stext.Length Then lx=0
    End Method
    Method draw()
        'get the visible string inside dt and draw that
        Local dt:String=""
        Local sp:Int=lx
        For Local i=0 Until scrollwidth
            dt+=String.FromChar(stext[sp])            
            sp+=1
            If sp>=stext.Length Then sp=0
        Next
        SetColor 255,255,255
        DrawText dt,x,y
    End Method
End Class

Global s:scrolltext = New scrolltext

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        s.scrollwidth=20 'character the scrolltext is wide
        s.x=100
        s.y=100
    End Method
    Method OnUpdate()  
        s.update      
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "ScrollText Example..",0,0
        s.draw
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Screen Wipe method 5 - Circle in/out - code example


Here another screen dissolve method. I saw this one in a Zelda game.

Import mojo

Class map
    Field tilewidth:Int,tileheight:Int
    Field mapwidth:Int,mapheight:Int
    Field wmwidth:Int,wmheight:Int
    Field map:Int[][]
    Field wipemap:Int[][]
    Field wipeactive:Bool=False
    Field wipemode:String
    Field sradius:Int
    Method New(mapwidth:Int,mapheight:Int,tilewidth:Int,tileheight:Int)
        Self.tilewidth = tilewidth
        Self.tileheight = tileheight
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        wmwidth=640/6+6
        wmheight=480/6+6
        map = New Int[mapwidth][]
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            setmap(x,y,Rnd(0,5))
        Next
        Next
        wipemap = New Int[wmwidth][]
        For Local i=0 Until wmwidth
            wipemap[i] = New Int[wmheight]
        Next
    End Method    
    Method update()
        If wipemode="wipein"            
            mapcircle((wmwidth)/2,(wmheight)/2,sradius,0)
            sradius+=1            
            If sradius>wmheight Then wipemode="wipedone"
        End If
        If wipemode="wipeout"
            For Local y1=0 Until wmheight
            For Local x1=0 Until wmwidth
                wipemap[x1][y1] = 1
            Next
            Next
            sradius-=1
            mapcircle((wmwidth)/2,(wmheight)/2,sradius,0)
            If sradius < 0 Then wipemode="nothing"
        End If
    End Method
    Method initwipein()
        For Local y1=0 Until wmheight
        For Local x1=0 Until wmwidth
            wipemap[x1][y1] = 1
        Next
        Next
        wipemode="wipein"
        sradius=0
    End Method
    Method initwipeout()
        sradius=wmheight
        wipemode="wipeout"
    End Method
    Method setmap:Void(x:Int,y:Int,val:Int)
        If x>=0 And y>=0 And x<mapwidth And y<mapheight
            map[x][y] = val
        End If
    End Method
    Method drawmap:Void()
        If wipemode<>"nothing"
               For Local y1=0 Until mapheight
               For Local x1=0 Until mapwidth
                   drawtile(map[x1][y1],x1*tilewidth,y1*tileheight)
               Next
               Next        
        End If
        If wipemode="wipein"
            SetColor 0,0,0
            For Local y1=0 Until wmheight
            For Local x1=0 Until wmwidth
                If wipemap[x1][y1] = 1
                    DrawRect x1*6,y1*6,6,6
                End If
            Next
            Next        
        End If
        If wipemode="wipeout"
              SetColor 0,0,0
            For Local y1=0 Until wmheight
            For Local x1=0 Until wmwidth
                If wipemap[x1][y1] = 1
                    DrawRect x1*6,y1*6,6,6
                End If
            Next
            Next                      
        End If
    End Method
    Method mapcircle(x1:Int,y1:Int,radius:Int,val:Int)
        For Local y2=-radius Until radius
        For Local x2=-radius Until radius
            If (y2*y2+x2*x2) <= radius*radius+radius*0.8
                Local x3:Int = x1+x2
                Local y3:Int = y1+y2
                If x3>=0 And y3>=0 And x3<wmwidth And y3<wmheight
                    wipemap[x3][y3] = val
                End If
            End If
        Next
        Next
    End Method
    Method drawtile(val:Int,x1:Int,y1:Int)
        Select val
            Case 0'water
                SetColor 0,0,255
                DrawRect x1,y1,tilewidth,tileheight
            Case 1'land
                SetColor 0,200,0
                DrawRect x1,y1,tilewidth,tileheight
            Case 2'forrest
                drawtile(1,x1,y1)
                SetColor 0,255,0
                DrawOval x1+5,y1+5,tilewidth-10,tileheight/2
                SetColor 150,10,0
                DrawRect x1+12,y1+tileheight-10,tilewidth-24,tileheight/2-5
            Case 3'hill
                drawtile(1,x1,y1)
                SetColor 0,255,0
                DrawOval x1+5,y1+10,tilewidth-10,tileheight-15
                SetColor 0,200,0
                DrawRect x1,y1+tileheight/1.5,tilewidth,10
            Case 4'mountain
                drawtile(1,x1,y1)
                SetColor 200,200,200
                DrawPoly(    [Float(x1+tilewidth/2),Float(y1),
                            Float(x1+tilewidth-5),Float(y1+tileheight-5),
                            Float(x1+5),Float(y1+tileheight-5)])
        End Select
    End Method

End Class

Global mymap:map = New map(20,14,32,32)
Global cnt:Int

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        mymap.initwipein
    End Method
    Method OnUpdate()
        mymap.update
        If Rnd(500)<2 And mymap.wipemode="wipedone" 
            mymap.initwipeout
        End If
        If mymap.wipemode="nothing"
            cnt+=1
            If cnt>100 Then
            mymap.initwipein            
            cnt=0
            End If
        End If
    End Method
    Method OnRender()
        Cls 0,0,0
        mymap.drawmap
        SetColor 255,255,255
        DrawText "Screen wipe example (circle) wipemode "+mymap.wipemode,0,0
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Screenwipe 4 - block out/in - code example


Here a screen dissolve that I first saw in a old game called Colonization. It has blocks on the screen that dissapear/appear with time and show/hide the screen behind it.

Code below :

Import mojo

Class map
    Field tilewidth:Int,tileheight:Int
    Field mapwidth:Int,mapheight:Int
    Field map:Int[][]
    Field wipemap:Int[][]
    Field wipeactive:Bool=False
    Field wipemode:String
    Field wipetime:Int
    Method New(mapwidth:Int,mapheight:Int,tilewidth:Int,tileheight:Int)
        Self.tilewidth = tilewidth
        Self.tileheight = tileheight
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        map = New Int[mapwidth][]
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            setmap(x,y,Rnd(0,5))
        Next
        Next
        wipemap = New Int[mapwidth][]
        For Local i=0 Until mapwidth
            wipemap[i] = New Int[mapheight]
        Next
    End Method    
    Method update()
        If wipemode="wipein"            
            If Millisecs()>wipetime Then wipemode="wipedone"
        End If
        If wipemode="wipeout"
            If Millisecs()>wipetime Then wipemode="nothing"
        End If
    End Method
    Method initwipein()
        For Local y1=0 Until mapheight
        For Local x1=0 Until mapwidth
            wipemap[x1][y1] = Millisecs()+Rnd(3000)
        Next
        Next
        wipemode="wipein"
        wipetime=Millisecs()+3500
    End Method
    Method initwipeout()
        For Local y1=0 Until mapheight
        For Local x1=0 Until mapwidth
            wipemap[x1][y1] = Millisecs()+Rnd(3000)
        Next
        Next
        wipemode="wipeout"
        wipetime=Millisecs()+3500        
    End Method
    Method setmap:Void(x:Int,y:Int,val:Int)
        If x>=0 And y>=0 And x<mapwidth And y<mapheight
            map[x][y] = val
        End If
    End Method
    Method drawmap:Void()
        If wipemode<>"nothing"
            For Local y1=0 Until mapheight
            For Local x1=0 Until mapwidth
                drawtile(map[x1][y1],x1*tilewidth,y1*tileheight)
            Next
            Next        
        End If
        If wipemode="wipein"
            SetColor 0,0,0
            For Local y1=0 Until mapheight
            For Local x1=0 Until mapwidth
                If wipemap[x1][y1] > Millisecs()
                    DrawRect x1*tilewidth,y1*tileheight,tilewidth,tileheight
                End If
            Next
            Next        
        End If
        If wipemode="wipeout"
              SetColor 0,0,0
            For Local y1=0 Until mapheight
            For Local x1=0 Until mapwidth
                If wipemap[x1][y1] < Millisecs()
                    DrawRect x1*tilewidth,y1*tileheight,tilewidth,tileheight
                End If
            Next
            Next                      
        End If
    End Method
    Method drawtile(val:Int,x1:Int,y1:Int)
        Select val
            Case 0'water
                SetColor 0,0,255
                DrawRect x1,y1,tilewidth,tileheight
            Case 1'land
                SetColor 0,200,0
                DrawRect x1,y1,tilewidth,tileheight
            Case 2'forrest
                drawtile(1,x1,y1)
                SetColor 0,255,0
                DrawOval x1+5,y1+5,tilewidth-10,tileheight/2
                SetColor 150,10,0
                DrawRect x1+12,y1+tileheight-10,tilewidth-24,tileheight/2-5
            Case 3'hill
                drawtile(1,x1,y1)
                SetColor 0,255,0
                DrawOval x1+5,y1+10,tilewidth-10,tileheight-15
                SetColor 0,200,0
                DrawRect x1,y1+tileheight/1.5,tilewidth,10
            Case 4'mountain
                drawtile(1,x1,y1)
                SetColor 200,200,200
                DrawPoly(    [Float(x1+tilewidth/2),Float(y1),
                            Float(x1+tilewidth-5),Float(y1+tileheight-5),
                            Float(x1+5),Float(y1+tileheight-5)])
        End Select
    End Method

End Class

Global mymap:map = New map(20,14,32,32)

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        mymap.initwipein
    End Method
    Method OnUpdate()
        mymap.update
        If mymap.wipemode="wipedone"
            mymap.initwipeout
        End If
        If mymap.wipemode="nothing"
            mymap.initwipein            
        End If
    End Method
    Method OnRender()
        Cls 0,0,0
        mymap.drawmap
        SetColor 255,255,255
        DrawText "Screen wipe example (blocks) wipemode "+mymap.wipemode,0,0
    End Method
End Class


Function Main()
    New MyGame()
End Function

Thursday, March 19, 2015

Monkey-X - simple gui Tabber - code example



Here something I made. A tabber.

Code below :

Import mojo

Class tab
    Field x:Int,y:Int
    Field buttontext:String[10]
    Field numbuttons:Int
    Field activetab:Int=0
    Method update()
        If MouseDown(MOUSE_LEFT)
            Local x1:Int=0
            For Local i=0 Until numbuttons
                Local tw:Int = TextWidth(buttontext[i])+16
                If rectsoverlap(MouseX(),MouseY(),1,1,x+x1,y,tw,16)
                    activetab = i
                    Exit
                End If
                x1+=tw
            Next    
        End If
    End Method
    Method draw()
        Local x1:Int=0
        For Local i=0 Until numbuttons
            Local tw:Int = TextWidth(buttontext[i])+16
            Local at:Bool=False
            Local mo:Bool=False
            If activetab=i Then at=True Else at=False
            mo = rectsoverlap(MouseX(),MouseY(),1,1,x+x1,y,tw,16)
            drawbutton(x+x1,y,tw,16,at,mo)
            SetColor 255,255,255
            DrawText buttontext[i],x+x1+(tw/2),y+8,0.5,0.5
            x1+=tw
        Next
    End Method
    Method drawbutton(x:Int,y:Int,w:Int,h:Int,selected:Bool,mouseover:Bool)
        If selected = False
            If mouseover=False Then 
                SetColor 150,150,150
            Else
                SetColor 180,180,180
            End If
            DrawRect x,y,w,h
            SetColor 200,200,200
            DrawLine x,y,x+w,y
            DrawLine x,y,x,y+h
            SetColor 100,100,100
            DrawLine x,y+h,x+w,y+h
            DrawLine x+w,y,x+w,y+h
        End If
        If selected = True
            If mouseover=False Then            
                SetColor 100,100,100
            Else
                SetColor 130,130,130
            End If
            DrawRect x,y,w,h
            SetColor 50,50,50
            DrawLine x,y,x+w,y
            DrawLine x,y,x,y+h
            SetColor 70,70,70
            DrawLine x,y+h,x+w,y+h
            DrawLine x+w,y,x+w,y+h        
        End If
    End Method
    Method 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 Method    
End Class

Global t:tab = New tab

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        t.x = 50
        t.y = 32
        t.buttontext[0] = "City 1"
        t.buttontext[1] = "City 2"
        t.buttontext[2] = "City 3"
        t.buttontext[3] = "City 4"
        t.buttontext[4] = "City 5"
        t.buttontext[5] = "City 6"
        t.numbuttons = 6
    End Method
    Method OnUpdate()        
        t.update
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "Simple Tabber, press mouse on buttons to switch screens",0,0
        DrawText "Tab Selected : "+t.activetab,t.x+100,t.y+100
        t.draw
        
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monday, March 16, 2015

Monkey-X - Basic List view/selector - code example


Here a simple listview for selecting items from a list.

Code below :

Import mojo

Global selecteditem:String=""

Class listview
    Field x:Int,y:Int
    Field w:Int=200
    Field h:Int=300
    Field item:String[100]
    Field pos:Int=0
    Field selected:Int=0
    Field itemcount:Int=100    
    Field vscrollerpos:Int=0
    Field keydelay:Int=0
    Field active:Bool=True
    Method setitemname(item:String,pos:Int)
        For Local i=0 Until itemcount
            If i = pos
                Self.item[i] = item
            End If
        Next
    End Method
    Method update()
        If active=False Then Return
        'keys
        If Millisecs()>keydelay
            If KeyDown(KEY_DOWN)
                selected+=1
                If selected > itemcount-1 Then selected = itemcount-1
                If selected > pos + (h-64)/15
                    pos+=1
                    If pos>itemcount-((h-64)/15) Then pos = itemcount-((h-64)/15)
                End If
                keydelay = Millisecs() + 60
            End If
            If KeyDown(KEY_UP)
                selected-=1
                If selected < 0 Then selected = 0
                If selected < pos 
                    pos-=1
                    If pos<0 Then pos=0
                End If
                keydelay = Millisecs() + 60
            End If    
        End If
        ' mouse on items
        Local i:Int=0
        Local exitloop:Bool=False
        While exitloop=False
            If MouseHit(MOUSE_LEFT)
            If rectsoverlap(MouseX(),MouseY(),1,1,x+10,y+20+i*15,w-32-20,15)
                selected = i+pos
                If selected > itemcount-1 Then selected=itemcount-1
            End If
            End If
            i+=1
            If i+pos>itemcount Then exitloop = True
            If i*15 > h-64 Then exitloop = True
        Wend
        'update vscroller
        vscrollerpos = pos*((h-96)/itemcount)
        If MouseDown(MOUSE_LEFT)
        If rectsoverlap(MouseX(),MouseY(),1,1,x+w-32,y+31,32-10,h-96)
            pos = (MouseY()-y-32)/((h-96)/itemcount)
            If pos>itemcount-((h-64)/15) Then pos = itemcount-((h-64)/15)
            If pos<0 Then pos = 0        
        End If
        End If
        '
        'update v scroll buttons
        If MouseDown(MOUSE_LEFT)
            If rectsoverlap(MouseX(),MouseY(),1,1,x+w-32,y+10,32-10,20)
                pos-=1
                If pos<0 Then pos=0
            End If
            If rectsoverlap(MouseX(),MouseY(),1,1,x+w-32,y+h-64,32-10,20)
                pos+=1
                If pos>itemcount-((h-64)/15) Then pos = itemcount-((h-64)/15)
            End If
        End If
        'Select using enter key
        If KeyHit(KEY_ENTER)        
            selecteditem=item[selected]
            active = False
        End If
        'Cancel select using esc
        If KeyHit(KEY_ESCAPE)
            selecteditem="Nothing"
            active=False            
        End If
        '
        ' Update ok cancel buttons
        If MouseHit(MOUSE_LEFT)
            'ok
            If rectsoverlap(MouseX(),MouseY(),1,1,x+10,y+h-32,w/2.2,32-8)
                selecteditem=item[selected]
                active = False
            End If
            'cancel
            If rectsoverlap(MouseX(),MouseY(),1,1,x+w/2,y+h-32,w/2.2,32-8)
                ' code here        
                selecteditem="Nothing"
                active=False
            End If
        End If
    End Method
    Method draw()
        If active=False Then Return
        SetColor 200,200,200
        DrawRect x,y,w,h
        SetColor 0,0,0
        DrawRect x+10,y+10,w-20,h-20
        SetColor 255,255,255
        Local i:Int=0
        Local exitloop:Bool=False
        While exitloop=False
            If selected = i+pos Then 
                drawboxedrect x+10,y+20+i*15-1,w-32-20,15
            End If
            DrawText item[i+pos],x+20,y+20+i*15            
            i+=1
            If i+pos>itemcount-1 Then exitloop = True
            If i*15 > h-64 Then exitloop = True
        Wend
        drawbutton(x+10,y+h-32,w/2.2,32-8,"Ok")
        drawbutton(x+w/2,y+h-32,w/2.2,32-8,"Cancel")
        drawbutton(x+w-32,y+10,32-10,20,"<")
        drawbutton(x+w-32,y+h-64,32-10,20,">")
        drawvscroller(x+w-32,y+31,32-10,h-96)
    End Method
    Method drawvscroller(x:Int,y:Int,w:Int,h:Int)
        SetColor 255,255,255
        drawboxedrect(x,y,w,h)
        DrawRect x+1,y+vscrollerpos,w-2,38
    End Method
    Method drawbutton(x:Int,y:Int,w:Int,h:Int,text:String)
        SetColor 200,200,200
        DrawRect x,y,w,h
        SetColor 0,0,0
        DrawRect x+2,y+2,w-4,h-4
        SetColor 255,255,255
        DrawText text,x+w/2,y+h/2,0.5,0.5
    End Method
    Method drawboxedrect(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 Method
    Method 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    Method
End Class

Global l:listview = New listview

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        l.itemcount=100
           For Local i=0 Until l.itemcount
               l.setitemname(String(i),i)
           Next
       l.x=100
       l.y=50
    End Method
    Method OnUpdate()        
        l.update
        If l.active = False And Rnd(100)<2 
            l.active = True
            l.x = Rnd(50,320)
            l.y = Rnd(50,100)
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        l.draw
        DrawText "Use Mouse and enter and curs up/down for listview",0,0
        DrawText "Selected item :"+selecteditem,0,15
    End Method
End Class


Function Main()
    New MyGame()
End Function

Thursday, March 12, 2015

Monkey-X - Simple RPG Shop screen - code example


Here a example of a rpg shop window. You can buy and sell swords and shields.

Code below :

Import mojo

Class player
    Field inventory:String[] =     [    "Wooden Sword","Wooden Shield"]
    Field gold:Int=362
End Class

Class shop
    Field items:String[] =     [    "Wooden Sword","Iron Sword",
                                "Steel Sword","Wooden Shield",
                                "Iron Shield","Steel Shield"]
    Field prices:Int[] =    [    10,20,30,15,30,40]
    Field buyarrowindex:Int=0
    Method update()
        ' navigate through the screen
        If KeyHit(KEY_DOWN)
            buyarrowindex+=1
        End If
        If KeyHit(KEY_UP)
            buyarrowindex-=1
        End If
        ' keep arrow inside options
        If buyarrowindex < 0 Then buyarrowindex = 0
        If buyarrowindex > 7 Then buyarrowindex = 7
        If KeyHit(KEY_ENTER)            
            If buyarrowindex < 6 'in the buy part of the screen
                If hastomanyitems()=False
                    buyitem(items[buyarrowindex])
                End If
            End If
            If buyarrowindex > 5 ' if in the sell part ot screen
                sellitem(buyarrowindex-6)
            End If
        End If
    End Method
    Method draw()
        SetColor 255,255,255
        PushMatrix()
        Scale 10,10
        Translate 50,50
        Rotate 45
        Translate -50,-50
        DrawText "Shop",35,-10
        PopMatrix()
        PushMatrix()
        Scale 2,2
        DrawText "Weapon",340/2,32
        DrawText "Price",550/2,32
        For Local y=0 Until items.Length
            If buyarrowindex = y
            DrawText ">",230/2,32+15+y*15
            End If
            DrawText "Buy",250/2,32+15+y*15
            DrawText items[y],340/2,32+15+y*15
            DrawText prices[y],550/2,32+15+y*15
        Next
        DrawText "Player gold",20,200/2+32+15
        DrawText p.gold,20,200/2+64
        DrawText "Player carying",340/2,200/2+32+15
        For Local y=0 Until p.inventory.Length
            If buyarrowindex - 6 = y
                DrawText ">",230/2,200/2+y*15+64
            End If
            DrawText "Sell",250/2,200/2+y*15+64
            DrawText p.inventory[y],340/2,200/2+y*15+64
            DrawText sellprice(p.inventory[y]),550/2,200/2+y*15+64
        Next
        PopMatrix()
    End Method
    Method sellitem:Void(index:Int)        
        p.gold += sellprice(p.inventory[index])
        p.inventory[index] = ""
    End Method
    Method buyitem:Void(item:String)
        If p.gold >= itemprice(item) Then 
            If p.inventory[0] = "" Then 
                p.inventory[0] = item
            Else
                p.inventory[1] = item
            End If
            p.gold -= itemprice(item)
        End If
    End Method
    Method itemprice:Int(item:String)
        For Local i=0 Until items.Length
            If item = items[i] Then Return prices[i]
        Next
    End Method
    Method hastomanyitems:Bool()
        If p.inventory[0] <> "" And p.inventory[1] <> "" Then Return True
        Return False
    End Method
    Method sellprice:Int(item:String)
        For Local i=0 Until items.Length
            If items[i] = item Then Return prices[i]/3
        Next
        Return 0
    End Method
End Class

Global p:player = New player
Global s:shop = New shop

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()
        s.update
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255        
        s.draw
        DrawText "Use arrow up/down and enter.",320,0,0.5,0
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - List Count - code example


Here code that shows how to use the Count command.

Code below :

Import mojo

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

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        mylist.AddLast(10)
        mylist.AddLast(20)
    End Method
    Method OnUpdate()   
        If Rnd(100)<2 Then mylist.AddLast(Rnd(0,100))     
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "List Count example.",0,0
        DrawText "Number of items in list : " + mylist.Count,0,15
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - String position and FromChar - code example


Here code that shows how to print a single character from a string.

Code below :

Import mojo

Global sp:Int=0
Global str:String="Hello World."

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(5)
    End Method
    Method OnUpdate()        
        sp+=1
        If sp>=str.Length Then sp=0
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        PushMatrix()
        Scale 2.0,2.0
        DrawText "String Character example.",0,0
        DrawText String.FromChar(str[sp]),0,15
        DrawText "Character "+sp+ " of : "+str,0,30
        PopMatrix()
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Player firing bullet at enemy - code example


Here a example of how to fire a bullet from the player position to the enemy. The bullet dissapears when it hits the enemy. The code uses the rectsoverlap function.

Code below :

Import mojo

Class player
    Field x:Float,y:Float
End Class

Class bullet
    Field x:Float,y:Float
    Field active:Bool
End Class

Class enemy
    Field x:Float,y:Float
End Class

Global p:player = New player
Global e:enemy = New enemy
Global b:bullet = New bullet

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        p.x = 320
        p.y = 240
        e.x = 460
        e.y = 240
    End Method
    Method OnUpdate()
        ' if the user presses space
        If KeyHit(KEY_SPACE)
            ' if the bullet is not active
            If b.active = False
                ' put the bullet at the player position
                b.x = p.x+32
                b.y = p.y+12
                ' activate the bullet
                b.active = True
            End If
        End If
        ' if the bullet is active
        If b.active = True 
            ' increase the position by 1 to the right
            b.x += 1
            ' if the bullet hits the enemy then set the 
            ' bullet active to false
            If rectsoverlap(b.x,b.y,6,6,e.x,e.y,32,32)
                b.active = False
            End If
            ' if the bullet gets out of the screen area
            ' then set it to not active
            If rectsoverlap(b.x,b.y,0,0,0,0,640,480) = False Then b.active = False
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
           ' draw player
           DrawRect p.x,p.y,32,32
           ' draw enemy
           DrawRect e.x,e.y,32,32
           ' draw bullet if it is active
           If b.active = True Then DrawRect b.x,b.y,6,6
           DrawText "Press space to fire bullet.",0,0
    End Method
End Class

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

Function Main()
    New MyGame()
End Function

Sunday, March 8, 2015

Monkey-X - Screen Wipe method 3 - code example


Here another way to wipe the screens inbetween scenes.

Code below :

Import mojo

Class map
    Field tilewidth:Int,tileheight:Int
    Field mapwidth:Int,mapheight:Int
    Field map:Int[][]
    Field bwipe:Int[10]
    Field wipeactive:Bool=False
    Field wipemode:String
    Method New(mapwidth:Int,mapheight:Int,tilewidth:Int,tileheight:Int)
        Self.tilewidth = tilewidth
        Self.tileheight = tileheight
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        map = New Int[mapwidth][]
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            setmap(x,y,Rnd(0,5))
        Next
        Next
    End Method    
    Method update()
        If wipemode = "wipeout"
            For Local i=0 Until 10
                If bwipe[i] < 64 Then bwipe[i] +=2
            Next
            If bwipe[0] > 63 Then wipemode="black"
        End If
        If wipemode = "wipein"
            For Local i=9 To 0 Step -1
                If bwipe[i] > 0 Then bwipe[i] -= 2
            Next
            If bwipe[9] <= 0 Then wipemode="nothing"
        End If
    End Method
    Method wipeininit()
        wipemode="wipein"
        For Local i=0 To 9
            bwipe[i] = 64+i*10
        Next
    End Method
    Method wipeoutinit()
        wipemode="wipeout"
        For Local i=0 To 9
            bwipe[i] = -100+(i*10)
        Next
    End Method
    Method setmap:Void(x:Int,y:Int,val:Int)
        If x>=0 And y>=0 And x<mapwidth And y<mapheight
            map[x][y] = val
        End If
    End Method
    Method drawmap:Void()
        For Local y1=0 Until mapheight
        For Local x1=0 Until mapwidth
            drawtile(map[x1][y1],x1*tilewidth,y1*tileheight)
        Next
        Next
        If wipemode="wipeout"
            SetColor 0,0,0
            For Local i=0 Until 10
                If bwipe[i]>0
                    DrawRect i*64,0,bwipe[i],480
                End If
            Next
        End If
        If wipemode="wipein"
            SetColor 0,0,0
            For Local i=0 Until 10
                If bwipe[i]<64
                    DrawRect i*64,0,bwipe[i],480
                Else
                    DrawRect i*64,0,64,480
                End If
            Next
        End If
        If wipemode="black"
            SetColor 0,0,0
            DrawRect 0,0,640,480
        End If
    End Method
    Method drawtile(val:Int,x1:Int,y1:Int)
        Select val
            Case 0'water
                SetColor 0,0,255
                DrawRect x1,y1,tilewidth,tileheight
            Case 1'land
                SetColor 0,200,0
                DrawRect x1,y1,tilewidth,tileheight
            Case 2'forrest
                drawtile(1,x1,y1)
                SetColor 0,255,0
                DrawOval x1+5,y1+5,tilewidth-10,tileheight/2
                SetColor 150,10,0
                DrawRect x1+12,y1+tileheight-10,tilewidth-24,tileheight/2-5
            Case 3'hill
                drawtile(1,x1,y1)
                SetColor 0,255,0
                DrawOval x1+5,y1+10,tilewidth-10,tileheight-15
                SetColor 0,200,0
                DrawRect x1,y1+tileheight/1.5,tilewidth,10
            Case 4'mountain
                drawtile(1,x1,y1)
                SetColor 200,200,200
                DrawPoly(    [Float(x1+tilewidth/2),Float(y1),
                            Float(x1+tilewidth-5),Float(y1+tileheight-5),
                            Float(x1+5),Float(y1+tileheight-5)])
        End Select
    End Method

End Class

Global mymap:map = New map(20,14,32,32)
Global mdelay:Int=Millisecs()+3000

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        mymap.wipeoutinit
    End Method
    Method OnUpdate()
        mymap.update
        If mymap.wipemode="black" And Millisecs() > mdelay
            mdelay=Millisecs()+3000
            mymap.wipeininit
        End If
        If mymap.wipemode="nothing" And Millisecs() > mdelay
            mdelay=Millisecs()+3000
            mymap.wipeoutinit
        End If
    End Method
    Method OnRender()
        Cls 0,0,0
        mymap.drawmap
        SetColor 255,255,255
        DrawText "Screenwipe example : "+mymap.wipemode,0,0
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - 2d Block Pushing - code example


Here a small example of how to push blocks around with the player.

Code below :

Import mojo

Global tilewidth = 32
Global tileheight = 32

Class box
    Field x:Float,y:Float
    Method New(x:Float,y:Float)
        Self.x = x
        Self.y = y
    End Method
    Method draw()
        SetColor 255,0,0
        DrawRect x,y,tilewidth,tileheight
    End Method
End Class

Class player
    Field x:Float,y:Float
    Method update()
        If KeyDown(KEY_RIGHT)
            If pushbox(1,0) = True Then x+=1
        End If
        If KeyDown(KEY_LEFT)
            If pushbox(-1,0) = True Then x-=1
        End If
        If KeyDown(KEY_UP)
            If pushbox(0,-1) = True Then y-=1
        End If
        If KeyDown(KEY_DOWN)
            If pushbox(0,1) = True Then y+=1
        End If

    End Method
    Method pushbox:Bool(x1:Int,y1:Int)
        For Local i:=Eachin boxes
            If rectsoverlap(x+x1,y+y1,tilewidth,tileheight,
                            i.x,i.y,tilewidth,tileheight)
                For Local ii:=Eachin boxes
                    If i<>ii
                        If rectsoverlap(i.x+x1,i.y+y1,tilewidth,tileheight,
                                    ii.x,ii.y,tilewidth,tileheight)
                            Return False
                        End If
                    End If
                Next
                i.x+=x1
                i.y+=y1
            End If
        Next
        Return True
    End Method
    Method draw()
        SetColor 255,255,255
        DrawRect x,y,tilewidth,tileheight
    End Method
End Class

Global p:player = New player
Global boxes:List<box> = New List<box>

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        p.x = 320
        p.y = 240
        boxes.AddLast(New box(360,240))
        boxes.AddLast(New box(400,240))
        boxes.AddLast(New box(400,300))
    End Method
    Method OnUpdate()        
        p.update
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "Block Pushing example. Cursors move player.",0,0
        For Local i:=Eachin boxes
            i.draw
        Next
        p.draw
    End Method
End Class

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

Function Main()
    New MyGame()
End Function

Sunday, March 1, 2015

Monkey-X - Platformer with Elevators - code example


Here a example of how to do elevators in platform games. The elevators are not that advanced. They do not come to you if you wait.

Code below :

Import mojo

Const tilewidth = 32
Const tileheight = 32
Const mapwidth:Int=20
Const mapheight:Int=10
Global map:Int[][] = [      [1,1,1,1,1,1,1,1,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,1],
                            [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                            [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                            [1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,1,1,1],
                            [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
                            [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
                            [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
                            [1,1,1,1,2,0,0,0,0,0,0,0,0,0,0,2,1,1,1,1],
                            [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ]

Class elevators
    Field id:Int
    Field x:Float,y:Float
    Field state:String
    Field waittime:Int
    Field waittime2:Int
    Method New(x:Float,y:Float,id:Int)
        Self.id = id
        Self.x = x
        Self.y = y
        state = "bottom"
    End Method
    Method update()
        If Millisecs() < waittime Then Return
        Select state
            Case "bottom"
            Case "top"
                If Millisecs() > waittime2 Then state="going down"
            Case "going up"
                Local x1:Int=x
                Local y1:Int=y-1
                x1/=tilewidth
                y1/=tileheight
                If map[y1][x1+1] = 1 Or map[y1][x1-1] = 1 Then 
                    y-=1 
                Else 
                    state="top"
                    waittime2 = Millisecs() + 5000
                    waittime = Millisecs() + 2000
                End If
            Case "going down"
                Local x1:Int=x
                Local y1:Int=y
                x1/=tilewidth
                y1/=tileheight
                If map[y1-1][x1] = 0
                    y+=1                    
                Else
                    state="bottom"
                    y=(y/tileheight)*tileheight-1
                    waittime = Millisecs()+ 2000
                End If
                y+=1
        End Select
    End Method
    Method draw()
        SetColor 0,0,200
        DrawRect x,y,tilewidth,10
    End Method
End Class

Class players
    Field x:Float = 640/2-16
    Field y:Float = 480/2
    Field pw:Int=32
    Field ph:Int=32
    Field incy:Float
    Field jump:Bool=False
    Field lockelevator:Bool=False
    Field elevatorid:Int
    Method New()
    End Method
    Method update()           
        playermovement
        playergravity
        playerelevator                     
    End Method
    Method playerelevator()
        For Local i:=Eachin elevator
            If lockelevator = False
                If i.state="going up" Or i.state="going down"
                    If rectsoverlap(x,y,pw,ph+1,i.x,i.y,tilewidth,1)
                        lockelevator = True
                        elevatorid = i.id
                    End If                    
                End If
            End If
            If i.state = "bottom" And Millisecs() > i.waittime            
            If rectsoverlap(x,y,pw,ph+1,i.x,i.y,tilewidth,1)
                i.state = "going up"
                lockelevator = True
                elevatorid = i.id
            End If
            End If
            If i.state = "top" And Millisecs() > i.waittime
            If rectsoverlap(x,y,pw,ph+1,i.x,i.y,tilewidth,1)
            If playertc2(0,1) = False
                i.state = "going down"
                lockelevator = True
                elevatorid = i.id
            End If
            End If
            End If
        Next
        If lockelevator = True
            For Local i:=Eachin elevator
                If i.id = elevatorid
                    If rectsoverlap(x,y,pw,ph+2,i.x,i.y,tilewidth,12) = False Then lockelevator = False
                    If i.state="top" Then lockelevator=False ; y=i.y-(tileheight+2)
                    If i.state="bottom" Then lockelevator = False ; y=i.y-(tileheight+2)
                    If i.state = "going up" Then y=(i.y-(tileheight+2))
                    If i.state = "going down" Then y=(i.y-(tileheight))
                End If
            Next
        End If
    End Method
    Method playergravity()
        If jump = False And playertc(0,1) = False And lockelevator=False
            jump = True
            incy = 0
        End If
        If jump = False And KeyDown(KEY_SPACE) = True
            lockelevator=False
            incy = -4
            jump = True
        End
        'If the player is in the jump
        If jump = True
            incy += 0.1
            'if the player is going up
            If incy <=0
                For Local i:Int = 0 Until Abs(incy)                
                    y -= 1
                    If playertc(0,-1) = True
                        incy = 0
                        Exit
                    End If
                End
            End
            ' if the player if going down
            If incy > 0
                For Local i:Int = 0 Until incy
                    y += 1
                    'if the player touches the ground
                    If playertc(0,1) = True
                        jump = False                        
                        Exit
                    End
                End
            End
        End
    End Method
    Method playermovement()
       If KeyDown(KEY_RIGHT)
           For Local i=0 Until 2
            If playertc(1,0) = False
                   x+=1
            End If
           Next
       End If
       If KeyDown(KEY_LEFT)
           For Local i=0 Until 2    
               If playertc(-1,0) = False
                   x-=1
               End If
           Next
       End If        
    End Method
    Method playertc2:Bool(x1:Int,y1:Int) 'player tile collision 2
        Local cx = (x + x1) / tilewidth
           Local cy = (y + y1) / tileheight
        For Local y2=cy-1 Until cy+2
        For Local x2=cx-1 Until cx+2
            If x2>=0 And x2<mapwidth And y2>=0 And y2<mapheight
                If map[y2][x2] = 1
                    If rectsoverlap(x+x1,y+y1,pw,ph,x2*tilewidth,
                                    y2*tileheight,tilewidth,tileheight) = True
                        Return True
                    End If
                End If
            End If
        Next
        Next
        Return False
    End Method
    Method playertc:Bool(x1:Int,y1:Int)
        Local cx = (x + x1) / tilewidth
           Local cy = (y + y1) / tileheight
        For Local y2=cy-1 Until cy+2
        For Local x2=cx-1 Until cx+2
            If x2>=0 And x2<mapwidth And y2>=0 And y2<mapheight
                If map[y2][x2] = 1
                    If rectsoverlap(x+x1,y+y1,pw,ph,x2*tilewidth,
                                    y2*tileheight,tilewidth,tileheight) = True
                        Return True
                    End If
                End If
            End If
        Next
        Next
        For Local i:=Eachin elevator
            If rectsoverlap(x+x1,y+y1,pw,ph,i.x,i.y,tilewidth,12) = True Then Return True
        Next
        Return False
    End Method
    Method draw()
        ' draw the player
        SetColor 255,255,0        
        DrawOval x,y,pw,ph        
    End Method
End Class

Global player:List<players> = New List<players>
Global elevator:List<elevators> = New List<elevators>

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        player.AddLast(New players())
        initelevators
    End
    Method OnUpdate()
        ' Player left and right movement
        For Local i:=Eachin player
            i.update
        Next
        For Local i:=Eachin elevator    
            i.update
        Next
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        ' draw the map
        For Local y:Int = 0 Until mapheight
        For Local x:Int = 0 Until mapwidth
            If map[y][x] = 1 Then DrawRect(x*tilewidth,y*tileheight,tilewidth,tileheight)
        End
        End
        For Local i:=Eachin elevator
            i.draw
        Next
        SetColor 255,255,255
        DrawText "Platformer Elevators Example",10,10
        DrawText "Use cursor left/right and space bar",160,10
        For Local i:=Eachin player
            i.draw
        Next
    End
End


Function initelevators:Void()
    Local cnt:Int=0
    For Local y=0 Until mapheight
    For Local x=0 Until mapwidth
        If map[y][x] = 2
            elevator.AddLast(New elevators(x*tilewidth,y*tileheight+tileheight,cnt))
            cnt+=1
        End If        
    Next
    Next
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

Function Main()
    New MyGame()
End

Monkey-X - Lists in List - code example


Here a example of how to put lists in a list. The list in a list can contain instances of classes. They need to be of the same class.

Code below :

Import mojo

Class test
    Field x:Int,y:Int,message:String
    Method New(x:Int,y:Int,message:String)
        Self.x = x
        Self.y = y
        Self.message = message
    End Method
    Method draw(x:Int,y:Int)
        Local ps:String =     "x:"+Self.x+" y:"+
                            Self.y+" Message :"+Self.message
        SetColor 255,255,255
        DrawText ps,x,y
    End Method
End Class

Global a:List< List< test > > = New List< List< test > >

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        ' create temp lists
         Local b:List<test> = New List<test>
         Local c:List<test> = New List<test>
         ' add the lists to the a list
        a.AddLast(b)
        a.AddLast(c)
        Local cnt:Int=0
        ' Loop through the lists in 'a'
        For Local i:=Eachin a
            'set some data
            Local s:String
            If cnt=0 Then s="First List"
            If cnt=1 Then s="Second List"
            i.AddLast(New test(0,15+cnt*15,s))
            cnt+=1
        Next
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        ' loop through all lists in a
        For Local i:=Eachin a
            'loop through the items in list b and c
            For Local ii:=Eachin i
                ii.draw(ii.x,ii.y)
            Next
        Next
        DrawText "Lists in List example..",0,0
    End Method
End Class

Function Main()
    New MyGame()
End Function

Monkey-X - Mouse/screen Effect - code example


I was playing the the hamster code and got this effect going.

Code below :

Import mojo

Const numhamsters:Int=400
Global tilewidth:Int=10
Global tileheight:Int=10
Global mapwidth:Int=640/tilewidth
Global mapheight:Int=480/tileheight
Global searchmap:Int[][] = New Int[mapwidth][]

Class hamsters
    Field x:Int,y:Int
    Method New(x:Int,y:Int)
        Self.x = x
        Self.y = y
    End Method
    Method update()
        Local x1:Int=0
        Local y1:Int=0
        Local val:Int=searchmap[x][y]
        searchmap[x][y] += 1
        Local exitloop:Bool = False
        Local cnt:Int=0
        While exitloop = False
            cnt+=1
            For Local y2=-1 To 1
            For Local x2=-1 To 1
                Local x3:Int=x+x2
                Local y3:Int=y+y2
                If x3>=0 And y3>= 0 And x3<mapwidth And y3<mapheight
                    If searchmap[x3][y3] <= val Or cnt>10
                        If Rnd(10)<2
                        x1 = x3 ; y1 = y3
                        exitloop = True
                        Exit
                        End If
                    End If
                End If
            Next
            Next
        Wend
        If Rnd(10)<2
            Local mx:Int=MouseX()
            Local my:Int=MouseY()
            mx /= tilewidth
            my /= tileheight
            x1=mx
            y1=my
        End If
        x=x1
        y=y1        
    End Method
    Method draw()
        SetColor 255,0,0
        DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
    End Method
End Class

Global hamster:List<hamsters> = New List<hamsters>

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(20)
        For Local i=0 Until mapwidth
            searchmap[i] = New Int[mapheight]
        Next
        For Local i=0 Until numhamsters
            hamster.AddLast(New hamsters(Rnd(2,mapwidth-4),Rnd(2,mapheight-4)))
        Next
    End Method
    Method OnUpdate()
        For Local i:=Eachin hamster
            i.update
        Next        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
        Next
        Next
        For Local i:=Eachin hamster
            i.draw
        Next
        SetColor 255,255,255
        DrawText "Move the mouse on the screen to see effect..",0,0
    End Method
End Class


Function Main()
    New MyGame()
End Function