Wednesday, August 26, 2015

Monkey-X - Beginners - Diagional bouncing and control - code example


Here a short example that shows how to bounce a object diagionally against the sides of the screen. The cursor keys can be used to change the direction.

Import mojo

' player coords and start position
Global px:Int = 320
Global py:Int = 220
' starting movement direction
Global horizdir:String="right"
Global vertdir:String="up"
Global movementspeed = 3

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
        ' the next 4 lines is the input (cursors)
        If KeyDown(KEY_LEFT) Then horizdir="left"        
        If KeyDown(KEY_RIGHT) Then horizdir="right"
        If KeyDown(KEY_UP) Then vertdir="up"
        If KeyDown(KEY_DOWN) Then vertdir="down"
        For Local i=0 Until movementspeed
            ' move the player based on the direction
            Select horizdir
                Case "left"
                    px-=1
                Case "right"
                    px+=1
            End Select
            Select vertdir
                Case "up"
                    py-=1
                Case "down"
                    py+=1
            End Select
            ' if the player is outside of the device
            ' screen then (BOUNCE) go in oposite direction
            If px<0 Then horizdir="right"
            If px>640-16 Then horizdir="left"
            If py<0 Then vertdir="down"
            If py>480-16 Then vertdir="up"
        Next
    End
    Method OnRender()
        Cls 0,0,0 
        'set the color
        SetColor 255,255,255
        ' draw the player
        DrawRect px,py,16,16
        DrawText "Diagional Bounce Example - Use cursors to change direction.",10,10
    End
End

Function Main()
    New MyGame()
End

Monkey-X - Beginners - Bouncing of the side and control - code example


Here a short example of how to bounce (reverse direction) a player of the sides of the screen. There is also control with the cursor keys. There is also a movement speed variable.

Import mojo

' player coords and start position
Global px:Int = 320
Global py:Int = 220
' starting movement direction
Global currentdir:String="right"
Global movementspeed = 3

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
        ' the next 4 lines is the input (cursors)
        If KeyDown(KEY_LEFT) Then currentdir="left"
        If KeyDown(KEY_RIGHT) Then currentdir="right"
        If KeyDown(KEY_UP) Then currentdir="up"
        If KeyDown(KEY_DOWN) Then currentdir="down"
        For Local i=0 Until movementspeed
            ' move the player based on the direction
            Select currentdir
                Case "left"
                    px-=1
                Case "right"
                    px+=1
                Case "down"
                    py+=1
                Case "up"
                    py-=1
            End Select
            ' if the player is outside of the device
            ' screen then (BOUNCE) go in other direction
            If px<0 Then currentdir="right"
            If px>640-16 Then currentdir="left"
            If py<0 Then currentdir="down"
            If py>480-16 Then currentdir="up"
        Next
    End
    Method OnRender()
        Cls 0,0,0 
        'set the color
        SetColor 255,255,255
        ' draw the player
        DrawRect px,py,16,16
        DrawText "Bounce Example - Use cursors to move player.",10,10
    End
End

Function Main()
    New MyGame()
End

Monkey-X - Topdown Auto move and cursor keys - code example


Here a example where you can move the player with the cursor keys. The player does not stop moving when you are not pressing the buttons.

Import mojo

' player coords and start position
Global px:Int = 320
Global py:Int = 220
' starting movement direction
Global currentdir:String="right"


Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
        ' the next 4 lines is the input (cursors)
        If KeyDown(KEY_LEFT) Then currentdir="left"
        If KeyDown(KEY_RIGHT) Then currentdir="right"
        If KeyDown(KEY_UP) Then currentdir="up"
        If KeyDown(KEY_DOWN) Then currentdir="down"
        ' move the player based on the direction
        Select currentdir
            Case "left"
                px-=1
            Case "right"
                px+=1
            Case "down"
                py+=1
            Case "up"
                py-=1
        End Select
        ' if the player is outside of the device
        ' screen then put him at the border at
        ' the other side.
        If px<0 Then px=DeviceWidth
        If px>640 Then px=0
        If py<0 Then py=DeviceHeight
        If py>480 Then py=0
    End
    Method OnRender()
        Cls 0,0,0 
        'set the color
        SetColor 255,255,255
        ' draw the player
        DrawRect px,py,16,16
        DrawText "Use cursors to move player.",10,10
    End
End

Function Main()
    New MyGame()
End

Sunday, August 23, 2015

Monkey-X - 2d topdown scrolling rpg with random maps - code example


Here a rather long source of a 2d topdown randommap coincollecting rpg. Not much has been done but it shows how you can mix different code from this blog into one thing.
press t to warp into a new dungeon. click in the applet to enable. move with the cursor keys. Collect health and coins. 

Import mojo

' map things
' 1 = wal, 2 = door, 3 = health, 4 = coin,
' 5 = tree, 6 = water, 7 = rock

'map gen part
Global mapwidth:Int = 70
Global mapheight:Int= 60
Global sx:Int 'path start x
Global sy:Int 'path start y
Global ex:Int=mapwidth/2 'path end x
Global ey:Int=mapheight/2 'path end y
Global monmap:Int[mapwidth][]
Global map:Int[mapwidth][]
Global olmap:Int[mapwidth][]
Global clmap:Int[mapwidth][]
Global pmap:Int[mapwidth][]
' this one refreshes maps
Global waittime:Int=0
'  scrolling rpg part
Const tilewidth:Int=20
Const tileheight:Int=20
Global mapx:Int=0
Global mapy:Int=0
Global mapsx:Int=0
Global mapsy:Int=0

Global showmap:Bool=False
Global remakemap:Bool=False

