Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Tile maps?

Author
Message
Camronas
14
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 2nd Oct 2010 08:02
I heard there is away to go into a txt document with a for loop and make maps with it. How would I do that?
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 2nd Oct 2010 08:52 Edited at: 2nd Oct 2010 08:54
Yes, you basically make a grid of numbers in the .txt file that represent the tile numbers used in your map. But because it's a .txt file (allowing you to edit it with Notepad) you have to load each line as a string and extract the numbers to put into the 2D map array. To do it properly it's not a single FOR/NEXT loop but two of them that cover both the horizontal and vertical of the entire map (the map x and y coordinates).

The following code snip will create a 2D map array, create 10 tiles, load the file that's attached to this message, and show the tile map. I used a common function to extract text in case you don't have IanMs Matrix 1 Utilities Plugin which you should have if you're serious about programming in Darkbasic Pro.

Here's the link: http://forum.thegamecreators.com/?m=forum_view&t=85209&b=18

Here's the code:


If you have anymore questions about tile maps or about my code specifically just post in this thread.

Attachments

Login to view attachments
Camronas
14
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 2nd Oct 2010 09:40
Looks nice but Im sorry its abit confusing. Is it possible to write a more n00b friendly version or maybe some more comments, sorry
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 2nd Oct 2010 21:33 Edited at: 2nd Oct 2010 22:05
That's the first time somebody has ever asked me to make my code simpler. It's probably because some of the concepts you've never used. Ok, I'll try.


This part is a 2D array that is used to store the data loaded from the file. We call it a 2D array because it's makes a grid of data like graph paper (small boxes that hold data) that is perfect for 2d maps like a tile map. The array size is 19x14 and since the array also counts zero as one of the elements the actual size of the array is 20x15. So if you have a 5x5 grid like this Map(4,4):


The 1 in the above code snip is at Map(0,0) the 7 is at Map(3,3), the 2 is at Map(1,4), and the 5 is at Map(0,2).



This part does just what the comment says it makes random number picking more random. Without this it's possible for the program to pick exactly the same random numbers every time the program is ran... which in games isn't very fun if the randomly generated bad guys are always in exactly the same spots. Any time your going to make Darkbasic Pro use the RND() command it's always best to have RANDOMIZE TIMER() only once before the first RND() command (never put RANDOMIZE TIMER() within a loop. In that code the only random number picking is for different colors and I put it in there because it's a habit to always use even if I use RND() only once.



In this part it makes a FOR/NEXT loop using t as the variable that counts from 1 to 10. The NEXT T tells Darkbasic that this is the end of the FOR/NEXT loop. So the first time it loops it t=1, the second loop t=2, the next loop t=3.



This may be confusing because it uses 3 different commands. The INK command changes the ink color (both foreground color and background color). INK changes both text color and 2D graphic colors (like BOX). The RGB() command breaks down the red, green, and blue content of the color we want. The numbers are between zero and 255. So if you have RGB(255,0,0) that color is bright red, RGB(0,255,0) is bright green, and RGB(0,0,255) is bright blue. Colors like grey use all three like RGB(150,150,150). The RND() command is what picks a random number. In this case it picks a random number for each red, green, and blue content in the RGB() command for the INK command but only changes the foreground color.



This makes a filled in box that starts at the screen coordinates 0,0 and ends at 32,32. The syntax is BOX Left,Top,Right,Bottom or BOX x1,y1,x2,y2. We usually use x as a variable to represent the horizontal coordinate and y to represent the vertical coordinate. So that command makes a box 32x32 pixels in size on the screen. If you look close though Darkbasic will make the box end at 31x31 but because of zero it is still 32x32. If you used BOX 100,200,150,250 it will make a box starting in the upper left corner at x coordinate 100 and y coordinate 200 and the lower right at x coordinate 149 and y coordinate 249 making a box that's 50x50.



This part writes centered text on the screen at x coordinate 16 and y coordinate 7. That command only accepts strings and since I wanted it to write the variable t it needs the STR$() command to convert the number stored in t to a string.



This command grabs a box area around the coordinates 0,0 and 32,32. Whatever is in that area is considered image number t. Since t is in a FOR/NEXT loop (shown earlier) the first time it loops it grabs image number 1, then image number 2, and so on till image number 10. The whole FOR/NEXT loop is designed to create 10 tiles to show you tiles without having to use external media (other than the Map.txt file).



This opens a file to read data from the file. The 1 is the file number you want to use. It's designed that way so you can open multiple files and read/write to them any way you see fit because after a file is opened you only need to remember the file number to access the file. The "Map.txt" is of course the file you want to open. The way this code is written that file needs to be in the same directory as your code otherwise it won't work.



This starts a FOR/NEXT loop making y equal the numbers 0 to 14 which will ultimately represent the vertical coordinate in the map array.



This reads a string from the file that was opened earlier (the 1 being the Map.txt file). It makes a$ equal the line read from the file. With text files you can only read them properly as strings even though what's in the text file are numbers and letters they are always considered strings. Because of the earlier FOR/NEXT loop "FOR y=0 to 14" the above code snip will be done 15 times to load the entire map one line at a time.



This just makes the variable Char equal 1. This is done because Char is changed later in the code so this makes sure it starts at 1 the next time Char is used. It will later represent the current character looked at in a$.



This starts a FOR/NEXT loop that makes x equal the numbers 0 to 19 which will represent the horizontal coordinate in the map array.



