diff --git a/img/tiles/terrain/grassLeft.png b/img/tiles/terrain/grassLeft.png new file mode 100644 index 0000000..c84ac23 Binary files /dev/null and b/img/tiles/terrain/grassLeft.png differ diff --git a/img/tiles/terrain/grassMid.png b/img/tiles/terrain/grassMid.png new file mode 100644 index 0000000..d619da8 Binary files /dev/null and b/img/tiles/terrain/grassMid.png differ diff --git a/img/tiles/terrain/grassRight.png b/img/tiles/terrain/grassRight.png new file mode 100644 index 0000000..d1351da Binary files /dev/null and b/img/tiles/terrain/grassRight.png differ diff --git a/src/Level1.txt b/saves/Level1.txt similarity index 94% rename from src/Level1.txt rename to saves/Level1.txt index 3901362..3879e91 100644 --- a/src/Level1.txt +++ b/saves/Level1.txt @@ -14,4 +14,4 @@ 00000000 00000000 00000000 -11111111 \ No newline at end of file +23333334 \ No newline at end of file diff --git a/sound/jump.wav b/sound/jump.wav new file mode 100644 index 0000000..ad420f8 Binary files /dev/null and b/sound/jump.wav differ diff --git a/src/GameFrame.java b/src/GameFrame.java index f81bdab..b8e47ad 100644 --- a/src/GameFrame.java +++ b/src/GameFrame.java @@ -5,6 +5,8 @@ Runs the constructor in GamePanel class */ import java.awt.*; import java.io.IOException; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; import javax.swing.*; public class GameFrame extends JFrame{ @@ -31,6 +33,10 @@ public class GameFrame extends JFrame{ // exceptions are raised when tiles are not found or are of incorrect dimensions menu.setVisible(true); menu.launchGame.setText("Invalid sprites error"); + } catch (UnsupportedAudioFileException e) { + throw new RuntimeException(e); + } catch (LineUnavailableException e) { + throw new RuntimeException(e); } this.add(game); this.setTitle("GUI is cool!"); //set title for frame diff --git a/src/GamePanel.java b/src/GamePanel.java index 556bfef..04f957e 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -18,6 +18,8 @@ import java.io.IOException; import javax.imageio.ImageIO; import java.util.ArrayList; import java.util.Scanner; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; import javax.swing.*; public class GamePanel extends JPanel implements Runnable, KeyListener{ @@ -46,7 +48,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ public BufferedImage box = getImage("img/tiles/boxes/box.png"); public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.png"); - public GamePanel() throws IOException, SpriteException { + public GamePanel() throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { camera = new Camera(0); background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT); for (int i = 0; i < 11; i++) { @@ -141,7 +143,7 @@ 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"); + MapReader.inputMap(map, "saves/Level1.txt"); } catch (IOException | SpriteException e) { throw new RuntimeException(e); } diff --git a/src/MapReader.java b/src/MapReader.java index 7776806..274182e 100644 --- a/src/MapReader.java +++ b/src/MapReader.java @@ -4,6 +4,12 @@ import java.util.Scanner; public class MapReader { //Input game map + /* + 1: Normal Grass + 2: Left Grass + 3: Middle Grass + 4: Right Grass + */ public static void inputMap(ArrayList map, String filePath) throws IOException, SpriteException { String file = FileManager.readFile(filePath); int x = 0; @@ -15,6 +21,12 @@ public class MapReader { } else if(file.charAt(i)=='1'){ map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grass.png"))); + } else if(file.charAt(i)=='2'){ + map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassLeft.png"))); + } else if(file.charAt(i)=='3'){ + map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMid.png"))); + } else if(file.charAt(i)=='4'){ + map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassRight.png"))); } x+=Tile.length; } diff --git a/src/Player.java b/src/Player.java index d8e4858..9658853 100644 --- a/src/Player.java +++ b/src/Player.java @@ -2,10 +2,13 @@ Paddle class defines behaviours for the left and right player-controlled paddles */ +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; +import java.io.IOException; public class Player extends GenericSprite { public final int SPEED = 5; @@ -13,14 +16,16 @@ public class Player extends GenericSprite { public static final int PLAYER_HEIGHT = 97; public int lastXDirection, lastYDirection, lastFrame; public int upKey, downKey, rightKey, leftKey; + + private Sound jump; // sA[0] is -x, -y // sA[1] is x, -y // sA[2] is -x, y // sA[3] is x, y public BufferedImage[][][] spriteArray; - public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][][] sprites) { + public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][][] sprites) throws UnsupportedAudioFileException, LineUnavailableException, IOException { super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH); - + jump = new Sound("sound/jump.wav"); this.upKey = upKey; this.downKey = downKey; this.leftKey = leftKey; @@ -82,6 +87,7 @@ public class Player extends GenericSprite { xVelocity -= 1; } if(upPressed&isGrounded){ + jump.start(); yVelocity = -10; } xVelocity*=0.9; diff --git a/src/Sound.java b/src/Sound.java new file mode 100644 index 0000000..f5c24bf --- /dev/null +++ b/src/Sound.java @@ -0,0 +1,19 @@ +import javax.sound.sampled.*; +import java.io.File; +import java.io.IOException; + +public class Sound { + private AudioInputStream audioInputStream; + private Clip clip; + private File file; + public Sound(String filePath) throws UnsupportedAudioFileException, IOException, LineUnavailableException { + file = new File(filePath); + audioInputStream = AudioSystem.getAudioInputStream(file); + clip = AudioSystem.getClip(); + clip.open(audioInputStream); + } + public void start(){ + clip.setFramePosition(0); + clip.start(); + } +}