Thursday, March 12, 2015

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