Sunday, December 25, 2016

Monkey-X - Hollyday Greetings Screen - code example

Import mojo

Global tr:Int=90
Global tg:Int=10
Global tb:Int=150

Class dot
    Field x:Float,y:Float
    Field rad:Int
    Field alpha:Float
    Field r:Int,g:Int,b:Int
    Method New()
        x = Rnd(640)
        y = Rnd(480)
        rad = Rnd(3,7)
        alpha = Rnd(0.4,0.8)
        Local c:Int=Rnd(50,255)
        If Rnd()<.8 Then 
            r = 255
            g = 255
            b = 255
            If Rnd()<.5
                rad=2
            End If
        else
                r = c/2+tr/2
                g = c/2+tg/2
                b = c/2+tb/2
        End If
    End Method
    Method draw()
        SetColor r,g,b
        SetAlpha alpha
        DrawCircle x,y,rad
    End Method
End Class

Class flake
    Field x:Float,y:Float
    Field angle:Float
    Field size:Float
    Field thick:Bool
    Field alpha:Float
    Method New(x:Float,y:Float,size:Int)
        Self.x = x
        Self.y = y
        Self.size = size
        alpha = Rnd(0.2,0.9)
        angle = Rnd(360)
        If Rnd()<.5 Then thick=True Else thick=False
    End Method
    Method draw()
        SetAlpha alpha
        PushMatrix()
        Translate(x,y)
        Rotate(angle)    
        Translate(-x,-y)
        SetColor 255,255,255
        For Local i=0 To 360-45 Step 45
            Local x1:Int=x+Cos(i)*size
            Local y1:Int=y+Sin(i)*size
            Local size2:Float=size/5+size/5
            For Local i2=0 Until 3
                Local x2:Int=x+Cos(i)*size2
                Local y2:Int=y+Sin(i)*size2
                Local size3:Int
                If i2 = 0 Then size3=size/4
                If i2 = 1 Then size3=size/3
                If i2 = 2 Then size3=size/5
                Local x3:Int=x2+Cos(i-40)*size3
                Local y3:Int=y2+Sin(i-40)*size3
                Local x4:Int=x2+Cos(i+40)*size3
                Local y4:Int=y2+Sin(i+40)*size3
                DrawLine x2,y2,x3,y3
                DrawLine x2,y2,x4,y4
                If thick = True
                    DrawLine x2+1,y2,x3+1,y3
                    DrawLine x2+1,y2,x4+1,y4
                End If
                size2+=size/5
            Next
            DrawLine x,y,x1,y1
            If thick = True Then
                DrawLine x+1,y,x1+1,y1            
            End If
        Next
        PopMatrix()
        SetAlpha 1
    End Method
    Method newang:Int(ang1:Int,m:Int)
        ang1 += m
        If ang1<0 Then Return 360+m
        If ang1>359 Then Return 0+m
    End Method
End Class

Global myflakes:List<flake> = New List<flake>
Global mydots:List<dot> = New List<dot>

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        Seed = 3
        For Local i=0 Until 170
            mydots.AddLast(New dot())
        Next
        For Local y=0 Until 480+128 Step 128
        For Local x=0 Until 640+128 Step 128
            Local mx:Int=Rnd(-64,64)
            Local my:Int=Rnd(-64,64)
            myflakes.AddLast(New flake(x+mx,y+my,Rnd(32,64)))
        Next
        Next

    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls tr,tg,tb
        SetColor 255,255,255
        For Local i:=Eachin mydots
            i.draw
        Next
        For Local i:=Eachin myflakes
            i.draw
        Next
        Scale 4,4
        SetAlpha .76
        SetColor 255,255,255
        DrawText "Merry Christmas",DeviceWidth/(2*4),20,.5,.5
        DrawText "And a",DeviceWidth/(2*4),DeviceHeight/4/4,.5,.5
        DrawText "Happy new Year",DeviceWidth/(2*4),DeviceHeight/3/4,.5,.5        
        Scale 1,1
        SetAlpha 1
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Random Maze and Random Cave tiles - code example


Import mojo

Global sw:Int=640
Global sh:Int=480
Global mapwidth:Int=19
Global mapheight:Int=15
Global smooth1:Int=10
Global minsmooth1:Int=3
Global maxsmooth1:Int=30
Global smooth2:Int=20
Global minsmooth2:Int=3
Global maxsmooth2:Int=30
Global myseed:Int=300
Global tilewidth:Int=32
Global tileheight:Int=32
Global maxsize:Int=tilewidth/2
Global size:Int=tilewidth/2

