Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tilemap question  (Read 1496 times)
0 Members and 1 Guest are viewing this topic.
Offline Subscript

Senior Newbie





« Posted 2012-04-14 05:12:03 »

Alright, so instead of hard coding my map into my code. I want to load it from a text file.

Wasn't too sure how to go about this, haven't done much file i/o. Thanks if you help.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
package com.akrillix;

import java.awt.Graphics;
import com.akrillix.*;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;

public class Map {
   private final int TILE_SIZE = 32;
   private Image grass, dirt, water;
   public static boolean drawGrid = false;
   public static int[][] map = {
         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0 },
         { 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 0 },
         { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 0 },
         { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };

   public Map() {
      grass = new ImageIcon("src/grass.png").getImage();
      dirt = new ImageIcon("src/dirt.png").getImage();
      water = new ImageIcon("src/water.png").getImage();
   }

   public void paint(Graphics g) {
      for (int across = 0; across < 25; across++) {
         for (int down = 0; down < 18; down++) {
            if (map[across][down] == 1) {
               g.drawImage(dirt, across * TILE_SIZE, down * TILE_SIZE,
                     null);
            } else if (map[across][down] == 0)
               g.drawImage(grass, across * TILE_SIZE, down * TILE_SIZE,
                     null);
            if (map[across][down] == 2) {
               g.drawImage(water, across * TILE_SIZE, down * TILE_SIZE,
                     null);
            }
            if (drawGrid) {
               g.drawRect(across * TILE_SIZE, down * TILE_SIZE, TILE_SIZE,
                     TILE_SIZE);
            }
         }
      }
   }
}
Offline jammas615

Junior Devvie


Projects: 1



« Reply #1 - Posted 2012-04-14 05:55:14 »

I was playing around with different map formats and the way data was loaded a few days ago. Here is basically how was doing it:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void loadMap(String mapName) {
      try {
         FileInputStream in = new FileInputStream(new File(mapName+".txt"));
         Scanner s = new Scanner(in);
         
         for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
               Tile newTile = new Tile(i, j);
               newTile.setTex(s.nextInt());
               newTile.setHeight(s.nextInt());
               tileData[i][j] = newTile;
            }  
         }
      } catch(IOException e) {
         e.printStackTrace();
      }
   }

In my map file i had the tile data set out one row per tile. Like this:
1  
2  
3  
4  
5  
1 0
1 0
1 0
1 1
etc...

The first number was the index of texture and the second for the height of the tile.

Offline davedes
« Reply #2 - Posted 2012-04-14 05:58:16 »

Basically you need to think about what format you would like to use an how you'd like to edit it. There are a lot of factors involved, but for a simple tile based game here are four common solutions.

Text File
This is the easiest to program, but designing the maps will be difficult since you'll need to type each tile in manually.

You simply output the two-dimensional array in a line-delimited text file. Reading it back is just as simple.
1  
2  
3  
4  
0022
0022
0012
0012


EDIT: Some code to get you started:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   final static int MAPSIZE = 4;
   
   public static int[][] read(InputStream in) throws IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String line = "";
      int[][] map = new int[MAPSIZE][MAPSIZE];
      for (int lineCount=0; (line=br.readLine())!=null; lineCount++) {
         for (int i=0; i<MAPSIZE && i<line.length(); i++) {
            map[lineCount][i] = Character.getNumericValue(line.charAt(i));
         }
      }
      return map;
   }


Result in game:


You can define the format however you'd like, as you can see jammas615 parses his text file a little differently.

Image File
This is the same principle as above, but instead of using characters in a text file, you're using pixels in an image file. Loop through the pixels, and simply pick the tile type that corresponds to the pixel color (e.g. in this case green (135,184,78) or "#87b84e" corresponds to the grass type).

It makes editing levels much more visual -- you can use Photoshop or a similar program as your "level editor."


Use a pre-existing map editor
Tiled will allow you to very easily design complex 2D tile maps. You just need to code yourself an importer; although I'm sure many implementations for Java2D already exist on the web.

Custom Editor & Format
Define your own format, i.e. using XML, JSON, or a combination of whatever you'd like. It can include game-specific properties, even small "scripts" if you want to get ambitious. Then build yourself an editor to design, save and load these files. This is in my opinion the best route if you really want to learn; although it's much more challenging.

EDIT: You should also think about object-oriented design to avoid the ugly string of if-else statements (if your game has 50 different tiles, imagine how that will look in code...).

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (79 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (96 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (152 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!