0

I've created a platform game where each type of game object is assigned to specific rgb values so I can create levels by drawing them out in paint and loading the image. Right now I have the first two levels already loaded and I am able to get the path of the 3rd level through a textfield input and load a custom 3rd level. Each level needs a path to the png image of the level, and the number of coins needed to progress to the next level. I want to have every level load up from one text file where each line maybe has the level number, the image path, and the # of coins. I'm making it to be customizable so that the user can add or change levels simply by adding these parameters through 3 textfields in my customize menu. This way also my designer can help create levels and by reading from the text file I imagine there will be a lot less code in the long run when there are 20+ levels. Any ideas on how I can load from and append to this file? Here's what I'm working with right now:

public static BufferedImageLoader loader = new BufferedImageLoader();
public Handler(Camera cam){
this.cam = cam;

level1 = loader.loadImage("/level1.png");
level2 = loader.loadImage("/level2.png");
}

public void changeLevel(){
clearLevel();
cam.setX(0);
Player.coinCount = 0;
if(Game.LEVEL == 1){
  Player.maxCoins = 4;
  LoadImageLevel(level1);
}
if(Game.LEVEL == 2){
  LoadImageLevel(level2);
  Player.maxCoins = 11;
}
if(Game.LEVEL == 3){
  System.out.println(Data.levelPath);
  try{
    level3 = loader.loadImage(Data.levelPath);
    LoadImageLevel(level3);
  } catch (Exception e) {
    e.printStackTrace();
    System.out.println("error loading custom level");
  }
}
}

public Menu(Game game, Handler handler){
this.handler = handler;

pathField = new JTextField(10);
levelField = new JTextField(10);
coinField = new JTextField(10);

if(Game.gameState == STATE.Menu){
  int selection = JOptionPane.showConfirmDialog(
      null, getPanel(), "Input Form : "
      , JOptionPane.OK_CANCEL_OPTION
      , JOptionPane.PLAIN_MESSAGE);
  if(selection == JOptionPane.OK_OPTION) {
    Data.levelPath = pathField.getText();
    Data.level = levelField.getText();
    Data.coinAmount = Double.valueOf(coinField.getText());
    System.out.println(Data.levelPath + Data.level + Data.coinAmount);
}
}

private JPanel getPanel(){
  JPanel basePanel = new JPanel();
  basePanel.setOpaque(true);

  JPanel centerPanel = new JPanel();
  centerPanel.setLayout(new GridLayout(3, 2, 5, 5));
  centerPanel.setBorder(
      BorderFactory.createEmptyBorder(5, 5, 5, 5));
  centerPanel.setOpaque(true);

  JLabel mLabel1 = new JLabel("Enter path:  (e.g., /level1.png) ");
  JLabel mLabel2 = new JLabel("Enter which level to load the image in: ");
  JLabel mLabel3 = new JLabel("Enter the amount of coins you must collect");

  centerPanel.add(mLabel1);
  centerPanel.add(pathField);
  centerPanel.add(mLabel2);
  centerPanel.add(levelField);
  centerPanel.add(mLabel3);
  centerPanel.add(coinField);

  basePanel.add(centerPanel);

  return basePanel;
}

Any ideas or suggestions are appreciated!

  • Maybe I can write all image paths to a textfile, read from the textfile and put them into an array and do a for loop that says if level == i, LoadImageLevel(level[i]) and do the same for coins in its own file and array? – David McDonald Dec 08 '16 at 08:22

1 Answers1

0

It's actually just....

Given a value of the input.getText() as 1#path#20, It have level number, the image path, and the number of coins separated by # token.

 public static String [] separateFields(String input, String separator){

    String[] separatedValues = input.split("#");

    return separatedValues;
    }

Call the function, define the arguments.

// input is textfield.getText() , and the value of input is 1#path#20
 String [] result= separateFields(input, "#");

Then you get

    int level = Integer.parseInt(result[0]); // level
    String aPath = result[1]; // path
    int numCoins = Integer.parseInt(result[2]); // number of coins
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
  • Hmm okay this allows me to load up one level with one text field but I still need to figure out how to load up multiple levels and save them in a text file so they are still there on the next start up – David McDonald Dec 08 '16 at 13:57