Class tiles
    Field walltile:Image
    Field rocktile:Image
    Field groundtile:Image
    Field moneytile:Image
    Field pixels:Int[tilewidth*tileheight]
    Method New()
        DebugLog "creating images"
        walltile = CreateImage(tilewidth,tileheight)
        rocktile = CreateImage(tilewidth,tileheight)
        groundtile = CreateImage(tilewidth,tileheight)
        moneytile = CreateImage(tilewidth,tileheight)
        DebugLog "drawing in image"
        Local s:Int=255/tileheight
        Local ss:Int=tileheight/125
        For Local y=0 Until tileheight
            drawr(0,y,tilewidth,y,y*s,y*s,y*s)
        Next        
        DebugLog "finishing image wall"
        walltile.WritePixels(pixels, 0, 0, tilewidth, tileheight, 0)
        For Local y=0 Until tileheight
            Local col:Int=0
            If y<tileheight/2
                col=0+((y*s))
            Else
                col=(255)-((y*s))
            End If
            drawr(0,y,tilewidth,y,col,col/3,0)
        Next        
        DebugLog "finishing image rock"
        rocktile.WritePixels(pixels, 0, 0, tilewidth, tileheight, 0)

        For Local y=0 Until tileheight
            Local col:Int=0
            If y<tileheight/2
                col=0+((y*s)/6)
            Else
                col=(255/6)-((y*s)/6)
            End If
            drawr(0,y,tilewidth,y,col,col,col)
        Next        
        DebugLog "finishing image ground"
        groundtile.WritePixels(pixels, 0, 0, tilewidth, tileheight, 0)
        drawo(tilewidth/3,tileheight/3,tilewidth/3,$FFFF0000)
        DebugLog "finishing image money"        
        moneytile.WritePixels(pixels, 0, 0, tilewidth, tileheight, 0)
    End Method
    Method drawo(x1,y1,radius,col)
        For Local y2=-radius To radius
        For Local x2=-radius To radius
            If (y2*y2+x2*x2) <= radius*radius+radius*0.8
                Local x3 = x2+x1
                Local y3 = y2+y1
                Local pc = y3*tilewidth+x3
                If pc>=0 And pc < tilewidth*tileheight
                    pixels[pc] = col
                End If
            End If
        Next
        Next    
    End Method    
    Method drawr(x1,y1,w1,h1,r:Int,g:Int,b:Int)
        For Local y2=y1 Until y1+h1
        For Local x2=x1 Until x1+w1
            Local pc = y2*tilewidth+x2
            If pc >= 0 And pc < tilewidth*tileheight
                pixels[pc] = argb(r,g,b)
            End If
        Next
        Next        
    End Method 
    Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255)
        Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b          
    End Function    
End Class    


Class monster
    Field lmap:Int[][]
    Field x:Int,y:Int
    Field mdelay:Int=0
    Field monsterspeed:Int
    Field offx:Int,offy:Int
    Field offxt:Int,offyt:Int
    Field mnx:Int,mny:Int
    Method New()
        lmap = makearray()
        ' find place for monster
        Local exitloop:Bool=False
        While exitloop = False
            Local x1:Int=Rnd(5,mapwidth-5)
            Local y1:Int=Rnd(5,mapheight-5)
            If monmap[x1][y1] = 1 Then
                exitloop = True
                lmap[x1][y1]=1
                Self.x = x1
                Self.y = y1
            End If
        Wend
        mnx = Self.x
        mny = Self.y
        offxt = 0
        offyt = 0
        offx = 0
        offy = 0      
        monsterspeed = Rnd(1,15)  
    End Method
    Method update()
        'Return
        mdelay+=1
        If mdelay< monsterspeed Then Return
        mdelay=0
        If offx<offxt
            offx+=1            
        End If
        If offx>offxt
            offx-=1
        End If
        If offy<offyt
            offy+=1
        End If
        If offy>offyt
            offy-=1
        End If
        Local set:Bool=False
        If     offx=offxt And
            offy=offyt Then
            set=True
        End If
        If set=False Then Return
'        DebugLog offx+":"+offxt+","+offy+":"+offyt
        x = mnx
        y = mny

        lmap[x][y]+=1
        If lmap[x][y] > 1000
            For Local y1=0 Until mapheight
            For Local x1=0 Until mapwidth
                lmap[x1][y1] = 0
            Next
            Next
            lmap[x][y]=1
        End If
        Local exitloop:Bool=False
        Local db:Int=0
        While exitloop = False            
            db+=1
            If db>200 Then exitloop=True
            Local y2:Int=Rnd(-2,2)
            Local x2:Int=Rnd(-2,2)
            If monmap[x+x2][y+y2] = 1
                If lmap[x+x2][y+y2] <= lmap[x][y]
                    offx = 0
                    offy = 0
                    offxt = 0
                    offyt = 0
                    If x2<0
                        offxt -= tilewidth
                    Elseif x2>0
                        offxt += tilewidth
                    End If
                    If y2<0
                        offyt -= tileheight
                    Elseif y2>0
                        offyt += tileheight                    
                    End If
                    'offxt=x2*tilewidth
                    'offyt=y2*tileheight                    
                    mnx=x+x2
                    mny=y+y2                    
                    exitloop=True
                    Return
                End If
            End If            
        Wend
    End Method
    Method draw()
        SetColor 255,0,0
        Local x1 = (x*tilewidth)
        Local y1 = (y*tileheight)
        x1-=(mapx*tilewidth)
        y1-=(mapy*tileheight)
        x1+=mapsx
        y1+=mapsy
        x1+=offx
        y1+=offy
        DrawRect     x1-tilewidth,
                    y1,
                    tilewidth,tileheight
        SetColor 255,255,255
        DrawText     "M",
                    (x1-tilewidth)+(tilewidth/2),
                    y1+tileheight/2,
                    0.5,0.5
    End Method
End Class


Class player
    Field x:Float=3*tilewidth
    Field y:Float=3*tileheight
    Field width:Int = tilewidth
    Field height:Int = tileheight
    Field keys:Int = 0
    Field coins:Int = 0
    Field health:Int = 3
    Field maxhealth:Int=10
End Class