This makes Map(x,y) equal the value of the string extracted from the mids function. Remember the x and y are represented by the FOR/NEXT loops above. The FOR/NEXT loop for x is within the FOR/NEXT loop for y so when this is done the first time it says y=0 and x=0 to 19, then y=1 and x=0 to 19, then y=2 and x=0 to 19, and so on. The VAL() command converts a string to a number (the opposite of the STR$() command). The mids() function is called to extract text from a$. I used this because the natural MID$() command that's in Darkbasic only extracts 1 character from a string at a time. The mids() function makes a normal basic MID$() command that allows multiple characters to be extracted from strings... in this case it takes out 2 characters from a$ starting at Char. When it starts Char=1 so it takes out the first two characters from a$, converts it to a number, and puts that number into the Map array at x,y.



This part adds 3 to Char. It does this because in the Map.txt file the numbers are 2 digits separated by a space. So when this is seen it sets Char up to extract the next two digits from a$ (when the code snip above this runs again).



The NEXT X and NEXT Y show the ends of the FOR/NEXT loops. Once those loops are done the CLOSE FILE 1 closes the file you opened earlier. It's always best to close a file after you're done with it. Leaving files open isn't a problem but closing it makes sure if you later open that same file number there aren't any errors. And it's just the proper way to code... a programmers version of the phrase "Remember to dot all your I's and cross all your T's".



This part starts another set of FOR/NEXT loops to go through the Map arrays x and y coordinates.



This is an IF/THEN statement that checks to see if the number stored in Map(x,y) is more than zero. This is done because there are some spots in the map where there are no tiles (all zeros) and the code that's within the IF/THEN shouldn't be seen unless there's an actual tile number... mainly because there is no such thing as an image number zero. Without this it'll error out the minute it tries the next code snip if Map(x,y)=0.



This places the images on the screen using the numbers stored in the map array to determine which tile to place. Remember the Map array has stored each number in the Map.txt file so when Map(x,y)=1 it'll place image number 1 on the screen at the ScreenX and ScreenY coordinates. If this is the first time Darkbasic has seen the variables ScreenX and ScreenY they will both start at zero (which is just perfect so there's no need to say earlier in code ScreenX=0 and ScreenY=0).



This increases ScreenX by 32 and closes the FOR/NEXT loop of x. Because this INC is also within the FOR/NEXT loop for x every time x loops this keeps adding 32 to ScreenX each loop. So every tile that is placed with PASTE IMAGE is placed next to each other on the screen.



This part is outside of the FOR/NEXT loop of x but inside the FOR/NEXT loop for y. This increases ScreenY by 32 and resets ScreenX to zero. So when the program completes the FOR/NEXT loop for x it sets up the variables for the next time the FOR/NEXT loop for x runs. Because the FOR/NEXT loop for y has the FOR/NEXT loop for x within it creates the map from top to bottom left to right. The first loop y=0 and StartY=0 then it does x from 0 to 19 placing all the tiles across the screen, then y=1 and StartY=32 and again does x from 0 to 19 placing the next line of tiles across the screen, then y=2 and StartY=64 and places the next line of tiles across the screen, and so on till all the tiles are placed to see the map stored in the Map.txt file.



This part waits for the user to press any key on the keyboard to continue. The END tells Darkbasic you're done with the program and to close it. There is more code later but it's a function that shouldn't be actually ran. Always END before the first function otherwise your program will end in an error.



This part is a function. A function is like a custom command you make to do whatever you want. It can handle as many as 255 variables but in this case it only takes in a string and two numbers. Within this function t$ a and b are whatever is sent to it but outside of the function t$ a and b are zero. Arrays like the Map can be seen inside a function because arrays are always global by default. The function ends with ENDFUNCTION A$ because I want that function to return a string. In this case it's always going to be 2 characters extracted from t$ (which is a$) in the main program.


This is several commands that used together act just like a normal Basic Mid$() command. Because Darkbasic Pros MID$() command only extracts one character at a time and not as many as we want I made that function to make life easier. IanMs Matrix 1 Utilities Plugin that I mentioned earlier fixes this obvious flaw in Darkbasic but I figured you didn't have it. The LEFT$() command extracts text starting from the left side of the string. The RIGHT$() command extracts text starting from the right side of the string. The LEN() command returns the number of characters in a string. If your string was this:


There are 22 characters in that string (the LEN() of that string would equal 22). And if you wanted to extract "this" you would call the mids() function with mids("Hello, this is a test.",8,4). It will start at the 8th character in the string and take out 4 characters from there. The actual command that does that does it this way (without the variables):


Breaking it down if you just use RIGHT$() it would return "this is a test.". Again RIGHT$() counts from the right side of the string and since 22-8+1=16. It counts starting from the right side 16 characters (at the end there's a return character that's never shown). The LEFT$() command does the same but from the left side and it only extracts in this case 4 characters. So if RIGHT$() extracts "this is a test." and LEFT$() only wants the first 4 characters then ultimately a$ will only be "this".

Man, this is a long message. I hope this helps but if you really want to learn the basics don't learn it all from me. TDKs tutorials will better explain the basics of Darkbasic. Again if you have anymore questions just post.

Here's TDKs tutorials:
http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

Login to post a reply

Server time is: 2024-09-28 22:31:06
Your offset time is: 2024-09-28 22:31:06