I'm making my first game in java, now I managed to paint the levels and move the character using the keyboard. At the moment my character moves in any direction (not diagonal) but you can wander around the map, and therefore my next step is to move the logic that has the game. I will present my idea for it and wanted to give me opinions ...
My levels are on file .Txt and I thought that according to the current level progress along the file at that level and be copied to an array. Later I have to check if the character can move in different positions, for it traverses the array with a for loop to find the character and then calculate the next position and according to the number you have saved the file to go because I can set the character motion. I enter the code below:
Code:
public void copyboard () {// create array from the current level
File file = null;
FileReader fr = null;
BufferedReader br = null;
try {
file = new File (getClass (). getResource ("/ levels /" + / * getLevel () + * / "1.txt"). toString ().substring (6));
fr = new FileReader (file);
br = new BufferedReader (fr)
String line;
while ((line = br.readLine ())! = null) {
char [] character = linea.toCharArray ();
// OFF = 0, WHITE = 1, SKIP = 2,
// WALL = 3, POSFINAL = 4, BLOCK = 5, CHARACTER = 6 = 7 PLACED
for (int i = 0; i <line.length (); i + +) {
for (int j = 0, j <array.length j + +) {
if (character [i] == '') {
array [i] [j] = WHITE;
} Else if (character [i] == '#') {
array [i] [j] = WALL;
} Else if (character [i] == '2 ') {
array [i] [j] = JUMP;
} Else if (character [i] == '$') {
array [i] [j] = BLOCK;
} Else if (character [i] == '@') {
array [i] [j] = CHARACTER;
} Else if (character [i] == '.') {
array [i] [j] = FINALPOS;
} Else if (character [i] == '0 ') {
array [i] [j] = NO;
} Else if (character [i] == '*') {
array [i] [j] = REGISTERED;
Else {}
System.out.println ("WTF character that does not exist");
}
}
}
}
} Catch (Exception e) {
System.out.println ("Error reading file");
e.printStackTrace ();
} Finally {
try {
if (null! = fr) {
fr.close ();
}
} Catch (Exception e2) {
e2.printStackTrace ();
}
}
}
The truth is that my code does not work (if compiled), because the press "up" for not only not moving but no longer incorporates keyboard. Please help me..
Bookmarks