Class mapgen
    Field gmap:Int[mapwidth][]
    Field hmap:Int[mapwidth][]
    Field twidth:Float,theight:Float
    Method New()
        For Local i = 0 Until mapwidth
            gmap[i] = New Int[mapheight]
            hmap[i] = New Int[mapheight]
        Next        
        twidth=640/mapwidth
        theight=480/mapheight
        makehmap
        makemap
    End Method
    Method makemap()
        DebugLog "make map"
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            map[x][y] = hmap[x][y]
        Next
        Next
        ex = Rnd(10,mapwidth-10)
        ey = Rnd(10,mapheight-10)
        Local numpaths:Int = (mapwidth*mapheight)/700
        DebugLog "make area's"
        For Local i=0 Until numpaths            
            sx = ex
            sy = ey
            Local exitloop:Bool=False
            While exitloop = False
                ex = Rnd(10,mapwidth-10)            
                ey = Rnd(10,mapheight-10)
                If sx<>ex And sy<>ey Then
                    exitloop = True
                End If
            Wend
            findpath    
            For Local ii:=Eachin path
                Local s:Int=2
                If Int(Rnd(1,5)) = 2
                    s=Rnd(2,5)
                    Elseif Int(Rnd(1,9)) = 2
                    s=8
                End If
                For Local y=-s/2 Until s/2
                For Local x=-s/2 Until s/2
                    If     ii.x+x >= 0 And ii.x+x <mapwidth And
                        ii.y+y >= 0 And ii.y+y < mapheight
                      gmap[ii.x+x][ii.y+y] = 1
                      pmap[ii.x+x][ii.y+y] = 1
                     End If
                Next
                Next
            Next
        Next        

        ' make monster map
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If gmap[x][y] = 1
                monmap[x][y] = 1
            End If
        Next
        Next

           DebugLog "add walls"
        ' here add the walls
        For Local y=1 Until mapheight-1
        For Local x=1 Until mapwidth-1
            If gmap[x][y] = 0 
            If gmap[x+1][y] = 1
            gmap[x][y]=2
            End If
            End If
            If gmap[x][y] = 1
            If gmap[x+1][y] = 0
            gmap[x+1][y] = 2
            End If
            End If
            If gmap[x][y] = 0
            If gmap[x][y+1] = 1
            gmap[x][y] = 2
            End If
            End If
            If gmap[x][y] = 1
            If gmap[x][y+1] = 0
            gmap[x][y+1] = 2
            End If
            End If
        Next
        Next
        DebugLog "set player on map and placecoins"
        ' set player on maptile
        setplayerposition
        DebugLog "place coins"
        placecoins((mapwidth*mapheight)/260)
        DebugLog "place life"
        placehealth((mapwidth*mapheight)/1000)
        ' map conversion
        DebugLog "turn walkable into rock"
        ' turn walkable into rock
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If gmap[x][y] = 0 Then gmap[x][y] = 7
        Next
        Next
        DebugLog "Turn floor into walkable"
        ' turn floor into walkable
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If gmap[x][y] = 1 Then
                gmap[x][y] = 0
            End If
            If gmap[x][y] = 2
                gmap[x][y] = 1
            End If
        Next
        Next
    End Method
    Method drawmap()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If gmap[x][y] = 1
                SetColor 150,150,150
                DrawRect     x*twidth,y*theight,
                            twidth,theight
            End If
            If gmap[x][y] = 2
                SetColor 200,200,200
                DrawRect     x*twidth,y*theight,
                            twidth,theight
            End If            
        Next
        Next 
        SetColor 255,255,0
        drawboxedrect(    mapx*twidth,mapy*theight,
                        (DeviceWidth/tilewidth)*twidth,
                        (DeviceHeight/tileheight)*theight)        
    End Method
    Method makehmap()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            hmap[x][y] = 3
        Next
        Next
        For Local i=0 To (mapwidth*mapheight)/10
            Local w:Int=Rnd(3,8)
            Local h:Int=Rnd(3,8)
            Local x:Int=Rnd(3,mapwidth-8)
            Local y:Int=Rnd(3,mapheight-8)
            For Local y1=-w/2 To w/2
            For Local x1=-h/2 To h/2
                hmap[x+x1][y+y1]+=1
            Next
            Next
        Next
    End Method
    Method drawhmap()        
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            SetColor hmap[x][y]*10,0,0
            DrawRect     x*twidth,y*theight,
                        twidth,theight
        Next
        Next
    End Method
    Method placehealth(amount:Int)
        For Local i=0 Until amount
            Local exitloop:Bool=False
            Local exloop:Int=0
            While exitloop = False
                Local x:Int=Rnd(0,mapwidth)
                Local y:Int=Rnd(0,mapheight)
                If gmap[x][y] = 1
                    gmap[x][y] = 3 
                    exitloop = True                
                End If
                exloop+=1
                If exloop>100 Then exitloop=True
            Wend                
        Next
    End Method

    Method placecoins(amount:Int)
        For Local i=0 Until amount
            Local exitloop:Bool=False
            Local exloop:Int=0
            While exitloop = False
                Local x:Int=Rnd(0,mapwidth)
                Local y:Int=Rnd(0,mapheight)
                If gmap[x][y] = 1
                    gmap[x][y] = 4 
                    exitloop = True                
                End If
                exloop+=1
                If exloop>100 Then exitloop=True
            Wend                
        Next
    End Method
    Method setplayerposition()
        Local exitloop:Bool=False
        Local eo:Int=0
        While exitloop = False
            eo+=1
            If eo>500 Then
                remakemap = True
                Return
            End If
            Local x:Int=Rnd(mapwidth)
            Local y:Int=Rnd(mapheight)
            If gmap[x][y] = 1
                mapx = x-(DeviceWidth/tilewidth)/2
                mapy = y-(DeviceHeight/tileheight)/2
                p.x = (DeviceWidth)/2
                p.y = (DeviceHeight)/2
                mapsx = 0
                mapsy = 0
                exitloop = True
            End If
        Wend
    End Method
End Class

Class openlist
    Field x:Int
    Field y:Int
    Field f:Int
    Field g:Int
    Field h:Int
    Field px:Int
    Field py:Int
    Method New(    x:Int=0,y:Int=0,f:Int=0,
                g:Int=0,h:Int=0,px:Int=0,
                py:Int=0)
        Self.x=x
        Self.y=y
        Self.f=f
        Self.g=g
        Self.h=h
        Self.px=px
        Self.py=py
    End Method
End Class
Class closedlist
    Field x:Int
    Field y:Int
    Field px:Int
    Field py:Int 
    Method New(x:Int,y:Int,px:Int,py:Int)
        Self.x = x
        Self.y = y
        Self.px = px
        Self.py = py
    End Method
End Class
Class pathnode
    Field x:Int
    Field y:Int
    Method New(x:Int,y:Int)
        Self.x = x
        Self.y = y
    End Method
End Class

Global mytiles:tiles
Global ol:List<openlist> = New List<openlist>
Global cl:List<closedlist> = New List<closedlist>
Global path:List<pathnode> = New List<pathnode>
Global mymonster:List<monster> = New List<monster>

Global mymapgen:mapgen

' the player class in p
Global p:player = New player

Class MyGame Extends App
    Method OnCreate()
        mytiles = New tiles
        SetUpdateRate(60)
        For Local i=0 Until mapwidth
            monmap[i] = New Int[mapheight]
            map[i] = New Int[mapheight]
            olmap[i] = New Int[mapheight]
            clmap[i] = New Int[mapheight]
            pmap[i] = New Int[mapheight]
        Next        
        mymapgen = New mapgen()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            map[x][y] = mymapgen.gmap[x][y]
        Next
        Next
        For Local i=0 Until 5
            mymonster.AddLast(New monster())
           Next
    End Method
    Method OnUpdate()
        waittime+=1
'        If waittime>120
'        If Rnd(0,100)>98 Then remakemap=True

        For Local i:=Eachin mymonster
            i.update
        Next        


        showmap=False
        If KeyDown(KEY_M)
            showmap=True
        End If
        If     KeyHit(KEY_T) Or 
            remakemap=True' Or
            'waittime>180 Then
            mymonster.Clear            
             waittime=0
            remakemap=False
            Seed=Millisecs()
            mapwidth=Rnd(30,200)
            mapheight=Rnd(30,200)
            monmap = makearray()
            map = makearray()
            olmap = makearray()
            clmap = makearray()
            pmap = makearray()
            mymapgen = New mapgen()
            For Local y=0 Until mapheight
            For Local x=0 Until mapwidth
                map[x][y] = mymapgen.gmap[x][y]
            Next
            Next
               For Local i=0 Until (mapwidth*mapheight)/500
                mymonster.AddLast(New monster())
               Next            
            If ptc(0,0)
                remakemap=True
            End If
        End If            
