From 99619a2bc94d40ce8197251a36c7739ac67642e7 Mon Sep 17 00:00:00 2001 From: bob Date: Fri, 3 Jun 2022 12:22:05 -0400 Subject: [PATCH] Tilemap --- src/GamePanel.java | 9 +++++++-- src/Level1 | 5 ----- src/Level1.txt | 15 +++++++++++++++ src/MapReader.java | 36 +++++++++++++++--------------------- 4 files changed, 37 insertions(+), 28 deletions(-) delete mode 100644 src/Level1 create mode 100644 src/Level1.txt diff --git a/src/GamePanel.java b/src/GamePanel.java index 945caad..eae9433 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -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 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. long lastTime = System.nanoTime(); double amountOfTicks = 60; @@ -197,7 +202,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ 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); FileWriter fileWriter = new FileWriter(newFile); fileWriter.write(writeString); @@ -205,7 +210,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ } // 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); if (newFile.createNewFile()) { return null; diff --git a/src/Level1 b/src/Level1 deleted file mode 100644 index b228584..0000000 --- a/src/Level1 +++ /dev/null @@ -1,5 +0,0 @@ -10000000 -01101101 -00000000 -00000000 -00010000 \ No newline at end of file diff --git a/src/Level1.txt b/src/Level1.txt new file mode 100644 index 0000000..57b1ff6 --- /dev/null +++ b/src/Level1.txt @@ -0,0 +1,15 @@ +10000000 +01101101 +00000000 +00000000 +00010000 +00010000 +00010000 +00010000 +00000000 +00000000 +00000000 +00000000 +00000000 +00000000 +11111111 \ No newline at end of file diff --git a/src/MapReader.java b/src/MapReader.java index 599eebf..8ed6a15 100644 --- a/src/MapReader.java +++ b/src/MapReader.java @@ -1,27 +1,21 @@ -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; +import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class MapReader { - public static void inputMap(ArrayList map, File file) throws FileNotFoundException { - int x = 0; - int y = 0; - Scanner sc = new Scanner(new BufferedReader(new FileReader("src/Level1.txt"))); - int rows = 7; - int columns = 7; - while(sc.hasNextLine()) { - for (int i=0; i<7; i++) { - String[] line = sc.nextLine().trim().split(" "); - for (int j=0; j map, String filePath) throws IOException { + String file = GamePanel.readFile(filePath); + int x = 0; + int y = 0; + for(int i=0; i