Hi I have been trying to get my head around 2D tile based games and following zenassem post
http://forum.thegamecreators.com/?m=forum_view&t=34076&b=4 I have managed to get my head round most of it.
Here is what I have so far:
sync on
#constant ScreenX 800
#constant ScreenY 600
Set Display Mode ScreenX,ScreenY,32
set window on
Global MapX
Global MapY
Global Xpos
Global Ypos
`MouseX/Y
Global mX
Global mY
`TileBrush Value- selects which tile to paint to map
Global BrushNumber
`Default BrushValue
BrushNumber=2
TotalBrushes = 10
nTilesX=40
nTilesY=40
nLayers=1
TileSizeX=32
TileSizeY=32
CentrescreenX= ScreenX/2
CentrescreenY= ScreenY/2
ViewableTilesX = int (ScreenX / TileSizeX)
ViewableTilesY = int (ScreenY / TileSizeY)
MaxScrollX = NtilesX - ViewabletilesX
MaxScrollY = NtilesY - ViewabletilesY
MapX=0
MapY=0
hide mouse
`Load images
`Cursor
load image "Media\Cursor.bmp",101,0
load image "Media\BlankTile.bmp",102,0
cls
`Array to store brushes
type BrushType
bVal1 as integer
bVal2 as integer
endtype
dim Brushes(TotalBrushes) as BrushType
load image "Media\textures.PNG",100
paste image 100,0,0
`Images 1-11 reserved for Tileset
For grabImg=0 to 10
get image grabImg+1,grabImg*32,0,(grabImg*32)+32,32,1
next grabImg
`Set bval2 of Brushes (Collision true or false)
Brushes(0).bVal2 = 0
Brushes(1).bVal2 = 1
Brushes(2).bVal2 = 1
Brushes(3).bVal2 = 0
Brushes(4).bVal2 = 0
Brushes(5).bVal2 = 1
Brushes(6).bVal2 = 1
Brushes(7).bVal2 = 0
Brushes(8).bVal2 = 0
Brushes(9).bVal2 = 0
Brushes(10).bVal2 = 1
sprite 100,0,0,100
hide sprite 100
`Array to store GridData
type MapData
xpos as integer
ypos as integer
tval as integer
tval2 as integer
tactive as integer
endtype
Dim map(nTilesX-1,nTilesY-1) as MapData
`########################
`Assign Xpos and Ypos Coords to Map- Raw data so coords as they appear on the map and not on the screen.
for aY = 0 to nTilesY-1
For aX = 0 to nTilesX-1
map(aX,aY).Xpos= TileSizeX * (aX)
map(aX,aY).Ypos= TileSizeY * (aY)
` map(aX,aY).tVal2= 1
map(aX,aY).tActive= 0
next aX
next aY
`############
`Xpos and Ypos Values of The players screen
Dim VisibleCoords(ViewableTilesX,ViewableTilesY) as MapData
for vy = 0 to ViewableTilesY
for vx = 0 to ViewableTilesX
VisibleCoords(vx,vy).xpos = TileSizeX * vx
VisibleCoords(vx,vy).ypos = TileSizeY * vy
next vx
next vy
do
GUIActive=0
cls
`##############
`Cursor
mX=MouseX()
mY=MouseY()
sprite 101,mX-16,mY-16,101
set sprite 101,1,1
`test GUI for displaying Tileset
if controlkey()=1 then GUIActive=1
If GUIActive=1
show sprite 100
if sprite collision(101,100)=1 and mouseclick()=1
For SetBrush= 0 to 10
if mX > ((SetBrush) * TileSizeX) and mX < ((SetBrush* TileSizeX) +TileSizeX) then BrushNumber = SetBrush + 1
next SetBrush
endif
else
hide sprite 100
endif
`############
`MouseClickControls
if mouseclick()=1 and controlkey()=0
CurrentX=mX
CurrentY=mY
TileSelectedX= (int(CurrentX)/int(TileSizeX))+MapX
TileSelectedY= (int(CurrentY)/int(TileSizeY))+MapY
map(TileSelectedX,TileSelectedY).tval=BrushNumber
endif
`WASD to move the grid around.
if keystate(32)=1 and mapX < MaxScrollX-1 then inc mapX:wait 100
if keystate(30)=1 and mapX > 0 then dec mapX:wait 100
if keystate(31)=1 and mapY < MaxScrollY-1 then inc mapY:wait 100
if keystate(17)=1 and mapY > 0 then dec mapY:wait 100
`DrawGrid
For DY = MapY to MapY + ViewableTilesY
For DrawGridX =MapX to MapX + ViewableTilesX
If map(DrawGridX,DY).tVal=0 then paste image 102,VisibleCoords(DrawGridX-MapX,DY-MapY).Xpos,VisibleCoords(DrawGridX-MapX,DY-MapY).Ypos
For TtoM = 1 to 11
If map(DrawGridX,DY).tVal=TtoM
paste image Ttom,VisibleCoords(DrawGridX-MapX,DY-MapY).Xpos,VisibleCoords(DrawGridX-MapX,DY-MapY).Ypos
map(DrawGridX,DY).tVal2 = Brushes(Ttom-1).bval2
Endif
Next TtoM
next GrawGridX
next DY
`Hold Shift to show Collisions- I plan to add an option to Manually overide collision
if shiftkey()=1
For DY2 = MapY to MapY + ViewableTilesY
For DrawGridX2 =MapX to MapX + ViewableTilesX
If map(DrawGridX2,DY2).tVal2=1 then paste image 101,VisibleCoords(DrawGridX2-MapX,DY2-MapY).Xpos,VisibleCoords(DrawGridX2-MapX,DY2-MapY).Ypos
next GrawGridX2
next DY2
endif
sync
loop
I have also uploaded a working compiled version with media so you can see for yourself how it works.
Basically You use WASD to move around the grid.
Holding down the Control Key will bring up the Tileset where you then select the brush you want.Then you just paint onto the grid.
Holding Shift will then give you a preview of which tiles have a collision by showing a black box. I plan to change this to a transparent sprite.
The problem I have now is.
A)How do I export this typed array, as I can't use the 'Save Array()' command, for use within my game?
and secondly if anyone can spot any areas where I could do things better please feel free to criticise
Regards,