'            waittime = 0
'        End If
        ' scrolling rpg part
        For Local i=0 Until 2
        If p.x > DeviceWidth / 2
            If mapx+(DeviceWidth/tilewidth) < mapwidth-1
                mapsx-=1
                If mapsx < 0 Then 
                    mapsx = tilewidth-1
                    mapx += 1
                Endif
                p.x-=1
            End If
        End If
        Next

        For Local i=0 Until 2
        If p.x < DeviceWidth / 2
            If mapx > 0
                mapsx+=1
                If mapsx > tilewidth Then 
                    mapsx = 0
                    mapx -= 1
                Endif
                p.x+=1
            End If
        End If
        Next
        ' scrolling down
        For Local i=0 Until 2
        If p.y > DeviceHeight / 2
            If mapy+(DeviceHeight/tileheight) < mapheight-1
                mapsy-=1
                If mapsy < 0 Then 
                    mapsy = tileheight-1
                    mapy += 1
                Endif
                p.y-=1
            End If
        End If
        Next
        ' scrolling up
        For Local i=0 Until 2
        If p.y < DeviceHeight / 2
            If mapy > 0
                mapsy+=1                
                If mapsy > tileheight-1 Then 
                    mapsy = 0
                    mapy -= 1
                Endif
                p.y+=1
            End If
        End If
        Next

        
        If KeyDown(KEY_RIGHT)
            ptcs(1,0)
            For Local i=0 Until 2
                If ptc(1,0) = False
                p.x+=1
                End If
            Next
        End If
        If KeyDown(KEY_LEFT)
            ptcs(-1,0)
            For Local i=0 Until 2
                If ptc(-1,0) = False
                p.x-=1
                End If
            Next
        End If
        If KeyDown(KEY_UP)
            ptcs(0,-1)
            For Local i=0 Until 2
                If ptc(0,-1) = False
                p.y-=1
                End If
            Next
        End If
        If KeyDown(KEY_DOWN)
            ptcs(0,1)
            For Local i=0 Until 2
                If ptc(0,1) = False
                p.y+=1
                End If
            Next
        End If        
    End Method
    Method OnRender()
        Cls(0,0,0)
        If remakemap=True Then Return
          If showmap=True Then
            mymapgen.drawmap
        End If
        If showmap = False        
            SetColor 255,255,255        
            drawmap
            SetColor 255,255,0
            DrawOval p.x,p.y,p.width,p.height
            For Local i:=Eachin mymonster
                i.draw
            Next         
            SetColor 0,0,0
            DrawRect 0,0,DeviceWidth,32
            SetColor 255,255,255
            DrawText "Keys : "+p.keys,10,0
            DrawText "Coins : "+p.coins,10,16
            DrawText     "Health : "+p.health+
                        " of "+p.maxhealth,96,0
            DrawText     "Use Cursor keys to move.",
                        DeviceWidth/2,0
            DrawText     "Collect coins, press t = Teleport"+
                        " to new cavern.",DeviceWidth/2,16
            DrawText     "Press m for mapscreen.",
                        DeviceWidth/2,DeviceHeight-20
         End If
    End
End


' player collide with special blocks.
Function ptcs:Int(offsetx:Int=0,offsety:Int=0)
    Local cx = (p.x+offsetx)/tilewidth+mapx
    Local cy = (p.y+offsety)/tileheight+mapy
    For Local y2=cy-1 Until cy+4
    For Local x2=cx-1 Until cx+4
        If     x2>=0 And x2<mapwidth And 
            y2>=0 And y2<mapheight
            If map[x2][y2] > 0
                Local x3 = (x2-mapx)*tilewidth-tilewidth+mapsx
                Local y3 = (y2-mapy)*tileheight+mapsy
                If rectsoverlap(    p.x+offsetx,
                                    p.y+offsety,
                                    p.width,
                                    p.height,
                                    x3,y3,
                                    tilewidth,
                                    tileheight) = True
                    Select map[x2][y2]
                        Case 2
                        If p.keys > 0 
                            map[x2][y2] = 0
                            p.keys-=1
                        End If
                        Case 3 'health
                        If p.health < p.maxhealth
                            map[x2][y2] = 0
                            p.health+=1
                            Else
                            map[x2][y2] = 0
                            p.maxhealth+=1
                            p.health+=1
                        End If
                        Case 4 'coins
                        map[x2][y2] = 0
                        p.coins+=1
                    End Select
                End If
            End If
        End If
    Next
    Next
    Return 0
End Function

'player collide with solid blocks true/false
Function ptc:Bool(offsetx:Int=0,offsety:Int=0)
    Local cx = (p.x+offsetx)/tilewidth+mapx
    Local cy = (p.y+offsety)/tileheight+mapy
    For Local y2=cy-1 Until cy+4
    For Local x2=cx-1 Until cx+4
        If     x2>=0 And x2<mapwidth And y2>=0 And 
            y2<mapheight
            If     map[x2][y2] = 1 Or map[x2][y2] = 2 Or 
                map[x2][y2] = 5 Or map[x2][y2] = 6 Or 
                map[x2][y2] = 7
                Local x3 = (x2-mapx)*tilewidth-tilewidth+mapsx
                Local y3 = (y2-mapy)*tileheight+mapsy
                If rectsoverlap(p.x+offsetx,p.y+offsety,
                                p.width,p.height,x3,y3,
                                tilewidth,tileheight) = True
                    Return True
                End If
            End If
        End If
    Next
    Next
    Return False
End Function



Function drawmap:Void()
    For Local y=0 To DeviceHeight/tileheight
    For Local x=0 To DeviceWidth/tilewidth
        Local x1 = ((x*tilewidth)+mapsx)-tilewidth
        Local y1 = ((y*tileheight)+mapsy)
        Local mw:Int=mapwidth
        Local mh:Int=mapheight
        Local mx:Int=mapx
        Local my:Int=mapy
        If     x+mapx<mapwidth And
            x+mapx>0 And
            y+mapy<mapheight And
            y+mapy>0
            Select map[x+mapx][y+mapy]
                Case 0 'ground
                DrawImage mytiles.groundtile,x1,y1
                Case 1'Wall
'                SetColor 100,100,100
'                DrawRect x1,y1,tilewidth,tileheight
                DrawImage mytiles.walltile,x1,y1
                Case 2'Door
                SetColor 200,100,0
                DrawRect x1,y1,tilewidth,tileheight
                Case 3'Health
                SetColor 200,0,0
                DrawOval x1+4,y1+4,tilewidth-8,tileheight-8
                SetColor 255,255,255
                DrawText "H",x1+tilewidth/2,y1+tileheight/2,0.5,0.5
                Case 4'Coin
                'SetColor 255,255,0
                'DrawOval x1+4,y1+4,tilewidth-8,tileheight-8
                'SetColor 255,255,255
                DrawImage mytiles.moneytile,x1,y1
                DrawText "$",x1+tilewidth/2,y1+tileheight/2,0.5,0.5
                Case 5'tree
                SetColor 0,200,0
                DrawPoly(    [Float(x1+16),y1 ,x1+tilewidth,
                        y1+tileheight , x1,y1+tileheight])
                Case 6'water
                SetColor 0,0,200
                DrawRect x1,y1,tilewidth,tileheight
                Case 7'rock
                'SetColor 150,50,0
                'DrawRect x1,y1+10,tilewidth,tileheight-10
                DrawImage mytiles.rocktile,x1,y1
                'DrawPoly([Float(x1+tilewidth/2),y1,x1+tilewidth,
                '            y1+tileheight,x1,y1+tileheight])
                            
            End Select        
        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

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

