Results 1 to 5 of 5

Thread: Array Help

  1. #1
    hobbles is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default Array Help

    I have been struggling working on a tile based map for a game. I don't know how to make the array of the map, for now the map is gonna be in the game but I will move it to a .txt file or something later. I have seen a map done something like this (cant find it) but I keep getting errors cause I'm doing something wrong with the array.

    Java Code:
    		
    int[][] map = new int[5][3];
    
    map = [1,1,1,1,1     //kinda looked something like this but im positive this is wrong, but it kinda shows what im talking about.
           1,0,0,0,1
           1,1,1,1,1]
    Also I wanted to put the the code that draws the map in a method outside of paint then call on the method from init, but I'm struggling here to. Hopefully someone can help so I can get past this part that's driving me crazy.

    Java Code:
    void init()
    {
    drawmap()
    }       
    	void drawmap()
    	{
    		g.drawRect(10,10,10,10);
                    repaint();
    	}
    
            public void paint (Graphics g)
    
    	{
            }
    
    	void drawmap(Graphics g)     //also tried this but got an error
    	{
    		g.drawRect(10,10,10,10);
                    repaint();
    	}
    Last edited by hobbles; 08-20-2010 at 07:20 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Hi,

    You can declare and initialize the array variable using the following way:

    Java Code:
    int[][] map = {
                    {1, 1, 1, 1, 1},
                    {1, 0, 0, 0, 1},
                    {1, 1, 1, 1, 1}
            };

  3. #3
    hobbles is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    Does anybody see any problems with this? This is just were I'm starting at trying to get some tiles drawn. I looked up tutorials on 2D tile based games and around the parts with arrays they all get kinda vague, so I'm trying to wing it. Thanks for the help with the array by the way.

    Java Code:
    import java.applet.*;
    
    import java.awt.*;
    
    
    
    public class helloworld extends Applet
    
    {
    
    	int[][] map = new int[15][15];
    
    	int x = 0;		
    
    	int y = 0;	
    
    	int xmap = 0;
    
    	int ymap = 0;
    
    	
    
    	
    
    	public void init()
    
    	{
    
    		int[][] map = {
    
                    {1,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,2,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    
                    {1,3,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,1}
    
            };
    
    		
    
    		
    
    		drawmap();
    
    	}
    
    
    
    
    
    	
    
    	void drawmap()
    
    	{
    
    		
    
    	}
    
    
    
    	public void paint (Graphics g)
    
    	{
    
    		while (y<=14)
    
    		{
    
    				if (map[x][y] == 0) // grass
    
    				{
    
    					g.setColor(Color.green);
    
    				}
    
    				if (map[x][y] == 1) // wall
    
    				{
    
    					g.setColor(Color.black);
    
    				}
    
    				if (map[x][y] == 2) // start
    
    				{
    
    					g.setColor(Color.red);
    
    				}
    
    				if (map[x][y] == 3) // end
    
    				{
    
    					g.setColor(Color.blue);
    
    				}
    
    				if (x > 15)
    
    				{
    
    					x = 0;
    
    					xmap = 0;
    
    					y = y + 1;
    
    					ymap = ymap + 16;
    
    				}
    
    				g.fillRect(xmap,ymap,16,16);
    
    				x = x + 1;
    
    				xmap = xmap + 16;
    
    			}
    
    			g.drawString("Ran",10,290);
    
    			
    
    		
    
    	}
    
    	
    
    
    
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're doing a couple things wrong:
    1) you're re-declaring the init variable inside of the init method. Doing this will have no effect on the init variable that is declared in the class -- meaning the latter will continue to hold default values (0).

    2) You seem to be trying to do graphics programming before you have a good footing in Java. Perhaps you're smarter than me, but from my experience, I found it much easier to learn the rudiments of Java first before attempting to climb the steep slope of graphics and GUI programming. Also, when you do go through the graphics and gui tutorials, I recommend that you use Swing and not the out of date AWT.

    Luck!

  5. #5
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    I'm also working on a map based game and from my experience, everything is 10 times harder than it seems at first glance. In my tower defence game, I save the maps in the following way:

    I make an array of points in a text file, like this:
    2,5
    2,6
    3,6
    etc.

    Then I turn those numbers in an ArrayList of Points. Then I send that ArrayList into my "Game"-class. Then game-class then check's the ArrayList (is every point inside the map etc.). Then the game class makes a map:

    square[][] map=new square[mapSize][mapSize]

    Square is a seperate class that saves everything about one square. Then I initialize every square using 2 for-loops inside each other. Then I simply do this for the entire ArrayList:

    map["ArrayList name".get(0).getX()][["ArrayList name".get(0).getY()].turnIntoRoad();

    turnIntoRoad is in my case a methode of the square class which determines that that square is part of the road of the game, that changes the color of the square etc. But you could just as easily make a turnIntoWall methode or whatever you want to turn that square into.

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 12:32 PM
  2. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 10:01 PM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 12:41 PM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 10:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •