|
Post by DJLinux on Dec 4, 2008 12:23:25 GMT -5
96x32 save it as "tilemap.png" for collision detection the ball should be in center of the texture and you must know it's radius 32x32 save it as "ball.png" in the same folder read and learn a litle bit from the code happy coding Joshy ' define usefull consts (UPPERCASE) const FPS = 50 ' Hz. const TILEFILE$ = "tilemap.png" const PLAYERFILE$ = "ball.png" const MONSTERFILE$ = "" const RADIUS = 8 ' radius of the ball in pixel ' ' your first game level ' data 20,15 ' width x height data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1 data 1,0,2,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,1 data 1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 data 1,0,2,2,2,2,0,2,2,2,2,2,2,2,2,2,2,2,2,1 data 1,0,1,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0 data 1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1 data 1,0,0,0,2,2,2,2,2,2,2,2,2,2,0,1,0,1,0,1 data 1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1 data 1,0,1,0,1,0,2,2,2,2,2,2,2,2,0,1,0,1,0,1 data 1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,1,0,1 data 1,0,1,0,1,0,1,0,0,0,1,0,0,0,1,1,0,1,0,1 data 1,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,1,0,1 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1 data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,-1,2,2,2,2,2 ' ' dim your Vars with "meanfull" names ' dim xPlayer,yPlayer,xOld,yOld dim TileCode,nTiles=TexStripFrames(TILEFILE$) dim Tiles(nTiles-1)=LoadTexStrip(TILEFILE$) dim wMap,hMap,xMap,yMap ' ' create your levelmap from DATA statments ' read wMap,hMap dim LevelMap(wMap-1,hMap-1) ' ' return True if player can move to x/y position ' function IsOk(x,y) ' x,y are inside the map? if (x-RADIUS)<0 or (y-RADIUS)<0 then return FALSE end if if ((x+RADIUS)/32)>(wMap-1) or ((y+RADIUS)/32)>(hMap-1) then return FALSE end if yMap=y/32:xMap=(x-RADIUS)/32 ' left if LevelMap(xMap,yMap) then return FALSE:end if xMap=(x+RADIUS)/32 ' right if LevelMap(xMap,yMap) then return FALSE:end if yMap=(y-RADIUS)/32:xMap=x/32 ' top if LevelMap(xMap,yMap) then return FALSE:end if yMap=(y+RADIUS)/32 ' bottom if LevelMap(xMap,yMap) then return FALSE:end if return True end function ' ' read the level ' for yMap = 0 to hMap-1 for xMap = 0 to wMap-1 read TileCode ' is it the player start position if TileCode=-1 then TileCode=0 ' change it back as way texture xPlayer=16+xMap*32 ' get xyPlayerStartPosition yPlayer=16+yMap*32 end if LevelMap(xMap,yMap)=TileCode next next dim TileMap = NewTileMap(Tiles) SprSetTiles (LevelMap) SprSetZorder(1) ' visible under the player and monsters SprSetParallax(FALSE) SprSetXRepeat (FALSE) SprSetYRepeat (FALSE) ' ' load the ball pink = trancparency ' SetTexTransparentCol(255,0,255) dim Player = NewSprite(LoadTex(PLAYERFILE$)) ' move sprite origin to center SprSetXCentre(0.5):SprSetYCentre(0.5) ' begin on start posiion SprSetPos(xPlayer,yPlayer) const SPEED=3 TextMode(TEXT_BUFFERED) ' ' main loop ' While True xOld=xPlayer:yOld=yPlayer if ScanKeyDown(VK_LEFT) then xPlayer=xPlayer-SPEED elseif ScanKeyDown(VK_RIGHT) then xPlayer=xPlayer+SPEED elseif ScanKeyDown(VK_UP) then yPlayer=yPlayer-SPEED elseif ScanKeyDown(VK_DOWN) then yPlayer=yPlayer+SPEED end if ' handle collision dection only if needed if (xOld<>xPlayer) or (yOld<>yPlayer) then if IsOK(xPlayer,yPlayer)=True then SprSetPos(xPlayer,yPlayer) else xPlayer=xOld:yPlayer=yOld end if end if
' refresh the screen DrawText() ' don't eat all the CPU power WaitTimer(1000/FPS) wend
here the same code but now the player is every time in center of the screen/window you need only one command SprCameraSetPos(xPlayer-wWin/2,yPlayer-hWin/2)
' define usefull consts (UPPERCASE) const FPS = 50 ' Hz. const TILEFILE$ = "tilemap.png" const PLAYERFILE$ = "ball.png" const MONSTERFILE$ = "" const RADIUS = 8 ' radius of the ball in pixel ' ' your first game level ' data 20,15 ' width x height data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1 data 1,0,2,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,1 data 1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 data 1,0,2,2,2,2,0,2,2,2,2,2,2,2,2,2,2,2,2,1 data 1,0,1,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0 data 1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1 data 1,0,0,0,2,2,2,2,2,2,2,2,2,2,0,1,0,1,0,1 data 1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1 data 1,0,1,0,1,0,2,2,2,2,2,2,2,2,0,1,0,1,0,1 data 1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,1,0,1 data 1,0,1,0,1,0,1,0,0,0,1,0,0,0,1,1,0,1,0,1 data 1,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,1,0,1 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1 data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,-1,2,2,2,2,2 ' ' dim your Vars here ' dim xPlayer,yPlayer,xOld,yOld dim TileCode,nTiles=TexStripFrames(TILEFILE$) dim Tiles(nTiles-1)=LoadTexStrip(TILEFILE$) dim wMap,hMap,xMap,yMap dim wWin=WindowWidth() dim hWin=WindowHeight()
' ' create your levelmap from DATA statments ' read wMap,hMap dim LevelMap(wMap-1,hMap-1) ' ' return True if player can move to x/y position ' function IsOk(x,y) ' x,y are inside the map? if (x-RADIUS)<0 or (y-RADIUS)<0 then return FALSE end if if ((x+RADIUS)/32)>(wMap-1) or ((y+RADIUS)/32)>(hMap-1) then return FALSE end if yMap=y/32:xMap=(x-RADIUS)/32 ' left if LevelMap(xMap,yMap) then return FALSE:end if xMap=(x+RADIUS)/32 ' right if LevelMap(xMap,yMap) then return FALSE:end if yMap=(y-RADIUS)/32:xMap=x/32 ' top if LevelMap(xMap,yMap) then return FALSE:end if yMap=(y+RADIUS)/32 ' bottom if LevelMap(xMap,yMap) then return FALSE:end if return True end function ' ' read the level ' for yMap = 0 to hMap-1 for xMap = 0 to wMap-1 read TileCode ' is it the player start position if TileCode=-1 then TileCode=0 ' change it back as way texture xPlayer=16+xMap*32 ' get xyPlayerStartPosition yPlayer=16+yMap*32 end if LevelMap(xMap,yMap)=TileCode next next
TextMode(TEXT_BUFFERED)
dim TileMap = NewTileMap(Tiles) SprSetTiles (LevelMap) SprSetZorder(1) ' visible under the player and monsters SprSetParallax(FALSE) SprSetXRepeat (FALSE) SprSetYRepeat (FALSE) ' ' load the ball pink = trancparency ' SetTexTransparentCol(255,0,255) dim Player = NewSprite(LoadTex(PLAYERFILE$)) ' move sprite origin to center SprSetXCentre(0.5):SprSetYCentre(0.5) ' begin on start posiion SprSetPos(xPlayer,yPlayer) ' set the cam over the player SprCameraSetPos(xPlayer-wWin/2,yPlayer-hWin/2)
const SPEED=3 TextMode(TEXT_BUFFERED) ' ' main loop ' While True xOld=xPlayer:yOld=yPlayer if ScanKeyDown(VK_LEFT) then xPlayer=xPlayer-SPEED elseif ScanKeyDown(VK_RIGHT) then xPlayer=xPlayer+SPEED elseif ScanKeyDown(VK_UP) then yPlayer=yPlayer-SPEED elseif ScanKeyDown(VK_DOWN) then yPlayer=yPlayer+SPEED end if ' handle collision dection only if needed if (xOld<>xPlayer) or (yOld<>yPlayer) then if IsOK(xPlayer,yPlayer)=True then SprCameraSetPos(xPlayer-wWin/2,yPlayer-hWin/2) SprSetPos(xPlayer,yPlayer) else xPlayer=xOld:yPlayer=yOld end if end if
' refresh the screen DrawText() ' don't eat all the CPU power WaitTimer(1000/FPS) wend
|
|