master
bob 2022-06-03 12:22:05 -04:00
parent a24e07f496
commit 99619a2bc9
4 changed files with 37 additions and 28 deletions

View File

@ -140,6 +140,11 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen //run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){ public void run(){
try {
MapReader.inputMap(map, "src/Level1.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen. //the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
long lastTime = System.nanoTime(); long lastTime = System.nanoTime();
double amountOfTicks = 60; double amountOfTicks = 60;
@ -197,7 +202,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
return flippedImage; return flippedImage;
} }
public void writeFile(String fileLocation, String writeString) throws IOException { public static void writeFile(String fileLocation, String writeString) throws IOException {
File newFile = new File(fileLocation); File newFile = new File(fileLocation);
FileWriter fileWriter = new FileWriter(newFile); FileWriter fileWriter = new FileWriter(newFile);
fileWriter.write(writeString); fileWriter.write(writeString);
@ -205,7 +210,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
} }
// will create file if it doesn't exist // will create file if it doesn't exist
public String readFile(String fileLocation) throws IOException { public static String readFile(String fileLocation) throws IOException {
File newFile = new File(fileLocation); File newFile = new File(fileLocation);
if (newFile.createNewFile()) { if (newFile.createNewFile()) {
return null; return null;

View File

@ -1,5 +0,0 @@
10000000
01101101
00000000
00000000
00010000

15
src/Level1.txt Normal file
View File

@ -0,0 +1,15 @@
10000000
01101101
00000000
00000000
00010000
00010000
00010000
00010000
00000000
00000000
00000000
00000000
00000000
00000000
11111111

View File

@ -1,27 +1,21 @@
import java.io.BufferedReader; import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
public class MapReader { public class MapReader {
public static void inputMap(ArrayList<Tile> map, File file) throws FileNotFoundException { public static void inputMap(ArrayList<Tile> map, String filePath) throws IOException {
String file = GamePanel.readFile(filePath);
int x = 0; int x = 0;
int y = 0; int y = 0;
Scanner sc = new Scanner(new BufferedReader(new FileReader("src/Level1.txt"))); for(int i=0; i<file.length(); i++){
int rows = 7; if(file.charAt(i)=='\n'){
int columns = 7; y+=Tile.length;
while(sc.hasNextLine()) { x=0;
for (int i=0; i<7; i++) { }
String[] line = sc.nextLine().trim().split(" "); else if(file.charAt(i)=='1'){
for (int j=0; j<line.length; j++) {
map.add(new Tile(x,y)); map.add(new Tile(x,y));
}
x+=Tile.length; x+=Tile.length;
} }
x=0;
y+=Tile.length;
}
}
} }
} }