Function makearray:Int[][]()
    Local aa:Int[mapwidth][]
    For Local i=0 Until mapwidth
        aa[i] = New Int[mapheight]
    Next
    Return aa
End Function

Function findpath:Bool()
    If sx = ex And sy = ey Then Return False
    For Local y=0 Until mapheight
    For Local x=0 Until mapwidth
        olmap[x][y] = 0
        clmap[x][y] = 0
    Next
    Next
    ol.Clear
    cl.Clear
    path.Clear
    ol.AddFirst(New openlist(sx,sy))
    Local tx:Int
    Local ty:Int
    Local tf:Int
    Local tg:Int
    Local th:Int
    Local tpx:Int
    Local tpy:Int
    Local newx:Int
    Local newy:Int
    Local lowestf:Int
    olmap[sx][sy] = 1
    While ol.IsEmpty() = False
        lowestf = 100000
        For Local i:=Eachin ol
            If i.f < lowestf
                lowestf = i.f
                tx = i.x
                ty = i.y
                tf = i.f
                tg = i.g
                th = i.h
                tpx = i.px
                tpy = i.py
            End If
        Next
        If tx = ex And ty = ey
            cl.AddLast(New closedlist(tx,ty,tpx,tpy))
            findpathback
            Return True
        Else
            removefromopenlist(tx,ty)
            olmap[tx][ty] = 0
            clmap[tx][ty] = 1
            cl.AddLast(New closedlist(tx,ty,tpx,tpy))
            For Local y=-1 To 1
            For Local x=-1 To 1
                newx = tx+x
                newy = ty+y
                If     newx>=0 And newy>=0 And 
                    newx<mapwidth And newy<mapheight
                If olmap[newx][newy] = 0
                If clmap[newx][newy] = 0
                    olmap[newx][newy] = 1
                    Local gg:Int
                    If pmap[newx][newy] = 1
                        gg=1
                        Else
                        gg = map[newx][newy]+1
                    End If
                    Local hh = distance(newx,newy,ex,ey)
                    Local ff = gg+hh
                    ol.AddLast(New openlist(newx,
                                            newy,ff,
                                            gg,hh,
                                            tx,ty))
                End If
                End If
                End If
            Next
            Next
        End If
    Wend
    Return False
End Function

Function findpathback:Bool()
    Local x=ex
    Local y=ey
    path.AddFirst(New pathnode(x,y))
    Repeat
        For Local i:=Eachin cl
            If i.x = x And i.y = y
                x = i.px
                y = i.py
                path.AddFirst(New pathnode(x,y))
            End If
        Next
        If x = sx And y = sy Then Return True
    Forever    
End Function

Function removefromopenlist:Void(x1:Int,y1:Int)
    For Local i:=Eachin ol
        If i.x = x1 And i.y = y1
            ol.Remove i
            Exit
        End If
    Next
End Function

Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
    Return Abs(x2-x1)+Abs(y2-y1)
End Function

Function Main()
    New MyGame()
End

Monkey-X - inloop class creation and creating 2 dim array (meth) - code example


Here a example of how to remake classes in the main loop. This is done with a map class. Also a example of how to set up the map array in the class and creating that with a
method.

Import mojo

Class map
    Field mapwidth:Int,mapheight:Int
    ' the 2 dimensional array i set up here
    ' it needs no size yet.
    Field map:Int[][]
    Field tilewidth:Float,tileheight:Float
    Method New(mapwidth:Int,mapheight:Int)
        ' here we resize the map array
        map = makearray(mapwidth,mapheight)
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        tilewidth = 640/mapwidth
        tileheight = 480/mapheight
        makemap
    End Method
    Method makemap() 'create random blocks
        For Local i=0 Until (mapwidth*mapheight)/10
            map    [Rnd(0,mapwidth)][Rnd(0,mapheight)] = 1
        Next
    End Method
    Method draw()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If map[x][y] = 1
                SetColor 150,150,150
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth,tileheight
                            
            End If
        Next
        Next
    End Method
    ' this function creates and returns a 2
    'dimensional array
    Method makearray:Int[][](a:Int,b:Int)
        Local aa:Int[a][]
        For Local i=0 Until a
            aa[i] = New Int[b]
        Next
        Return aa
    End Method    
End Class

Global mymap:map
Global mytime:Int

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        ' here we create the first map
        mymap = New map(20,20) 
    End Method
    Method OnUpdate()
        If mytime>60 
            mytime=0
            ' here we create a new map
            mymap = New map(    Rnd(10,51),
                                Rnd(10,51))
        Else
            mytime+=1
        End If
    End Method
    Method OnRender()
        Cls(0,0,0)
        mymap.draw
        SetColor 255,255,255
        DrawText     "Inloop Class creation and "+
                    "return 2 dim array",2,2
    End
End

Function Main()
    New MyGame()
End

Saturday, August 22, 2015

Monkey-X - A*(astar) and heighmap random map generator - code example


Here a map/image generator. It makes random paths on a simple heightmap and puts walls around the edges. (edit:Fixed a bug)

Import mojo

Global mapwidth:Int = 50
Global mapheight:Int= 50
Global sx:Int 'path start x
Global sy:Int 'path start y
Global ex:Int=mapwidth/2 'path end x
Global ey:Int=mapheight/2 'path end y
Global map:Int[mapwidth][]
Global olmap:Int[mapwidth][]
Global clmap:Int[mapwidth][]
Global pmap:Int[mapwidth][]
' this one refreshes maps
Global waittime:Int=0

