Hi all! This is my first code snippet/tutorial here, which shows
how to make a tiled map background for a 2D game. Also, I'm sure
you can apply the same to a 3D plane so you can have a tilemapped
plane for 3D effects (which I might do in the future, who knows!)
P.S. - Attached is the code with media files necessary. Warning: I am not that good of a paint artist! =P
Hey all! This will be my first code snippet/tutorial, which shows how to load a tilemap from a single image that
contains our tiles, and a data file that represents our map. Our tilemap image is a 160x32 image with 5x1 tiles.
Our map file contains linear data for a 25x25 map. So without further ado, let the coding and (hopefully) learning
REM Our data
mapimg = 1000
i = 0
j = 0
Load map data from file.
In this case, it's a 25x25 data file with integers to represent
each tile. In our case though, there's only 5 tile's we're using. :)
open to read 1, "media/mymap.rhymap"
For j = 1 To 25
For i = 1 To 25
read long 1, mymap(i,j)
close file 1
Set our display mode to 640x480x32 and our sync rate to 60 frames per second.
SET DISPLAY MODE 640, 480, 32
SYNC ON : SYNC RATE 60
Here, we load our tiles image as a bitmap, and then set it to the current bitmap
so that way we can use GET IMAGE to get sections from it for 5 images to be used
as tiles. Simplicity at it's best!
The GET IMAGE code here might be a bit confusing, but if you literally replace i
with the numbers from 1 to 5, you can get an idea of what we're doing. Simply put,
all we're doing is getting 32x32 images one right after another from the bitmap.
i.e. - If using coordinates (x1 to x2): (0 to 31) for the first tile, (32 to 63)
for the second, etc...
After that, we reset the current bitmap to the screen (0) so we can draw to it.
LOAD BITMAP "media/tiles2.bmp", mapimg
SET CURRENT BITMAP mapimg
FOR i = 1 TO 5
GET IMAGE i, (i-1)*32, 0, ((i)*32)-1, 31
SET CURRENT BITMAP 0
Our main loop here lies ahead! All that we do in this case is draw our map to the screen.
What we do is we take those tiles we created using GET IMAGE earlier, and paste them to
the screen according to the layout we got from our map data file.
After that, we draw according to the framerate we set up earlier by using SYNC.
FOR j = 1 To 25
For i = 1 To 25
PASTE IMAGE mymap(i,j)+1, (i-1)*31,(j-1)*31