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
So my display is 1024x700 is their a way i could make it render in quicker?
ReplyDeleteI am not exactly sure what you mean but the command SetUpdateRate(60) is the refresh rate - sync. If you set this to 120 it would render double the amount of times.
ReplyDeleteIt has been a while doing monkeyx so I am not sure if there was a way to drawimage into other images. If you render the tiles inside a image buffer then drawing this to the screen would also be faster.
sorry and thanks for the response im trying to create a sidescroller game of my own im wondering what mapsx is for
ReplyDeletemapsx is a variable that is used to draw the map at a pixel offset value(on the screen) A width in this example of a tile is 32. If we scroll left then we go from 32 to 0 and back to 32 etc and if we scroll to the right we go from 0 to 32 and then 32 again. Here the cursors are used to move the map to the left or right one pixel at a time. mapx and mapy are the actual tile locations.
ReplyDeleteI hope this makes sense. It is hard to describe code :)