Friday, December 26, 2014

Monkey-X - 2D Sidescrolling map code Example.


The code below shows you how to create a 2d sidescrolling map. I did not add a player to keep the code small.

Code Below : (Copy and paste it into a empt project in your Monkey editor)

Import mojo

' The most left and most right tiles do not get drawn.
Global level:Int[][] = [    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
                            [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]

' Level array x location (left of the screen)
Global mapx:Int = 20
' Scrolling offset
Global mapsx:Int = 0

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
        If KeyDown(KEY_LEFT)
            If mapx > 0 Then mapsx+=1
            If mapsx > 31
                mapsx = 0
                If mapx > 0
                    mapx -= 1
                End If
            End If
        End If
        If KeyDown(KEY_RIGHT) 
            If mapx < 29 Then mapsx-=1
            If mapsx < 1
                mapsx = 32
                If mapx < 29
                    mapx += 1
                End If
            End If
        End If
    End Method
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,255,255)
        For Local y=0 Until 10
        For Local x=0 Until 21
                If level[y][x+mapx] = 1
                    DrawRect x*32+mapsx-32,y*32,32,32
                End If
        Next
        Next
        DrawText "mapsx:"+mapsx+" mapx:"+mapx,10,10
        DrawText "Use cursor Left and Right to Scroll the map.",10,20
    End
End

Function Main()
    New MyGame()
End

No comments:

Post a Comment