Class mapgen
    Field gmap:Int[mapwidth][]
    Field hmap:Int[mapwidth][]
    Field twidth:float,theight:float
    Method New()
        For Local i = 0 Until mapwidth
            gmap[i] = New Int[mapheight]
            hmap[i] = New Int[mapheight]
        Next        
        twidth=640/mapwidth
        theight=480/mapheight
        makehmap
        makemap
    End Method
    Method makemap()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            map[x][y] = hmap[x][y]
        Next
        Next
        ex = Rnd(10,mapwidth-10)
        ey = Rnd(10,mapheight-10)
        Local numpaths:Int = (mapwidth*mapheight)/700
        For Local i=0 Until numpaths            
            sx = ex
            sy = ey
            Local exitloop:Bool=False
            While exitloop = False
                ex = Rnd(10,mapwidth-10)            
                ey = Rnd(10,mapheight-10)
                If sx<>ex And sy<>ey Then
                    exitloop = True
                End If
            Wend
            findpath    
            For Local ii:=Eachin path
                Local s:Int=2
                If Int(Rnd(1,5)) = 2
                    s=Rnd(2,5)
                    Elseif Int(Rnd(1,9)) = 2
                    s=8
                End If
                For Local y=-s/2 Until s/2
                For Local x=-s/2 Until s/2
                    If     ii.x+x >= 0 And ii.x+x <mapwidth And
                        ii.y+y >= 0 And ii.y+y < mapheight
                      gmap[ii.x+x][ii.y+y] = 1
                      pmap[ii.x+x][ii.y+y] = 1
                     End if
                Next
                Next
            Next
        Next
        ' here add the walls
        For Local y=1 Until mapheight-1
        For Local x=1 Until mapwidth-1
            If gmap[x][y] = 0 
            If gmap[x+1][y] = 1
            gmap[x][y]=2
            End If
            End If
            If gmap[x][y] = 1
            If gmap[x+1][y] = 0
            gmap[x+1][y] = 2
            End If
            End If
            If gmap[x][y] = 0
            If gmap[x][y+1] = 1
            gmap[x][y] = 2
            End If
            End If
            If gmap[x][y] = 1
            If gmap[x][y+1] = 0
            gmap[x][y+1] = 2
            End If
            End if
        Next
        Next
    End Method
    Method drawmap()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If gmap[x][y] = 1
                SetColor 150,150,150
                DrawRect     x*twidth,y*theight,
                            twidth,theight
            End If
            If gmap[x][y] = 2
                SetColor 200,200,200
                DrawRect     x*twidth,y*theight,
                            twidth,theight
            End If            
        Next
        Next 
    End Method
    Method makehmap()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            hmap[x][y] = 3
        Next
        Next
        For Local i=0 To (mapwidth*mapheight)/10
            Local w:Int=Rnd(3,8)
            Local h:Int=Rnd(3,8)
            Local x:Int=Rnd(3,mapwidth-8)
            Local y:Int=Rnd(3,mapheight-8)
            For Local y1=-w/2 To w/2
            For Local x1=-h/2 To h/2
                hmap[x+x1][y+y1]+=1
            Next
            Next
        Next
    End Method
    Method drawhmap()        
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            SetColor hmap[x][y]*10,0,0
            DrawRect     x*twidth,y*theight,
                        twidth,theight
        Next
        Next
    End Method
End Class

Class openlist
    Field x:Int
    Field y:Int
    Field f:Int
    Field g:Int
    Field h:Int
    Field px:Int
    Field py:Int
    Method New(    x:Int=0,y:Int=0,f:Int=0,
                g:Int=0,h:Int=0,px:Int=0,
                py:Int=0)
        Self.x=x
        Self.y=y
        Self.f=f
        Self.g=g
        Self.h=h
        Self.px=px
        Self.py=py
    End Method
End Class
Class closedlist
    Field x:Int
    Field y:Int
    Field px:Int
    Field py:Int 
    Method New(x:Int,y:Int,px:Int,py:Int)
        Self.x = x
        Self.y = y
        Self.px = px
        Self.py = py
    End Method
End Class
Class pathnode
    Field x:Int
    Field y:Int
    Method New(x:Int,y:Int)
        Self.x = x
        Self.y = y
    End Method
End Class

Global ol:List<openlist> = New List<openlist>
Global cl:List<closedlist> = New List<closedlist>
Global path:List<pathnode> = New List<pathnode>
Global mymapgen:mapgen

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        For Local i=0 Until mapwidth
            map[i] = New Int[mapheight]
            olmap[i] = New Int[mapheight]
            clmap[i] = New Int[mapheight]
            pmap[i] = New Int[mapheight]
        Next        
        mymapgen = New mapgen()
    End Method
    Method OnUpdate()
        waittime+=1
        If waittime>120
            mapwidth=Rnd(50,150)
            mapheight=mapwidth
            map = makearray()
            olmap = makearray()
            clmap = makearray()
            pmap = makearray()
            mymapgen = New mapgen()
            waittime = 0
        End If
    End Method
    Method OnRender()
        Cls(0,0,0)
        mymapgen.drawmap
        SetColor 255,255,255        
    End
End

Function makearray:Int[][]()
    Local aa:Int[mapwidth][]
    For Local i=0 Until mapwidth
        aa[i] = New Int[mapheight]
    Next
    Return aa
End Function

Function findpath:Bool()
    If sx = ex And sy = ey Then Return False
    For Local y=0 Until mapheight
    For Local x=0 Until mapwidth
        olmap[x][y] = 0
        clmap[x][y] = 0
    Next
    Next
    ol.Clear
    cl.Clear
    path.Clear
    ol.AddFirst(New openlist(sx,sy))
    Local tx:Int
    Local ty:Int
    Local tf:Int
    Local tg:Int
    Local th:Int
    Local tpx:Int
    Local tpy:Int
    Local newx:Int
    Local newy:Int
    Local lowestf:Int
    olmap[sx][sy] = 1
    While ol.IsEmpty() = False
        lowestf = 100000
        For Local i:=Eachin ol
            If i.f < lowestf
                lowestf = i.f
                tx = i.x
                ty = i.y
                tf = i.f
                tg = i.g
                th = i.h
                tpx = i.px
                tpy = i.py
            End If
        Next
        If tx = ex And ty = ey
            cl.AddLast(New closedlist(tx,ty,tpx,tpy))
            findpathback
            Return True
        Else
            removefromopenlist(tx,ty)
            olmap[tx][ty] = 0
            clmap[tx][ty] = 1
            cl.AddLast(New closedlist(tx,ty,tpx,tpy))
            For Local y=-1 To 1
            For Local x=-1 To 1
                newx = tx+x
                newy = ty+y
                If newx>=0 And newy>=0 And newx<mapwidth And newy<mapheight
                If olmap[newx][newy] = 0
                If clmap[newx][newy] = 0
                    olmap[newx][newy] = 1
                    Local gg:Int
                    If pmap[newx][newy] = 1
                        gg=1
                        Else
                        gg = map[newx][newy]+1
                    End If
                    Local hh = distance(newx,newy,ex,ey)
                    Local ff = gg+hh
                    ol.AddLast(New openlist(newx,newy,ff,gg,hh,tx,ty))
                End If
                End If
                End If
            Next
            Next
        End If
    Wend
    Return False
End Function

Function findpathback:Bool()
    Local x=ex
    Local y=ey
    path.AddFirst(New pathnode(x,y))
    Repeat
        For Local i:=Eachin cl
            If i.x = x And i.y = y
                x = i.px
                y = i.py
                path.AddFirst(New pathnode(x,y))
            End If
        Next
        If x = sx And y = sy Then Return True
    Forever    
End Function