Class map
    Field image:Image[][]
    Field imagepixels:Int[]
    Field imagemap:Int[][]
    Field map1:Int[][]
    Field map2:Int[][]
    Field map3:Int[][][]
    Field mazex:Stack<Int> = New Stack<Int>
    Field mazey:Stack<Int> = New Stack<Int>            
    Method New()
        imagemap = New Int[tilewidth][]
        For Local i=0 Until tilewidth
            imagemap[i] = New Int[tileheight]
        Next        
        map1 = New Int[mapwidth][]
        For Local i=0 Until mapwidth
            map1[i] = New Int[mapheight]
        Next        
        
        map2 = New Int[mapwidth/2][]
        map3 = New Int[mapwidth/2][][]    
        For Local i=0 Until mapwidth/2
            map2[i] = New Int[mapheight/2]
            map3[i] = New Int[mapheight/2][]
            For Local z = 0 Until mapheight/2
                map3[i][z] = New Int[4]
            Next            
        Next
        image = New Image[mapwidth][]
        For Local i=0 Until mapwidth
            image[i] = New Image[mapheight]
        Next
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            image[x][y] = CreateImage(tilewidth,tileheight)
        Next
        Next
        imagepixels = New Int[tilewidth*tileheight]
        Seed = myseed
        makemaze()
        tilesonmap()
        'maketile()
        'map1[0][0]=1
        'finalizeimage(0,0)


    End Method
    Method tilesonmap()
        Local top:Bool=False
        Local right:Bool=False
        Local bottom:Bool=False
        Local left:Bool=False
        For Local y=1 Until mapheight-1
        For Local x=1 Until mapwidth-1
            If map1[x][y] = 1
            If map1[x][y-1] = 1 Then top=True Else top=False
            If map1[x+1][y] = 1 Then right=True Else right=False
            If map1[x][y+1] = 1 Then bottom=True Else bottom=False
            If map1[x-1][y] = 1 Then left=True Else left = False
            maketile(top,right,bottom,left)
            finalizeimage(x,y)
            End If
        Next
        Next

        For Local x=1 Until mapwidth-1
            top=False
            If map1[x][0] = 1
            If map1[x+1][0] = 1 Then right = True Else right=False
            If map1[x][1] = 1 Then bottom = True Else bottom=False
            If map1[x-1][0] = 1 Then left = True Else left=False
            maketile(top,right,bottom,left)
            finalizeimage(x,0)            
            End If
            bottom=False
            If map1[x][mapheight-1] = 1
            If map1[x][mapheight-2] = 1 Then top=True Else top=False
            If map1[x+1][mapheight-1] = 1 Then right=True Else right =False
            If map1[x-1][mapheight-1] = 1 Then left = True Else left=False
            maketile(top,right,bottom,left)
            finalizeimage(x,mapheight-1)            
            End If            
        Next


        For Local y=1 Until mapheight-1
            left=False
            If map1[0][0] = 1
            If map1[0][y-1] = 1 Then top = True Else top=False
            If map1[1][y] = 1 Then right=True Else right=False
            If map1[0][y+1] = 1 Then bottom=True Else bottom=False
            maketile(top,right,bottom,left)
            finalizeimage(0,y)            
            End If
            right=False
            If map1[mapwidth-1][y] = 1
            If map1[mapwidth-1][y-1] = 1 Then top=True Else top=False
            If map1[mapwidth-1][y+1] = 1 Then bottom = True Else bottom=False
            If map1[mapwidth-2][y] = 1 Then left=True Else left = False
            maketile(top,right,bottom,left)
            finalizeimage(mapwidth-1,y)            
            End If            
        Next

        
        If map1[mapwidth-1][mapheight-1] = 1
            bottom=False
            right=False
            If map1[mapwidth-2][mapheight-1] = 1 Then left=True Else left=False
            If map1[mapwidth-1][mapheight-2] = 1 Then top=True Else top=False
            maketile(top,right,bottom,left)
            finalizeimage(mapwidth-1,mapheight-1)            

        End If

    End Method
    Method maketile(top:Bool=False,right:Bool=False,bottom:Bool=False,left:Bool=False)
        For Local y=0 Until tileheight
        For Local x=0 Until tilewidth
            imagemap[x][y] = 0
        Next
        Next
        smooth1 = Rnd(minsmooth1,maxsmooth1)
        smooth2 = Rnd(minsmooth2,maxsmooth2)
        If Rnd()<.8 Then smooth1 = Rnd(minsmooth1,10)
        Local ang:Int=0
        Local x:Int=tilewidth/2
        Local y:Int=tileheight/2
        Local d:Int=size-1
        Local linex:Stack<Int> = New Stack<Int>
        Local liney:Stack<Int> = New Stack<Int>
        Local drawme:Stack<Bool> = New Stack<Bool>
        Local ex:Bool=False
        Local topset:Bool=False
        Local rightset:Bool=False
        Local bottomset:Bool=False
        Local leftset:Bool=False
        If right=True Then ang=17
        linex.Push(x+Cos(ang)*d)
        liney.Push(y+Sin(ang)*d)
        drawme.Push(True)
        While ang<(360-smooth2)
            ang+=smooth2
            If bottom=True And bottomset=False
            If (ang>70 And ang<120) Then 
                bottomset=True
                ang=90-20
                linex.Push(x+Cos(ang)*d)'
                 liney.Push(y+tileheight/2)
                 drawme.Push(True)
                ang=90+21
                   linex.Push(x+Cos(ang)*d)'
                 liney.Push(y+tileheight/2)
                 drawme.Push(False)
                ang+=smooth2
               End If
               End If
            If left=True And leftset=False
            If (ang>160-smooth2 And ang<200) Then 
                leftset=True
                ang=180-20
                linex.Push(x-tilewidth/2)'
                 liney.Push(y+Sin(ang)*d)
                 drawme.Push(True)                 
                ang=180+21
                linex.Push(x-tilewidth/2)'
                 liney.Push(y+Sin(ang)*d)
                 drawme.Push(False)                 
                ang+=smooth2
               End If
               End If
            If top=True And topset=False
            If (ang>250 And ang<290) Then 
                topset=True
                ang=270-20
                linex.Push(x+Cos(ang)*d)'
                 liney.Push(y-tileheight/2)
                 drawme.Push(True)
                ang=270+21
                linex.Push(x+Cos(ang)*d)'
                 liney.Push(y-tileheight/2)
                 drawme.Push(False)                 
                ang+=smooth2
               End If
               End If
            If right=True And rightset=False
            If (ang>340-smooth2) Then
                rightset=True
                ang=360-20
                linex.Push(x+tilewidth/2)'
                 liney.Push(y+Sin(ang)*d)
                 drawme.Push(True)
                ang=21
                linex.Push(x+tilewidth/2)'
                 liney.Push(y+Sin(ang)*d)
                 drawme.Push(False)                 
                ang+=smooth2
                ang=360
                ex=True
               End If
               End If
               If ex=False
            d = size-1
            Local d2:Int=d+Rnd(-d/smooth1,d/smooth1)
            If d2>maxsize Then d2=maxsize
            linex.Push(x+Cos(ang)*d2)
            liney.Push(y+Sin(ang)*d2)
               drawme.Push(True)            
            'linex.Push(x+Cos(ang)*d)'
            'liney.Push(y+Sin(ang)*d)
            End If
        Wend

        linex.Push(linex.Get(0))
        liney.Push(liney.Get(0))
        drawme.Push(True)        
        For Local i=1 Until linex.Length
            Local x1:Int=linex.Get(i-1)
            Local y1:Int=liney.Get(i-1)
            Local x2:Int=linex.Get(i)
            Local y2:Int=liney.Get(i)            
            If drawme.Get(i) = True Then
            'If drawme.Get(i) = True
            line x1,y1,x2,y2
            'End if
            End If
        Next
        '    
        floodimagemap(2) 'walkable tiles is no 2
    End Method
    Method floodimagemap(fillval:Int)
        Local fx:Stack<Int> = New Stack<Int>
        Local fy:Stack<Int> = New Stack<Int>
        Local mx:Int[] = [0,1,0,-1]
        Local my:Int[] = [-1,0,1,0]
        fx.Push(tilewidth/2)
        fy.Push(tileheight/2)
        While fx.Length > 0
            Local x1:Int=fx.Top
            Local y1:Int=fy.Top
            fx.Pop
            fy.Pop
            For Local i=0 Until 4
                Local x2:Int=x1+mx[i]
                Local y2:Int=y1+my[i]
                If x2>=0 And x2<tilewidth And y2>=0 And y2<tileheight
                If imagemap[x2][y2] = 0
                    imagemap[x2][y2] = fillval
                    fx.Insert(0,x2)
                    fy.Insert(0,y2)
                End If
                End If
            Next
        Wend
    End Method
    Method finalizeimage(tx:Int,ty:Int)
        For Local i=0 Until tilewidth*tileheight
            imagepixels[i] = 0
        Next
        Local cnt:Int=0
        For Local y=0 Until tileheight
        For Local x=0 Until tilewidth
        If imagemap[x][y] = 1
        imagepixels[cnt] = argb(255,255,255)
        End If
        If imagemap[x][y] = 2
        imagepixels[cnt] = argb(55,55,55)
        End If

        cnt+=1
        Next
        Next
        image[tx][ty].WritePixels(imagepixels, 0, 0, tilewidth, tileheight, 0)        
    End Method
    
    Method makemaze()
        Local ax:Int[] = [0,1,0,-1]
        Local ay:Int[] = [-1,0,1,0]
        mazex.Push(Rnd(mapwidth/2))
        mazey.Push(Rnd(mapheight/2))                                
        While mazex.IsEmpty = False            
            Local x:Int=mazex.Top
            Local y:Int=mazey.Top
            Local d:Int[] = New Int[4]
            Local deadend:Bool=True
            For Local i:=0 Until ax.Length
                Local x2:Int=x+ax[i]
                Local y2:Int=y+ay[i]
                If x2>=0 And x2<mapwidth/2 And y2>=0 And y2<mapheight/2
                    If map2[x2][y2] = 0 
                        d[i] = 1
                        deadend=False
                    End If
                End If
            Next
            
            If deadend = False                
                Local eloop:Bool=False
                While eloop = False
                    Local r:Int=Rnd(0,4)
                    If d[r] = 1 Then                        
                        eloop = True
                        Local nx:Int=x+ax[r]
                        Local ny:Int=y+ay[r]
                        map3[x][y][r] = 1                                                                        
                        mazex.Push(nx)
                        mazey.Push(ny)                        
                        map2[x][y] = 1
                        map2[nx][ny] = 1
                    End If
                Wend
            Else    ' if nothing happened than backtrace
                mazex.Pop()
                mazey.Pop()
            End If
        Wend
        ' convert the map into a tilemap
        For Local y:=0 Until mapheight/2
        For Local x:=0 Until mapwidth/2
            Local x2:Int=x*2
            Local y2:Int=y*2
            If map2[x][y] = 1 Then map1[x2+1][y2+1] = 1
            If map3[x][y][0] = 1 Then map1[x2+1][y2] = 1
            If map3[x][y][1] = 1 Then map1[x2+2][y2+1] = 1
            If map3[x][y][2] = 1 Then map1[x2+1][y2+2] = 1
            If map3[x][y][3] = 1 Then map1[x2][y2+1] = 1            
        Next
        Next                
    End Method    
    Method line:Void(x1:Int,y1:Int,x2:Int,y2:Int)
        Local dx:Int, dy:Int, sx:Int, sy:Int, e:Int
        dx = Abs(x2 - x1)
          sx = -1
          If x1 < x2 Then sx = 1      
          dy = Abs(y2 - y1)
          sy = -1
          If y1 < y2 Then sy = 1
          If dx < dy Then 
              e = dx / 2 
          Else 
              e = dy / 2          
          End If
          Local exitloop:Bool=False
          While exitloop = False
              If x1>=0 And x1<tilewidth And y1>=0 And y1<tileheight
            imagemap[x1][y1] = 1
            End If
            
            If x1 = x2 
                If y1 = y2
                    exitloop = True
                End If
            End If
            If dx > dy Then
                x1 += sx ; e -= dy 
                  If e < 0 Then e += dx ; y1 += sy
            Else
                y1 += sy ; e -= dx 
                If e < 0 Then e += dy ; x1 += sx
            Endif
          Wend
    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     
    Method draw()    
        SetColor 255,255,255
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            If map1[x][y] = 1
                DrawImage image[x][y],x*tilewidth,y*tileheight
            End If
        Next
        Next
    End Method
