🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

I need some theory behind tile based games...

Started by
47 comments, last by Roots 19 years, 11 months ago
I just want some explenation of how to make tile based maps, i already built the part of my game that actually builds the map and displays the tiles (32x32) but now i need to know how to make the scroll thing... i have no clue.. no clue at all, if you could give me some tutorials of doing it in SDL or just in a lib thats pretty much like sdl that would be great... thanx.. (it would be even greater if you could explain it personally =))
Advertisement
Check the isometric section of the Articles page. There's more info there than you will need. :)
okay thanx... but my tile style is rectangular... but i could probably get some info from there... thanx... i want more answers :)
The formal title of the section is "Isometric and Tile-based Games."
damn it seems really hard =S ill never get trought this
You have two spaces: world space and screen space. The screen is basically just a window that looks at a particular portion of your world. All you have to do is keep track of where in the world you are, and then use that value to calculate where in the tile set to start drawing from. Let's just use the player's character as the point of reference and assume we always want the character at the center of the screen. If the character is at world_x=100 and world_y=100, just look into your tile array and you will start drawing from start_x=world_x-10 and strart_y=world_y-7. You now have your starting coordinate in the tile array to begin from. Now you draw 20 columns and 15 rows from that start position to fill the screen. When the player does an action that should move the character, you simply increment or decrement world_x and world_y and then recalculate the start position to draw from your tile array.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
Codemunkie:
thanx! i think im starting to get it... but can i add you on msn or do you use IRC? wich server and chan in that case... its easier to chat =) that would be cool
I'll post a little psuedo code sample that may make it clearer. Obviously, you will have to adapt it to what you are doing:

int world_x;//player's x position in the worldint world_y;//player's y position in the wolrdint tiles[1000][1000];//our array of tiles that makes up the world//...insert code that updates world_x and world_y based on player input.void draw(){     //assume player character is at center of screen     //find start position to draw from in tile array     int start_x = world_x-10;//assume tiles are 32x32 and screen width is 640.  That means 20 columns, and we want half     int start_y = world_y-7;//assume tiles are 32x32 and screen height is 480.  That means 15 rows and again we want half     for(int y = start_y; y < start_y+15; ++y)//again, assumes 15 rows     {          for(int x = start_x; x < start_x+20; ++x)//assumes 20 columns          {               draw_tile(tiles[x][y]);          }     }}


Ok, that's the general idea. Obviously, there are a lot of assumptions there about the size of the tiles and the screen resolution. Also, there is no checking if the player is too close to the edge of the map (danger of going out of bounds of the array). Also, this will not do smooth scrolling, but rather old school Final Fantasy 1 style scrolling. Smooth scrolling is not much harder once you get the basic concept. I hope that is fairly clear.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
thanx... i think i built up my tile thingie all wrong to get that to work... could you place the pseudo code in my code? if you have to time... cous i think i did it all wrong

#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
SDL_Surface *screen;
int game_state;
int i;
int x;
int y;
int bytecount;

int makemap()
{


fprintf(stderr, "MakeMapfunction");
FILE *count;
long size;

count = fopen ("maps/desert.map","rb");
if (count==NULL) perror ("Error opening file");
else
{
fseek (count, 0, SEEK_END);
size=ftell (count);
fclose (count);
printf ("Size of myfile.txt: %ld bytes.\n",size);
bytecount = size;
printf("\n\n%i\n\n", bytecount);
}

FILE *file;
file=fopen("maps/desert.map","r");
if ( !file )
{
printf ( "Unable to open file!" );
exit (0);
}
while(i<=bytecount)
{

fseek(file,i,SEEK_SET);
int c = fgetc(file);


if(c=='W')
{
backbuffer("gfx/water.bmp", screen, x, y);
printf("W");
}
if(c=='D')
{
ShowBMP("gfx/desert.bmp", screen, x, y);
printf("D");
}
if(c=='G')
{
ShowBMP("gfx/grass.bmp", screen, x, y);
printf("G");
}
if(c=='\n')
{
y = y + 32;
x = 0;
}
else
{

x = x + 32;
}
i = i + 1;
}
SDL_Delay(3000);
game_state = 4;
fclose(file);
}

Yes, you may need to do some restructuring. It seems like you are on the right track. What you are basically doing is drawing the map as you load it. That is not very useful however, because as you have found it is static. What you should do, or rather what is normally done, is to load in a file that describes how to draw the map and in your main game loop, you continuously update and draw it. It looks like you must have a file like that, and I will assume it looks something like this:

WWWWWWWW
WWDDWWWW
WDDDDDWW
WDDGGGDW
WWDDDDWW
WWWWWWWW

Where W is water, D is desert, and G is grass. In that case, we have defined above a desert island (use your imagination ;)). It may be more useful to instead use integers instead of characters. Although it accomplishes the same thing, this will allow you to do a little trick. If you instead define your map as follows:

00000000
00110000
01111100
01122110
00111100
00000000

You can say that 0 means a water tile, 1 is a sand tile and 2 is a grass tile. In code, define an array to load it into:

int map[8][6];

Now we can open the file and insert each value from the file into the array. You will also need another array that stores your tiles:

SDL_surface tiles[3];

Load the images in and use this array as a lookup table to know which tile to display during the draw routine. Using integers instead of characters will allow you to easily look into the tile array from your map array.
For example, map[2][1] will return the value 1, which you can then use to look into the tile array and grab the desert tile in your draw routine.

[Edited by - CodeMunkie on August 31, 2004 3:04:17 PM]
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.

This topic is closed to new replies.

Advertisement