Monday, January 5, 2015

Monkey-X - Setting up and using multidimensional arrays (map[x][y]) - tilemap code example


Here an example on how to set up and use a multidimensional array. The code creates an array with the size of the screen in blocks of 32*32 and sets the map values to 0 until 3. It then draws the tilemap on the screen.

Import mojo

Class MyGame Extends App
    Const mapwidth:Int=640/32
    Const mapheight:Int=480/32
    Global map:Int[mapwidth][]

    Method OnCreate()
        SetUpdateRate(60)
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            map[x][y] = Int(Rnd(0,4))
        Next
        Next
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            Select map[x][y]
                Case 0 ; SetColor(255,0,0)
                Case 1 ; SetColor(0,255,0)
                Case 2 ; SetColor(0,0,255)
                Case 3 ; SetColor(255,255,0)                                
            End Select
            DrawRect x*32,y*32,32,32
        Next
        Next
    End
End


Function Main()
    New MyGame()
End

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.