End Class

Global mymap:map

Class MyGame Extends App
    Field cnt:Int=0
    Method OnCreate()
        SetUpdateRate(60)
        myseed=1
        mymap = New map
    End Method
    Method OnUpdate()
        cnt+=1
        If MouseDown(MOUSE_LEFT) Or cnt>120
            cnt=0
            myseed = Millisecs()
            mymap = New map
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        mymap.draw
        SetColor 255,255,255
        DrawText "Press mouse to draw new map or wait",0,0
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Flood Fill (seed fill) and Distance - code example

Import mojo

Global screenwidth:Int=640
Global screenheight:Int=480
Global mapwidth:Int=20
Global mapheight:Int=20
Global tilewidth:Float=Float(screenwidth)/Float(mapwidth)
Global tileheight:Float=Float(screenheight)/Float(mapheight)

Class MyGame Extends App
    Field flood:Bool=False
    Field fillval:Int=1
    Field delay:Int
    Field map:Int[][]
    Field mapd:Int[][] 'map containing the distance (distance map)
    Field floodx:Stack<Int> = New Stack<Int> 'flood 
    Field floody:Stack<Int> = New Stack<Int>
    Field floodv:Stack<Int> = New Stack<Int> 'distance
    Field mx:Int[] = [0,1,0,-1] 'expand up/right/down/left
    Field my:Int[] = [-1,0,1,0]
    Method OnCreate()
        SetUpdateRate(60)
        map = New Int[mapwidth][]
        For Local i=0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        mapd = New Int[mapwidth][]
        For Local i=0 Until mapwidth
            mapd[i] = New Int[mapheight]
        Next
        For Local x=0 Until mapwidth/2
            map[x][mapheight/2] = 6
        Next
    End Method
    Method OnUpdate()
        Local tx:Int=MouseX()/tilewidth
        Local ty:Int=MouseY()/tileheight
        If MouseDown(MOUSE_LEFT) And flood=False And map[tx][ty] <> 6
            Print "Flooding - "+Millisecs()
            floodx.Clear
            floody.Clear
            map[tx][ty] = fillval
            floodx.Push(tx)
            floody.Push(ty)
            floodv.Push(1)
            flood = True
            fillval+=1
            If fillval > 5 Then fillval = 0
        End If
        If flood = True
            If floodx.Length > 0
            Local x1:Int=floodx.Top
            Local y1:Int=floody.Top
            Local v1:Int=floodv.Top
            floodx.Pop
            floody.Pop
            floodv.Pop
            For Local i=0 Until 4
                Local x2:Int=x1+mx[i]
                Local y2:Int=y1+my[i]
                If x2>=0 And x2<mapwidth And y2>=0 And y2<mapheight
                If Not (map[x2][y2] = fillval) And Not (map[x2][y2] = 6)
                    map[x2][y2] = fillval
                    ' if you insert the new locations at the bottom
                    ' of the list then you will get correct distance values (flooding)
                    floodx.Insert(0,x2)
                    floody.Insert(0,y2)
                    floodv.Insert(0,v1+1)
                    'store the distance in the map
                    mapd[x2][y2] = v1+1
                End If
                End If
            Next
            Else
            flood=False
            Print "Flooding done"
            End If
        End If
    End Method
    Method OnRender()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            Local col:Int=map[x][y]
            SetColor col*30,col*30,col*30
            DrawRect x*tilewidth,y*tileheight,tilewidth+1,tileheight+1
        Next
        Next
        For Local i=0 Until floodx.Length
                SetColor 255,255,0
            DrawCircle floodx.Get(i)*tilewidth+tilewidth/2,floody.Get(i)*tileheight+tileheight/2,tilewidth/2
        Next
        SetColor 255,255,255
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            DrawText mapd[x][y],x*tilewidth+tilewidth-10,y*tileheight+tileheight-10
        Next
        Next
        SetColor 255,255,255
        DrawText "Press Left Mouse to flood map..",0,0
    End Method

