Need Help to understand how this works

Blitz3D Forums/Blitz3D Programming/Need Help to understand how this works

David819(Posted 2005) [#1]
Hi, i've been working on tile coding for a while and came accross some old code that i had from previous attemps, the problem is i do not under stand how the tiling works. like how the use of the data command sets the tile used etc. i need to know this because i'm trying to add a player but dont know how to identify the specific tile being used for collision or to destroy a tile when i player touches it:

.map
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0


;Set Up Graphics Mode
Graphics 640,480,32,2
SetBuffer BackBuffer()

;Find and print the maximum tiles that can be displayed on screen
maxtilesx = GraphicsWidth() / 32
maxtilesy = GraphicsHeight() / 32
Print maxtilesx
Print maxtilesy


Restore map
Dim tiles(maxtilesx, maxtilesy)
For y=0 To maxtilesy-1
For x=0 To maxtilesx-1
Read tiles(x, y)
Next
Next

Tileset=LoadAnimImage("boxes.bmp",32,32,0,5)



While Not KeyHit(1)

Cls
For y=0 To maxtilesy-1
For x=0 To maxtilesx-1
DrawImage tileset,x*32,y*32,tiles(x,y)
Next
Next


Color 0,0,0
Text 0,0, "tile x: " + (MouseX()/32+1)
Text 0,10, "tile y: " + (MouseY()/32+1)
Flip

Wend
End

Can anyone explain this to me please.


tonyg(Posted 2005) [#2]
Restore Map will start readin the data from the data statements. It create an array dimensioned big enough to fit 640/32 tiles wide and 480/32 tiles high.
In each of the array 'slots' it will place one of the numbers for the data statement...
e.g. tiles(0,0)=0
tiles(1,0)=0
etc
until tiles(19,14)=0
The loadanimimage command will load 5 seperate 'frames' (or in this case tiles) number from 0 to 4. Thes ematch the numbers in the data statement.
The program will then do the while/wend loop. Each loop it clears the screen (not really necessary in this case) and draw a tile in certain positions on the screen
e.g Drawimage tileset,0*32,0*32,tiles(0,0)
As tiles(0,0) is 0 it will draw the first frame of the loadnimimage.
The next tile will be drawn with...
drawimage tileset,1*32,0*32,tiles(1,0)
tiles(1,0) contains the number 0 so the first frame will be drawn at that position.
The loop continues until x=19 and y=14
The text command will then 'print' the number of the current X and Y location.


David819(Posted 2005) [#3]
Ok, Thanks, i think i understand now. thanks again.


Rob Farley(Posted 2005) [#4]
OK before you go any further... STOP!!

Use files instead...

Here's a quick editor I knocked up in about 2 minutes:



Obviously you can expand on this:

Mousewheel increase/decrease number

S to save
L to load


Check out the code, work out what it does... ask any questions! But STOP USING DATA you'll kick yourself later!


David819(Posted 2005) [#5]
Ok Thanks Rob, I'll change that abit later on. I'm curently working on collision but cant get it to work on certain tiles, e.g if tiles(x,y) = 0 and player pos = tiles(x,y) then playerpos = oldpos
If you see what i mean were 0 is a wall, so if the value of tiles(x,y) was 1 it would not collide.

Here is my code, i have got it so that you can not leave the outer walls but can't fig how to get it to work via recognition of the tiles used:

.map
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0


;Set Up Graphics Mode
Graphics 640,480,32,2
SetBuffer BackBuffer()

px = 1
py = 1
oldpx = 0
oldpy = 0

;Find and print the maximum tiles that can be displayed on screen
maxtilesx = GraphicsWidth() / 32
maxtilesy = GraphicsHeight() / 32
Print maxtilesx
Print maxtilesy


Restore map
Dim tiles(maxtilesx, maxtilesy)
For y=0 To maxtilesy-1
For x=0 To maxtilesx-1
Read tiles(x, y)
Next
Next

Tileset=LoadAnimImage("boxes.bmp",32,32,0,5)
player=LoadImage("Player.bmp")

While Not KeyHit(1)

oldpx = px
oldpy = py

Cls

For y=0 To maxtilesy-1
For x=0 To maxtilesx-1
DrawImage tileset,x*32,y*32,tiles(x,y)
Next
Next

If KeyDown(205) Then
px=px+1
EndIf

If KeyDown(203) Then
px=px-1
EndIf

If KeyDown(200) Then
py=py-1
EndIf

If KeyDown(208) Then
py=py+1
EndIf



If px = tiles(x,y) Or px = maxtilesx-1 Then
px = oldpx
EndIf

If py = tiles(x,y) Or py = maxtilesy-1 Then
py = oldpy
EndIf


Text 0,50, px

DrawImage player,px*32,py*32


Color 0,0,0
Text 0,0, "tile x: " + (MouseX()/32+1)
Text 0,10, "tile y: " + (MouseY()/32+1)

Flip

Wend
End

Thanks


tonyg(Posted 2005) [#6]
If tiles(playerx/tilewidth,playery/tileheight)=0 then playerx=oldplayerx and playery=oldplayery


Tobo(Posted 2005) [#7]
Rob - liked your editor. Very neat and small. Was it missing a 'setbuffer backbuffer()' at line 2? Sticking one in seemed to stop the flickering.

Can you explain for us 'simple noobs' why using data is so bad?

Many thanks.

Tobo.


octothorpe(Posted 2005) [#8]
Imagine wanting to be able to load one of several maps, and to be able to choose which one using a variable.


Shifty Geezer(Posted 2005) [#9]
Or imagine designing a game with 20 levels of 200x200 tiles, and then finding a bug in one of the tiles on one of the levels, and staring at screenfulls of digits trying to lock it down.

Using a level editor is sensible in the same way using a paint-program is better for drawing graphics then typing in per-pixel RGB values!


Boiled Sweets(Posted 2005) [#10]
Also it means you could release a level editor and get people to design maps/levels and post them to you.


Rob Farley(Posted 2005) [#11]
Can you explain for us 'simple noobs' why using data is so bad?
As the guys have said, you just include the function loadmap in your code then you can just loadmap("level1.map") .

The more stuff you have 'soft coded' in you game the better (to a certain extent at least) this means that you game engine is just the engine and not all the levels etc.

This means you can create level packs if you want. You can delete levels easily, rearrange levels easily, modify levels easily... generally makes life much easier.

Shifty Geezer pretty much hit the nail on the head with the graphics comparison. You use a graphics editor to do graphcs... You use a level editor to do levels.

It's rare that graphics would be hard coded into your game, likewise, why should levels be hard coded into your game either.

Anyway, there are many many more reasons to have them as external files. But that should keep you going for a while.

Oh, and another thing, you might want to add extra stuff to the level file like, start position, bad guy positions, what bad guys are in the level etc.

Take a look at my Helicopter post and you'll see my fairly extensive level file definition explained there.


David819(Posted 2005) [#12]
tonyg You rock, Thank you.


David819(Posted 2005) [#13]
Ok, i'm now using files for my levels and got all collision working, can any one explain scrolling to me, i've looked at tiling examples but i cant make heads or tails of the scrolling methods used, I'm hoping of eventually making a tile bases final fantasy game.
Hope someone can help.
Thanks.