Added sprites to tilemap

master
bob 2022-06-03 13:17:03 -04:00
parent c4e15de2aa
commit 7a211f67a1
3 changed files with 8 additions and 11 deletions

View File

@ -64,9 +64,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
}
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', spriteArray); //create a player controlled player, set start location to middle of screenk
// the height of 35 is set because it is half of the original tile height (i.e., 70px)
map.add(new SingleTile(1000, 500, box, 35));
map.add(new SingleTile(700, 400, boxCoin, 35));
map.add(new SingleTile(1000, 300, boxCoin, 35));
map.add(new SingleTile(1000, 500, box));
map.add(new SingleTile(700, 400, boxCoin));
map.add(new SingleTile(1000, 300, boxCoin));
map.add(new Tile(700, 200));
this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input
@ -142,7 +142,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public void run(){
try {
MapReader.inputMap(map, "src/Level1.txt");
} catch (IOException e) {
} catch (IOException | SpriteException 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.
@ -187,7 +187,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
}
public BufferedImage getImage(String imageLocation) throws IOException {
public static BufferedImage getImage(String imageLocation) throws IOException {
return ImageIO.read(new File(imageLocation));
}

View File

@ -4,7 +4,7 @@ import java.util.Scanner;
public class MapReader {
//Input game map
public static void inputMap(ArrayList<Tile> map, String filePath) throws IOException {
public static void inputMap(ArrayList<Tile> map, String filePath) throws IOException, SpriteException {
String file = FileManager.readFile(filePath);
int x = 0;
int y = 0;
@ -14,7 +14,7 @@ public class MapReader {
x=0;
}
else if(file.charAt(i)=='1'){
map.add(new Tile(x,y));
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grass.png")));
}
x+=Tile.length;
}

View File

@ -2,17 +2,14 @@ import java.awt.*;
import java.awt.image.BufferedImage;
public class SingleTile extends Tile {
public int length;
public BufferedImage tileImage;
public SingleTile(int x, int y, BufferedImage tileImage, int length) throws SpriteException {
public SingleTile(int x, int y, BufferedImage tileImage) throws SpriteException {
super(x, y);
if (tileImage.getWidth() != tileImage.getHeight()) {
throw new SpriteException();
}
this.length = length;
this.tileImage = tileImage;
}
public void draw(Graphics g){