End Class

Function Main()
    New MyGame()
End Function

Monkey-X - Cos and Sin fun 01 - code example


Import mojo

Global smooth1:Int=10
Global smooth2:Int=20
Global myseed:Int=300
Global size:Int=50

Class MyGame Extends App
    
    Method OnCreate()
        SetUpdateRate(30)
    End Method
    Method OnUpdate()
        If KeyDown(KEY_RIGHT) Then smooth1+=1
        If KeyDown(KEY_LEFT) Then smooth1-=1
        If smooth1 <1 Then smooth1 = 2
        If smooth1 >100 Then smooth1 = 100
        If KeyDown(KEY_UP) Then smooth2+=1
        If KeyDown(KEY_DOWN) Then smooth2-=1
        If smooth2 <1 Then smooth2 = 2
        If smooth2 >100 Then smooth2 = 100
        If KeyDown(KEY_EQUALS) Then myseed+=1
        If KeyDown(KEY_MINUS) Then myseed-=1
        If KeyDown(KEY_9) Then size-=1
        If KeyDown(KEY_0) Then size+=1
        If size<5 Then size = 5
        If size>200 Then size=200
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        '
        Local sx:Int
        Local sy:Int
        Local ex:Int
        Local ey:Int
        Local ang:Int=0
        Local x:Int=320
        Local y:Int=240
        Local d:Int=size
        Local x1:Int=x+Cos(ang)*d
        Local y1:Int=y+Sin(ang)*d
        sx=x1
        sy=y1
        Seed = myseed

        While ang<(360-smooth2)
            ang+=smooth2
            d = size
            Local x2:Int=x+Cos(ang)*d
            Local y2:Int=y+Sin(ang)*d
            Local d2:Int=d+Rnd(-d/smooth1,d/smooth1)
            Local x3:Int=x+Cos(ang-10)*d2
            Local y3:Int=y+Sin(ang-10)*d2
               DrawLine x1,y1,x3,y3
            DrawLine x3,y3,x2,y2
