Tanks game

Hello everyone,

I’m fairly new to java programming ( about 1 month ) and I have chosen to make a sort of “Battle city style” game ( the one in gameboy :slight_smile: ). I am supposed to use a server, as it will be a multi-player using tcp/ip. I think I got the socket communication working for registering and logging a player in a server and I was thinking about starting to build 2D maps for the tanks to move. How exactly am I supposed to do that?

Thanks in advance,

basically a tiled map is an array [][]
like
0000000000000000000000000000000
0000111121121111223322111100000
0000111121121111223322111100000
0000111121121111223322111100000
0000111121121111223322111100000
0000111121121111223322111100000
0000000000000000000000000000000

where 0 would be the water, 1 is the grass, 2 is dirt etc… where you assign an Image to be 1 and 2 etc… and so you can either make your own map like that or use a map editor, such as Mappy, or TileD.

I have made a map in TileD. Now, how can I generate the code for that map in order to add that map to my game and, after that, put a tank moving there?

You may use my Java/Swing-based 2D tilemap editor :slight_smile:

which program do I use to load the maps TileD generates into java code?

I usually write my own editor or use a text file, both of which are pretty simple. Typically the text file approach works if you’ve got a game like Mario where each block is exactly the same from another block of the same type. An RPG where different characters have different messages they say and whatnot is not suitable for a text-based map.

A simple text file map for Mario might look like this, where s is sky, c is cloud, q is question block, g is ground block, k is koopa (enemy), and b is bush.


sssscssscssccsssscssscsssc
scssscssssccssssscssscssss
sscssscsqscsccsqscscssssss
sggbssbbskssbsksbssgggsbss
gggggggggggggggggggggggggg
gggggggggggggggggggggggggg

Then you simply read in the text file, and go through each character and create a new object of the correct type to match the letter that was put in.

The next level in complexity from a text file is an XML file, which is often used all over the place, even for many professional games. XML is basically just a way of structuring data, and Java has built in XML parsers to make it easy to turn that data into useable information for your map. Using this method you can get increasingly complicated and do things like have multiple things per tile (obviously only if your game supports it, though).

XML might be like this:


<tile xPos="0" yPos = "0">
     <entity name="Brick" type="wall">
          <image url="images/brick.gif" />
          <vector type="dimension" x="50" y="50" />
          <vector type="position" x="0" y="0" />
     </entity>
</tile>
<tile xPos="1" yPos = "0">
     <entity name="Brick" type="wall">
          <image url="images/brick.gif" />
          <vector type="dimension" x="50" y="50" />
          <vector type="position" x="50" y="0" />
     </entity>
     <entity name="Sword" type="item">
          <image url="images/sword.gif" />
          <vector type="dimension" x="50" y="50" />
          <vector type="position" x="50" y="0" />
     </entity>
</tile>
<!-- Etc. etc. -->

Obviously the XML file will take a long time to write by hand and can lead to a lot of possible errors. One advantage to it though is that it can represent literally anything, is very clear to read externally, and can be edited by your players if they desire. XML is also… erm… extensible :wink: and so can used anywhere for anything.

The final choice is to make your own editor, as I said previously, complete with a GUI, buttons, all that jazz. Obviously you can also use one like Bad Sector provided, but I would recommend rolling your own at first just to learn something. In many games early in my game programming career, before I made my own tile map editor, I used text files. Like that Mario example I actually did create several years ago. But then it turned out that it was too difficult to visualize my levels in text, so I made an editor. The editor originally just made text files. A small toolbar at the bottom drew out each type tile and had a letter overlayed the top it, then I’d press that letter to make that tile active. Then when I clicked and drew it would put that tile down wherever the mouse was. When I clicked save it would just become a text file again.

Over a multitude of games and experiments, my need grew so I slowly added onto my editor until it became capable of a staggering amount. The key to making this possible is ObjectInputStream and ObjectOutputStream, which I highly recommend you checking out. Basically, your editor creates the actual objects you will use, actually saves them in an array as you desire, and then writes that data to a file. You then read it later and put the array directly into memory. Easy and can do absolutely anything.

So anyway, my final suggestion is that you start with a text file and then work your way up like I did. You’ll learn a lot in the process.

Oh, here’s an image of what my Mario level editor looked like:

So, what do I use then to make my game run the map and have a tank moving in it? some sort of array?

thanks in advance,

I have most of the code written but I am experiencing a lot of lag… I am using tcp/ip sockets