Function removefromopenlist:Void(x1:Int,y1:Int)
    For Local i:=Eachin ol
        If i.x = x1 And i.y = y1
            ol.Remove i
            Exit
        End If
    Next
End Function

Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
    Return Abs(x2-x1)+Abs(y2-y1)
End Function

Function Main()
    New MyGame()
End

Friday, August 14, 2015

Monkey-X - RPG - Dungeon with monsters - code example


I combined the last dungeon generator with the shitting hamster code for the monster ai. The monsters will get to every last corner of the map and will thus eventually find you. Shows ai and map generation.

Import mojo

Global twidth:Int=8
Global theight:Int=8

Class monster
    Field mapwidth:Int=640/twidth
    Field mapheight:Int=480/theight
    Field map:Int[640/twidth][]
    Field x:Int,y:Int
    Field mdelay:Int=0
    Method New()
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        ' find place for monster
        Local exitloop:Bool=False
        While exitloop = False
            Local x1:Int=Rnd(5,mapwidth-5)
            Local y1:Int=Rnd(5,mapheight-5)
            If myroom.map[x1][y1] = 1 Then
                exitloop = True
                Self.x = x1
                Self.y = y1
                map[x][y] = 1
            End If
        Wend        
    End Method
    Method update()
        mdelay+=1
        If mdelay< 15 Then Return
        mdelay=0
        map[x][y]+=1
        If map[x][y] > 1000
            For Local y1=0 Until mapheight
            For Local x1=0 Until mapwidth
                map[x1][y1] = 0
            Next
            Next
            map[x][y]=1
        End If
        Local exitloop:Bool=False
        While exitloop = False            
            Local y2:Int=Rnd(-2,2)
            Local x2:Int=Rnd(-2,2)
            If myroom.map[x+x2][y+y2] = 1
            If map[x+x2][y+y2] <= map[x][y]
                x+=x2
                y+=y2
                exitloop=True
                Return
            End If
            End If
        Wend
    End Method
    Method draw()
        SetColor 255,255,0
        DrawRect x*twidth,y*theight,twidth,theight
    End Method
End Class

Class room
    Field mapwidth:Int=640/twidth
    Field mapheight:Int=480/theight
    Field tilewidth:Int=twidth
    Field tileheight:Int=theight
    Field map:Int[640/twidth][]
    Field refreshmaptime:Int=0
    Method New()
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        newmap
    End Method    
    Method update()

    End Method
    Method draw()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If map[x][y] = 1
                SetColor 150,150,150
                DrawRect     x*tilewidth,y*tileheight,
                            tilewidth,tileheight
            End If
            If map[x][y] = 2
                SetColor 200,200,200
                DrawRect     x*tilewidth,y*tileheight,
                            tilewidth,tileheight
            End If            
        Next
        Next
    End Method
    Method newmap()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            map[x][y] = 0
        Next
        Next
        drawrectinmap(Rnd(10,mapwidth-10),Rnd(10,mapheight-10),5,3)
        For Local i=0 Until 50
            makeroom
        Next
        makewalls
    
    End Method
    Method makewalls()
        ' put walls on the map
        For Local y=1 Until mapheight-1
        For Local x=1 Until mapwidth-1
            If map[x][y] = 0
                If map[x+1][y] = 1
                    map[x+1][y] = 2
                End If                
            End If
            If map[x][y] = 1
                If map[x+1][y] = 0
                    map[x][y] = 2
                End If
            End If
            If map[x][y] = 1
                If map[x][y+1] = 0
                    map[x][y] = 2
                End If
            End If
            If map[x][y] = 0
                If map[x][y+1] = 1
                    map[x][y+1] = 2
                End If
            End If
        Next
        Next
    End Method
    Method makeroom:Bool()
        'find suitable place to make room
        Local exitloop:Bool=False
        Local cnt:Int=0
        While exitloop = False
            cnt+=1
            If cnt>8000 Then exitloop=True
            Local x:Int=Rnd(5,mapwidth-8)
            Local y:Int=Rnd(5,mapheight-8)
            Local roomw:Int=Rnd(4,8)
            Local roomh:Int=Rnd(4,8)
            Local pass1:Bool=True
            For Local y1=0 Until roomh
            For Local x1=0 Until roomh
                If map[x1+x][y1+y] = 1 Then pass1=False
            Next
            Next
            Local pass2:Bool=False
            If pass1=True Then
                For Local y1=3 To roomh-3
                    If map[x-1][y+y1] = 1 Then pass2=True
                    If map[x+roomw][y+y1] = 1 Then pass2=True
                Next
                 For Local x1=3 To roomw-3
                    If map[x+x1][y-1] = 1 Then pass2 = True
                    If map[x+x1][y+roomh] = 1 Then pass2 = True
                Next
            End If
            If pass2 = True Then
                drawrectinmap(x,y,roomw,roomh)
                Return
            End If
        Wend
    End Method
    Method issuitable:Bool(x:Int,y:Int,w:Int,h:Int)
        For Local y1=0 Until h
        For Local x1=0 Until w
            If map[x+x1][y+y1] = 1 Then Return False
        Next
        Next
        Return True
    End Method
    Method drawrectinmap(x:Int,y:Int,w:Int,h:Int)
        For Local y1=0 Until h
        For Local x1=0 Until w
            map[x+x1][y+y1] = 1
        Next
        Next
    End Method
End Class

Global myroom:room = New room()
Global mymonster:List<monster> = New List<monster>


Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)                
        For Local i=0 Until 10
            mymonster.AddLast(New monster())
        Next
    End Method
    Method OnUpdate()        
        myroom.update
        For Local i:=Eachin mymonster
            i.update
        Next        
    End Method
    Method OnRender()
        Cls(0,0,0)
        myroom.draw
        For Local i:=Eachin mymonster
            i.draw
        Next         
        SetColor 255,255,255
        DrawText     "Monsters in a dungeon. (shitting hamster algorithm)",
                    2,2
    End
End


Function Main()
    New MyGame()
End

Thursday, August 13, 2015

Monkey-X - Random room placement map creator - example code


I wanted to try and create a map generator for games like rpg's and such. I created code here that randomly finds a spot for a new room on the map and puts it there if it its next to another room with enough space to walk through.

Import mojo