'            DrawLine x1,y1,x3,y3
            x1=x2
            y1=y2
        Wend
        DrawLine x1,y1,sx,sy
        '
        DrawText "Press Cursor Up and Cursor Down - Cursor Left and Cursor Down",0,0
        DrawText "Press + and -",0,20
        DrawText "Press 9 and 0",0,40
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Random Colored Textures - code example





Import mojo

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


Class texture

    Field mapimage:Image
    Field mappixels:Int[]
    Field map:Int[][]
    Field iw:Int=screenwidth
    Field ih:Int=screenheight

    Field sc:Int=1'Rnd(1,3)
    Method New()   
        mappixels = New Int[iw*ih]
        mapimage = CreateImage(iw,ih)
        map = New Int[iw][]
        For Local i = 0 Until iw
            map[i] = New Int[ih]
        Next

        render1()

    End Method
    Method render1()        
        'noisey layer
        noisylayer()
        If Rnd()<.5 Then cellularlayer()



        ' lines down
        If Rnd()<.5
        Local c:Int=Rnd(1,5)
        For Local i=0 Until c
        Local val:Int=Rnd(-60,60)
        If Rnd()<.5 Then val = Rnd(2,10)
        Local y:Int=0
        Local v1:Int=Rnd(6,20)
        Local v2:Int=Rnd(2,v1)
        While y<ih
        For Local x=0 To iw
            For Local i=0 To v2
            decmappixel(x,y+i,val)
            Next
        Next
        y+=v1
        Wend
        Next
        End If
        ' line right
        If Rnd()<.5
        Local c:Int=Rnd(1,5)
        For Local i=0 Until c
        Local val:Int=Rnd(-60,60)
        If Rnd()<.5 Then val = Rnd(2,10)
        'For Local x=0 To iw Step 8
        Local v1:Int=Rnd(3,iw/20)
        Local v2:Int=Rnd(2,v1)
        Local x=0
        While x<iw
        For Local y=0 To ih 
            For Local i=0 To v2
            decmappixel(x+i,y,val)
            Next
        Next
        x+=v1
        Wend
        Next
        End If
        'lines right
        If Rnd()<.5
        Local c:Int=Rnd(1,5)
        For Local i=0 Until c
        Local val:Int=Rnd(-60,60)
        If Rnd()<.5 Then val = Rnd(2,10)
        Local y:Int=0
        Local v1:Int=Rnd(3,ih/10)
        Local v2:Int=Rnd(2,v1)
        'For Local y=0 To ih Step 8
        While y<ih
        For Local x=0 To iw
            For Local i=0 To v2
            addmappixel(x,y+i,val)
            If Rnd()<.5 Then addmappixel(x+i,y,val)
            Next
        Next
        y+=v1
        Wend
        Next
        End If
        ' lines down
        If Rnd()<.5
        Local c:Int=Rnd(1,5)
        For Local i=0 Until c
        Local t:Int=Rnd(1,5)
        For Local i=0 Until t
        Local val:Int=Rnd(-60,60)
        If Rnd()<.5 Then val = Rnd(2,10)
        Local v1:Int=Rnd(3,iw/5)
        Local v2:Int=v1/2
        Local x:Int=0
        While x<iw
        For Local y=0 To ih 
            For Local i=0 To v2
            addmappixel(x+i,y,val)
            If Rnd()<.5 Then addmappixel(x+i,y,val)
            Next
        Next
        x+=v1
        Wend
        Next
        Next
        End If
        'diagonal lines
        If Rnd()<.5
        Local c:Int=Rnd(1,5)
        For Local i=0 Until c
        Local val:Int=Rnd(-60,60)
        If Rnd()<.5 Then val = Rnd(2,10)
        Local x:Int=0
        Local x1:Int=-iw
        Local v1:Int=Rnd(8,iw/5)
        Local v2:Int=Rnd(3,v1)
        While x1<iw 
        For Local y=0 To ih
            For Local i=0 To v2
                addmappixel(x1+(x+i),y,val)
                'If Rnd()<.5 Then addmappixel(x1+(x+i),y)
            Next
            x+=1
        Next
        x=0
        x1+=v1
        Wend
        Next
        End If

        'blocks
        If Rnd()<.5
            Local x1:Int=0
            Local y1:Int=0
            Local sw:Int=Rnd(15,iw/10)
            Local sh:Int=Rnd(15,ih/10)
            Local sx:Int=Rnd(sw,sw*2)
            Local sy:Int=Rnd(sh,sh*2)
            Local t:Int
            If Rnd()<.5 Then 
                t=Rnd(-80,-30)
                Else 
                t=Rnd(30,80)
            End if
            While y1<ih
                addr2(x1,y1,sw,sh,t)
                x1+=sx
                If x1>iw
                x1=0
                y1+=sy
                End If                
            Wend
        End If


        'triangle up
        If Rnd()<.2 Then addtriangleup
        
        'triangle down
        If Rnd()<.2 Then addtriangledown


        'triangle left
        If Rnd()<.2 Then addtriangleleft
        'triangle Right
        If Rnd()<.2 Then addtriangleright
        If Rnd()<.2 Then addoval()            
        If Rnd()<.5 Then addheightmap
        tintmap
        mapimage.WritePixels(mappixels, 0, 0, iw, ih, 0)
    End Method

    Method update()
        
    End Method

    Method cellularlayer()
        'cellular layer
        For Local y=0 Until ih
        For Local x=0 Until iw
            If Rnd()<.5 Then
                map[x][y] = 1
            Else            
                map[x][y] = 0
            End If
        Next
        Next
        For Local i=0  Until 2
        ' loop through the map
        For Local y=0 Until ih
        For Local x=0 Until iw
            'count the neigbouring 1's 
            Local cnt = 0
            For Local y1=-1 To 1
            For Local x1=-1 To 1
                Local x2=x+x1
                Local y2=y+y1
                If x2>=0 And y2>=0 And x2<iw And y2<ih
                    If map[x][y] = 1 Then cnt+=1
                End If
            Next
            Next
            ' if 3 walls and map is a wall then map x,y is not a wall anymore
            If cnt < 4                
                    map[x][y] = 0
            End If
            ' if more then 4 walls then map x,y is wall
            If cnt >= 5 Then map[x][y] = 1
        Next
        Next
        Next

        For Local x=0 Until iw
        For Local y=0 Until ih
            If map[x][y] = 1 Then 
            addr2(x,y,1,1,100)
            Else
            addr2(x,y,1,1,40)
            End If
        Next
        Next
    End Method


    Method noisylayer()    
        Local var1:Int=Rnd(3,130)
        Local var2:Int=Rnd(20,200)
        Local depth:Int=Rnd(1,25)
        For Local i=0 To iw*ih*depth
            Local x1:Int=Rnd(iw)
            Local y1:Int=Rnd(ih)
            Local cnt:Int=0
            For Local y2=-5 To 5
            For Local x2=-5 To 5
                Local x3:Int=x1+x2
                Local y3:Int=y1+y2
                If x3>=0 And x3<iw And y3>=0 And y3<ih
                If getred(getmappixel(x3,y3)) > var2 Then
                    'addmappixel(x3,y3)
                    cnt+=1    
                End If
                End If
            Next
            Next
            If cnt<var1
                addmappixel(x1,y1,Rnd(2,20))
            End If
            cnt=0
        Next
    End Method
    Method addtriangleup()
        Local val:Int=Rnd(-120,120)
        If Rnd()<.5 Then val = Rnd(2,10)
        Local wa:Float=0
        Local wb:Float=iw 
        Local s:Float=Float(ih)/Float(iw)
        For Local y=0 To ih
        For Local x=wa To wb
            addmappixel(x,y,val)            
        Next
            wa+=s
            wb-=s    
        Next
    End Method


    Method addtriangledown()
        Local val:Int=Rnd(-120,120)
        If Rnd()<.5 Then val = Rnd(2,10)
        Local wa:Float=0
        Local wb:Float=iw 
        Local s:Float=Float(ih)/Float(iw)
        For Local y=ih To 0 Step -1
        For Local x=wa To wb
            addmappixel(x,y,val)            
        Next
            wa+=s
            wb-=s    
        Next
    End Method

    Method addtriangleleft()    
        Local val:Int=Rnd(-120,120)
        If Rnd()<.5 Then val = Rnd(2,10)
        Local ha:Float=0
        Local hb:Float=ih
        Local s:Float=((Float(iw)/Float(ih))/3.5)
        For Local x=iw To 0 Step -1
        For Local y=ha To hb
            addmappixel(x,y,val)            
        Next
            ha+=s
            hb-=s    
        Next
    End Method

    Method addoval()
        'oval
        Local val:Int=Rnd(-100,100)
        If Rnd()<.5 Then val=Rnd(-15,15)
        addo(iw/2,ih/2,ih/2,val)
    End Method

    Method addtriangleright()
        Local val:Int=Rnd(-120,120)
        If Rnd()<.5 Then val = Rnd(2,10)
        Local ha:Float=0
        Local hb:Float=ih
        Local s:Float=((Float(iw)/Float(ih))/3.5)
        For Local x=0 To iw
        For Local y=ha To hb
            addmappixel(x,y,val)            
        Next
            ha+=s
            hb-=s    
        Next
    End Method


    Method addheightmap()
        'heightmap
        Local mw:Int=Rnd(5,iw/10)
        Local mh:Int=Rnd(5,ih/10)
        Local cnt:Float=Rnd(1,4)
        For Local i:Int=0 To iw*ih*cnt
            Local x:Int=Rnd(-10,iw)
            Local y:Int=Rnd(-10,ih)
            Local w:Int=Rnd(3,mw)
            Local h:Int=Rnd(3,mh)
            Local val:Int
            If Rnd()<.5 Then val=-1 Else val=1
            addr(x,y,w,h,val)
        Next        
    End Method

    Method tintmap()
        'tint random
        Local r1:Int=Rnd(0,125)
        Local g1:Int=Rnd(0,125)
        Local b1:Int=Rnd(0,125)
        For Local y=0 Until ih
        For Local x=0 Until iw    
            Local r2:Int=getred(getmappixel(x,y))
            Local g2:Int=r2
            Local b2:Int=r2
            setmappixel(x,y,argb(r1+r2/2,g1+g2/2,b1+b2/2))
        Next
        Next
    End Method

    Method decmappixel(x:Int,y:Int,val:Int=10)
        Local pos:Int=(y*iw)+x
        If pos<0 Or pos>=iw*ih Then Return
        Local r:Int=getred(mappixels[pos])-val
        Local g:Int=getgreen(mappixels[pos])-val
        Local b:Int=getblue(mappixels[pos])-val        
        Local nr:Int=Clamp(r,0,255)
        Local ng:Int=Clamp(g,0,255)
        Local nb:Int=Clamp(b,0,255)         
        mappixels[pos] = argb(nr,ng,nb)
    End Method
    Method addmappixel(x:Int,y:Int,val:Int=10)
        Local pos:Int=(y*iw)+x
        If pos<0 Or pos >=iw*ih Then Return
        Local r:Int=getred(mappixels[pos])+val
        Local g:Int=getgreen(mappixels[pos])+val
        Local b:Int=getblue(mappixels[pos])+val        
        Local nr:Int=Clamp(r,0,255)
        Local ng:Int=Clamp(g,0,255)
        Local nb:Int=Clamp(b,0,255)                
        mappixels[pos] = argb(nr,ng,nb)
    End Method


    Method setmappixel:Int(x:Int,y:Int,col)
        Local pos:Int=(y*iw)+x
        If pos<0 Or pos>=iw*ih Then Return 0
        mappixels[pos] = col
    End Method

    Method getmappixel:Int(x:Int,y:Int)
        Local pos:Int=(y*iw)+x
        If pos<0 Or pos>=iw*ih Then Return 0
        Return mappixels[pos]
    End Method

    Method getpixel2:Int(x:Int,y:Int)
        Local pos:Int=(y*iw)+x
        If pos<0 Or pos>=iw*ih Then Return 0        
        Return mappixels[pos]
    End Method


    Method draw()
        DrawImage mapimage,0,0,0,sc,sc
        SetColor 0,0,0
        DrawRect iw-200,ih-200,200,200
        SetColor 255,255,255
        DrawImage mapimage,iw-200+1,ih-200+1,0,198.0/Float(iw),198.0/Float(ih)
        SetColor 0,0,0
        DrawRect iw-64,ih-64,64,64
        SetColor 255,255,255
        DrawImage mapimage,iw-64+1,ih-64+1,0,62.0/Float(iw),62.0/Float(ih)

    End Method
    Method addr2(x1,y1,w1,h1,val:Int)
        For Local y2=y1 Until y1+h1
        For Local x2=x1 Until x1+w1
            Local pc = y2*iw+x2
            If pc >= 0 And pc < iw*ih
                Local r1:Int=getred(mappixels[pc])+val
                Local g1:Int=getgreen(mappixels[pc])+val
                Local b1:Int=getblue(mappixels[pc])+val
                r1 = Clamp(r1,0,255)                                
                g1 = Clamp(g1,0,255)
                b1 = Clamp(b1,0,255)
                mappixels[pc] = argb(r1,g1,b1)
            End If
        Next
        Next    
       End Method

    Method addr(x1,y1,w1,h1,val:Int)
        For Local y2=y1 Until y1+h1
        For Local x2=x1 Until x1+w1
            Local pc = y2*iw+x2
            If pc >= 0 And pc < iw*ih
                Local r1:Int=getred(mappixels[pc])+val
                Local g1:Int=getgreen(mappixels[pc])+val
                Local b1:Int=getblue(mappixels[pc])+val
                r1 = Clamp(r1,0,255)                                
                g1 = Clamp(g1,0,255)
                b1 = Clamp(b1,0,255)
                If r1>20 Then mappixels[pc] = argb(r1,g1,b1)
            End If
        Next
        Next    
       End Method
    Method drawr(x1,y1,w1,h1,col)
        For Local y2=y1 Until y1+h1
        For Local x2=x1 Until x1+w1
            Local pc = y2*iw+x2
            If pc >= 0 And pc < iw*ih
                mappixels[pc] = col
            End If
        Next
        Next    
       End Method
    Method addo(x1,y1,radius:Float,val:Int=10)
        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*iw+x3
                If pc>=0 And pc < iw*ih
                    Local r1:Int=getred(mappixels[pc])
                    Local g1:Int=getgreen(mappixels[pc])
                    Local b1:Int=getblue(mappixels[pc])                                        
                    Local r2:Int=r1+val
                    Local g2:Int=g1+val
                    Local b2:Int=b1+val
                    r2 = Clamp(r2,0,255)
                    g2 = Clamp(g2,0,255)
                    b2 = Clamp(b2,0,255)                                        
                    mappixels[pc] = argb(r2,g2,b2)
                End If
            End If
        Next
        Next    
    End Method 
    Method drawo(x1,y1,radius:Float,col:Int)
        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*iw+x3
                If pc>=0 And pc < iw*ih
                    mappixels[pc] = col
                End If
            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
    Function getred:Int(rgba:Int)    
        Return((rgba Shr 16) & $FF)    
    End Function              
    Function getgreen:Int(rgba:Int)    
        Return((rgba Shr 8) & $FF)    
    End Function    
    Function getblue:Int(rgba:Int)    
        Return(rgba & $FF)    
    End Function    
    Function getalpha:Int(rgba:Int)    
        Return ((rgba Shr 24) & $FF)    
    End Function    
