This site requires JavaScript, please enable it in your browser!
Greenfoot back
Alan_c
Alan_c wrote ...

2016/1/19

multiple level creation from different text files

Alan_c Alan_c

2016/1/19

#
I have been using a few youtube tutorials to make a maze game. I would like to use textfiles (.txt) to have a easier level creation but having problems on how i would change the current text file selected. I provided the link to the video to get a better understanding, i would try going to the creators website but it's not longer around.
Alan_c Alan_c

2016/1/19

#
This is my code that i have at the moment
public class MyWorld extends World
{
// varables
private final int WORLD_WIDTH = getWidth(); // Width of the world 
private final int WORLD_HEIGHT = getHeight(); // Height of the world

Wall wallTemplate = new Wall(); 
Player PlayerTemplate = new Player(); 
//wall
GreenfootImage wtImg = wallTemplate.getImage(); // get the image of the wall
private final int WALL_HEIGHT = wtImg.getHeight(); //finds the height of the wall
private final int WALL_WIDTH = wtImg.getWidth(); //finds the width of the wall
//player
GreenfootImage plImg = PlayerTemplate.getImage(); // get the image of the wall
private final int PLAYER_HEIGHT = plImg.getHeight(); //finds the height of the Player
private final int PLAYER_WIDTH = plImg.getWidth(); //finds the width of the player

private final int MAP_WIDTH = (int)(WORLD_WIDTH/WALL_WIDTH); // changes the MAP_WIDTH depending on wall width
private final int MAP_HEIGHT = (int) (WORLD_HEIGHT/WALL_HEIGHT); // changes the MAP_HEIGHT depending on wall height

private final String FILENAME = "Map1.txt";

private final String WALL_MARKER = "*";
private final String Player_Spawn = "P";
String[][]mapArray = new String [MAP_HEIGHT][MAP_WIDTH];

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld() throws IOException 
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600,400,1); 
        mapArray = readMapFromTextFile(MAP_WIDTH, MAP_HEIGHT, FILENAME);
        drawWallMapFromTextArray(WALL_WIDTH,WALL_HEIGHT,WALL_MARKER,PLAYER_WIDTH, PLAYER_HEIGHT, Player_Spawn, mapArray);
    }
    public void drawWallMapFromTextArray(int WALL_WIDTH, int WALL_HEIGHT, String WALL_MARKER, int playerHeight, int playerWidth, String playerMarker,String [][] mapArray)
    {
      int x = 0;
      int y = 0;
       for (y=0;y<mapArray.length;y++)
       {
           for (x=0;x<mapArray[y].length;x++)
           {
               if(mapArray[y][x].equals(WALL_MARKER))
               {
                  int wallX = x*WALL_WIDTH + WALL_WIDTH/2;
                  int wallY = y*WALL_HEIGHT + WALL_HEIGHT/2;
                  addObject (new Wall(), wallX, wallY);
               }
               if (mapArray[y][x].equals(playerMarker))
                {
                    int playerX = playerWidth + playerWidth;
                    int playerY = playerHeight + playerHeight;
                    addObject(new Player(), playerX, playerY);
                }
           }
       }
    }   
    
    public String [][] readMapFromTextFile(int MAP_WIDTH, int MAP_HEIGHT, String fileName) throws IOException
    {
            BufferedReader br = null;
            String[][] mArray = new String[MAP_HEIGHT][MAP_WIDTH];
            try{
              br = new  BufferedReader(new FileReader(fileName));
              String l;
              int mapRow= 0; //tells us what row we're on
              while((l=br.readLine())!=null)
              {
               mArray[mapRow] = l.split("");
               mapRow++;
              }
            } finally {
             if (br!=null)
             br.close();
            }
            return mArray;
    }
}
danpost danpost

2016/1/19

#
By making the following changes, you will be able to create worlds using different filenames:
// add this
public MyWorld()
{
    this(FILENAME};
}

// change line 31 to this
public MyWorld(String filename)

// change 'FILENAME' in line 35 to 'filename'
I do not think you need to specify that the exceptions are thrown in your method declarations. I do think, however, that you need a 'catch' block following the 'try' block and before the 'finally' block.
davmac davmac

2016/1/19

#
I do think, however, that you need a 'catch' block following the 'try' block and before the 'finally' block.
I'm not sure I agree. The try/finally construct looks legitimate here (the finally clause ensures that the BufferedReader will be closed if an exception occurs within the try block). The IOException may as well propagate out of the method, since I don't think it can be meaningfully handled within the method.
danpost danpost

2016/1/20

#
davmac wrote...
The try/finally construct looks legitimate here (the finally clause ensures that the BufferedReader will be closed if an exception occurs within the try block). The IOException may as well propagate out of the method, since I don't think it can be meaningfully handled within the method.
I was not absolutely sure. It was just that I have never seen a try block without a catch block. I just looked back at the java tutorials and it does not seem to say whether the catch block was mandatory or not -- at least, not directly; but,, maybe it does, in a roundabout way. :</
davmac davmac

2016/1/20

#
It's a bit unusual to leave it out, but the 'catch' block is not mandatory if there is a 'finally' block.
danpost danpost

2016/1/20

#
davmac wrote...
It's a bit unusual to leave it out, but the 'catch' block is not mandatory if there is a 'finally' block.
That actually does make sense. Thanks.
You need to login to post a reply.