Class room
    Field mapwidth:Int=640/12
    Field mapheight:Int=480/12
    Field tilewidth:Int=12
    Field tileheight:Int=12
    Field map:Int[640/12][]
    Field refreshmaptime:Int=0
    Method New()
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        newmap
    End Method    
    Method update()
        If KeyHit(KEY_SPACE) Then 
        newmap
        refreshmaptime=0
        End If
        refreshmaptime+=1
        If refreshmaptime>200 Then 
            newmap
            refreshmaptime=0
        End If
    End Method
    Method draw()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If map[x][y] = 1
                SetColor 150,150,150
                DrawRect     x*tilewidth,y*tileheight,
                            tilewidth,tileheight
            End If
            If map[x][y] = 2
                SetColor 200,200,200
                DrawRect     x*tilewidth,y*tileheight,
                            tilewidth,tileheight
            End If            
        Next
        Next
    End Method
    Method newmap()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            map[x][y] = 0
        Next
        Next
        drawrectinmap(Rnd(10,mapwidth-10),Rnd(10,mapheight-10),5,3)
        For Local i=0 Until 10
            makeroom
        Next
        makewalls
    
    End Method
    Method makewalls()
        ' put walls on the map
        For Local y=1 Until mapheight-1
        For Local x=1 Until mapwidth-1
            If map[x][y] = 0
                If map[x+1][y] = 1
                    map[x+1][y] = 2
                End If                
            End If
            If map[x][y] = 1
                If map[x+1][y] = 0
                    map[x][y] = 2
                End If
            End If
            If map[x][y] = 1
                If map[x][y+1] = 0
                    map[x][y] = 2
                End If
            End If
            If map[x][y] = 0
                If map[x][y+1] = 1
                    map[x][y+1] = 2
                End If
            End If
        Next
        Next
    End Method
    Method makeroom:Bool()
        'find suitable place to make room
        Local exitloop:Bool=False
        Local cnt:Int=0
        While exitloop = False
            cnt+=1
            If cnt>8000 Then exitloop=True
            Local x:Int=Rnd(5,mapwidth-8)
            Local y:Int=Rnd(5,mapheight-8)
            Local roomw:Int=Rnd(4,8)
            Local roomh:Int=Rnd(4,8)
            Local pass1:Bool=True
            For Local y1=0 Until roomh
            For Local x1=0 Until roomh
                If map[x1+x][y1+y] = 1 Then pass1=False
            Next
            Next
            Local pass2:Bool=False
            If pass1=True Then
                For Local y1=3 To roomh-3
                    If map[x-1][y+y1] = 1 Then pass2=True
                    If map[x+roomw][y+y1] = 1 Then pass2=True
                Next
                 For Local x1=3 To roomw-3
                    If map[x+x1][y-1] = 1 Then pass2 = True
                    If map[x+x1][y+roomh] = 1 Then pass2 = True
                Next
            End If
            If pass2 = True Then
                drawrectinmap(x,y,roomw,roomh)
                Return
            End If
        Wend
    End Method
    Method issuitable:Bool(x:Int,y:Int,w:Int,h:Int)
        For Local y1=0 Until h
        For Local x1=0 Until w
            If map[x+x1][y+y1] = 1 Then Return False
        Next
        Next
        Return True
    End Method
    Method drawrectinmap(x:Int,y:Int,w:Int,h:Int)
        For Local y1=0 Until h
        For Local x1=0 Until w
            map[x+x1][y+y1] = 1
        Next
        Next
    End Method
End Class

Global myroom:room = New room()

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
        myroom.update
    End Method
    Method OnRender()
        Cls(0,0,0)
        myroom.draw
        SetColor 255,255,255
        DrawText     "Press spacebar to create new map ",
                    2,2
    End
End


Function Main()
    New MyGame()
End

Wednesday, August 12, 2015

Monkey-X - RPG - Beginners - Selector with 3 options - code example


Here a example for beginning programmers of the RPG genre. Use the cursor keys to move inside the selector.

Import mojo

Class rpgselector
    Field x:Int,y:Int
    Field w:Int=180,h:Int=70
    Field title:String
    Field opt1:String,opt2:String,opt3:String
    Field cs:Int=0
    Method New(    title:String,opt1:String,opt2:String,
                opt3:String,x:Int,y:Int)
        Self.x = x
        Self.y = y
        Self.opt1 = opt1
        Self.opt2 = opt2
        Self.opt3 = opt3
        Self.title = title
    End Method
    Method update()
        If KeyHit(KEY_RIGHT)
            If cs = 0 Then cs = 1 ; Return
            If cs = 2 Then cs = 1 ; Return
        End If
        If KeyHit(KEY_DOWN)
            If cs = 0 Then cs = 2 ; Return
            If cs = 2 Then cs = 1 ; Return
            If cs = 1 Then cs = 2 ; Return
        End If
        If KeyHit(KEY_UP)
            If cs = 2 Then cs = 0 ; Return
            If cs = 1 Then cs = 0 ; Return
        End If
        If KeyHit(KEY_LEFT)
            If cs = 1 Then cs = 0 ; Return
        End If
    End Method
    Method draw()
        SetColor 100,100,100
        DrawRect x,y,w,h
        SetColor 255,255,255
        DrawText title,x+20,y-5
        DrawText opt1,x+20,y+20
        If cs = 0 Then DrawRect x+10,y+22,8,8
        DrawText opt2,x+90,y+20
        If cs = 1 Then DrawRect x+80,y+22,8,8        
        DrawText opt3,x+20,y+40
        If cs = 2 Then DrawRect x+10,y+42,8,8        
    End Method
End Class

Global myrpgsel:rpgselector

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
        myrpgsel = New rpgselector(    "Action",
                                    "Option 1",
                                    "Option 2",
                                    "Option 3",
                                    300,100)
    End Method
    Method OnUpdate()        
        myrpgsel.update
    End Method
    Method OnRender()
        Cls(0,0,0)
        SetColor 255,255,255
        DrawText     "Use the cursor keys to move through the"+
                    "selector...",2,2
        myrpgsel.draw
    End
End


Function Main()
    New MyGame()
End

Getting started/learning/getting into game programming.


  • Invest enough hours in this.
  • It would probably be useful to learn how to use drawing/animation programs.
  • It would be usefull if you learn to model in 3d editing programs.
  • Video editing is something that you will probably need to learn about.
  • It could help if you could use sound/music editing programs.
  • Look for tutorial video's.
  • Learn a couple of languages so you will be able to read more example code online and in books ect.
  • Do not be afraid to ask questions on game programming forums. (Do look if the question has not been asked yet.)
  • Look at gameplay video's and think on how you would be able to program cenrtain things from that yourself.
  • Study games by playing them.
  • Spend money to learn how to programming better.
  • Buy and read programming books.
  • Buy tutorials/art/programming languages/libraries/tools.
  • Pay someone to teach you specific things.
  • Make reusable code/parts. (lego coding)
  • Try and stay motivated and keep it fun.
  • If you spend a lot of time learning game programming then you will get better. 
  • Make a lot of things.

Monkey-X - RPG - Beginners - Making requesters code example



Here a example of how to make draw some graphics for a rpg. It should learn you how you can create a full screen of for instance a battle screen.

Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls(0,0,0)
        SetColor 100,100,100
        DrawRect 100,100,100,60
        SetColor(255,255,255)
        drawboxedrect(100,100,100,60)
        SetColor 255,255,255
        DrawText "Player",120,95
        DrawText "Bash",110,110
        DrawText "PSI",110,130
        DrawText "Goods",150,110
    End
End

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

Function Main()
    New MyGame()
End