End Class

Global mytexture:texture

Class MyGame Extends App
    Field cnt:Int=0
    Method OnCreate()
        SetUpdateRate(1)
           Seed = GetDate[5]
        mytexture = New texture()
    End Method
    Method OnUpdate()
        cnt+=1
        If cnt>6 Then
            mytexture = New texture()
            cnt=0
        End If
    End Method
    Method OnRender()
        Cls 50,50,50
        mytexture.draw
        SetColor 255,255,255
     End Method
End Class


Function Main()
    New MyGame()
End Function

Monday, December 19, 2016

Monkey-X - Beginners - image Width and Height - code example


Import mojo

Class MyGame Extends App
    '
    Field image:Image
    '
    Method OnCreate:Int()
        SetUpdateRate(60)        
        ' create an image 
        image = CreateImage(64, 64)
        ' create an array for that image
        Local pixels:Int[image.Width * image.Height]
        ' draw the color red in the pixels array
        For Local i:Int = 0 Until image.Width * image.Height
            pixels[i] = argb(200,0,0)
        Next
        ' copy the array to the image
        image.WritePixels(pixels, 0, 0, image.Width, image.Height, 0)
    End Method
    
    Method OnRender:Int()
        Cls 0,0,0
        DrawImage(image, 50,50)
        SetColor 255,255,255
        DrawText "Image Width is : "+image.Width+" Image Height is : "+image.Height,0,0
    End Method
    
End Class

Function Main:Int()
    New MyGame()
End Function

'helper function
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

Monkey-X - Beginners - Block Commenting #Rem - code example

#Rem
Anything typed here
will not cause a error.
#End
Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(1)
        
        ' You can disable a part of the code between #rem and #end
        #Rem
        DebugLog "This will not"
        DebugLog "be executed."
        #End
    End Method
    Method OnUpdate()
    End Method
    Method OnRender()
    End Method
End Class

Function Main()
    New MyGame()
End Function