TGC Codebase Backup



Simple 2D tiling system by IanM

12th Oct 2003 6:38
Summary

Simple 2D tiling system showing scrolling under keyboard control



Description

This code builds a random map of 40x40 using tiles of 64x64 and allows the user to scroll around it using the cursor keys. Special care has been taken to ensure that only visible tiles are drawn to ensure the fastest redraw speed.

No media required



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    #constant TILE_WIDTH    64
#constant TILE_HEIGHT   64
#constant GRID_WIDTH    40
#constant GRID_HEIGHT   40
#constant MOVEMENT_RATE 2

flush video memory
sync on
sync rate 0
hide mouse

` Build the required images
BuildBackgroundTiles()
BuildSpriteImage()

` Build a random map
global dim TileMap(GRID_WIDTH-1, GRID_HEIGHT-1) as integer
for x=0 to GRID_WIDTH-1
   for y=0 to GRID_HEIGHT-1
      TileMap(x,y)=1+rnd(2)
   next y
next x

x=0
y=0

` Display the player sprite
sprite 1,320,240,10
offset sprite 1,31,31

TilesAcross=screen width() / TILE_WIDTH
TilesDown=screen height() / TILE_HEIGHT

` This array determines the rotation of the central sprite based on its current movement
dim Rotation(9) as float
Rotation(1) = -45.0
Rotation(2) = 0.0
Rotation(3) = 45.0
Rotation(4) = -90.0
Rotation(5) = 0.0
Rotation(6) = 90.0
Rotation(7) = -135.0
Rotation(8) = 180.0
Rotation(9) = 135.0

do

   DPos = 5

   ` This control routine adjusts the coordinates to start plotting the map at, so ...
   ` moving left will increase the tiled-map coordinates, moving the map right. The same
   ` goes for the up/down/right directions.
   if leftkey()=1 and x<0 then inc x,MOVEMENT_RATE : dec DPos
   if rightkey()=1 and x>(0-(TILE_WIDTH*(GRID_WIDTH-TilesAcross))) then dec x,MOVEMENT_RATE : inc DPos

   if upkey()=1 and y<0 then inc y,MOVEMENT_RATE : dec DPos, 3
   if downkey()=1 and y>(0-(TILE_HEIGHT*(GRID_HEIGHT-TilesDown))) then dec y,MOVEMENT_RATE : inc DPos, 3

   if DPos <> 5 then rotate sprite 1, Rotation(DPos)

   DrawMap(x,y)

   sync
loop
wait key
end


function DrawMap(ScreenX as integer, ScreenY as integer)
   local BitmapStartX as integer
   local BitmapStartY as integer
   local MapStartX as integer
   local MapStartY as integer
   local CurrentX as integer
   local CurrentY as integer
   local MapX as integer
   local MapY as integer
   local BitmapWidth as integer
   local BitmapHeight as integer

   ` The next two 'if' commands determine the x/y coords in the TileMap array to use to
   ` start with, and the screen x/y coords to start displaying them at
   if ScreenX >= 0
      BitmapStartX = ScreenX
      MapStartX = 0
   else
      BitmapStartX = ScreenX mod TILE_WIDTH
      MapStartX = abs(ScreenX / TILE_WIDTH)
   endif

   if ScreenY >= 0
      BitmapStartY = ScreenY
      MapStartY = 0
   else
      BitmapStartY = ScreenY mod TILE_HEIGHT
      MapStartY = abs(ScreenY / TILE_HEIGHT)
   endif

   ` Cache the bitmap size for a minor speed increase
   BitmapWidth = bitmap width()-1
   BitmapHeight = bitmap height()-1

   ` Start displaying row-by-row
   MapX = MapStartX
   for CurrentX = BitmapStartX to BitmapWidth step TILE_WIDTH
      if MapX >= GRID_WIDTH then exit

      MapY = MapStartY

      for CurrentY = BitmapStartY to BitmapHeight step TILE_HEIGHT
         if MapY >= GRID_HEIGHT then exit

         paste image TileMap(MapX, MapY), CurrentX, CurrentY

         inc MapY
      next CurrentY

      inc MapX
   next CurrentX
endfunction

function BuildBackgroundTiles()
   box 0,0,TILE_WIDTH,TILE_HEIGHT,rgb(255,0,0),rgb(0,0,255),rgb(0,255,0),rgb(127,0,127)
   get image 1,0,0,TILE_WIDTH,TILE_HEIGHT,1
   box 0,0,TILE_WIDTH,TILE_HEIGHT,rgb(0,0,0), rgb(255,255,255), rgb(255,255,255), rgb(0,0,0)
   get image 2,0,0,TILE_WIDTH,TILE_HEIGHT,1
   box 0,0,TILE_WIDTH,TILE_HEIGHT, rgb(255,255,255), rgb(0,0,0),rgb(0,0,0), rgb(255,255,255)
   get image 3,0,0,TILE_WIDTH,TILE_HEIGHT,1
endfunction

function BuildSpriteImage()
   cls
   for x=0 to 63
      line 31,0,x,63
   next x
   dot 31,0,rgb(255,0,0)
   get image 10,0,0,64,64,1
endfunction