Sound Class and some Tile Ids I guess

master
bob 2022-06-03 13:50:13 -04:00
parent bcd896ed45
commit 8a01e8d541
10 changed files with 50 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 955 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 739 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 957 B

View File

@ -14,4 +14,4 @@
00000000 00000000
00000000 00000000
00000000 00000000
11111111 23333334

BIN
sound/jump.wav Normal file

Binary file not shown.

View File

@ -5,6 +5,8 @@ Runs the constructor in GamePanel class
*/ */
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*; import javax.swing.*;
public class GameFrame extends JFrame{ 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 // exceptions are raised when tiles are not found or are of incorrect dimensions
menu.setVisible(true); menu.setVisible(true);
menu.launchGame.setText("Invalid sprites error"); menu.launchGame.setText("Invalid sprites error");
} catch (UnsupportedAudioFileException e) {
throw new RuntimeException(e);
} catch (LineUnavailableException e) {
throw new RuntimeException(e);
} }
this.add(game); this.add(game);
this.setTitle("GUI is cool!"); //set title for frame this.setTitle("GUI is cool!"); //set title for frame

View File

@ -18,6 +18,8 @@ import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*; import javax.swing.*;
public class GamePanel extends JPanel implements Runnable, KeyListener{ 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 box = getImage("img/tiles/boxes/box.png");
public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.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); camera = new Camera(0);
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT); background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT);
for (int i = 0; i < 11; i++) { 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 //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 { try {
MapReader.inputMap(map, "src/Level1.txt"); MapReader.inputMap(map, "saves/Level1.txt");
} catch (IOException | SpriteException e) { } catch (IOException | SpriteException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -4,6 +4,12 @@ import java.util.Scanner;
public class MapReader { public class MapReader {
//Input game map //Input game map
/*
1: Normal Grass
2: Left Grass
3: Middle Grass
4: Right Grass
*/
public static void inputMap(ArrayList<Tile> map, String filePath) throws IOException, SpriteException { public static void inputMap(ArrayList<Tile> map, String filePath) throws IOException, SpriteException {
String file = FileManager.readFile(filePath); String file = FileManager.readFile(filePath);
int x = 0; int x = 0;
@ -15,6 +21,12 @@ public class MapReader {
} }
else if(file.charAt(i)=='1'){ else if(file.charAt(i)=='1'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grass.png"))); 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; x+=Tile.length;
} }

View File

@ -2,10 +2,13 @@
Paddle class defines behaviours for the left and right player-controlled paddles */ 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.*;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver; import java.awt.image.ImageObserver;
import java.io.IOException;
public class Player extends GenericSprite { public class Player extends GenericSprite {
public final int SPEED = 5; public final int SPEED = 5;
@ -13,14 +16,16 @@ public class Player extends GenericSprite {
public static final int PLAYER_HEIGHT = 97; public static final int PLAYER_HEIGHT = 97;
public int lastXDirection, lastYDirection, lastFrame; public int lastXDirection, lastYDirection, lastFrame;
public int upKey, downKey, rightKey, leftKey; public int upKey, downKey, rightKey, leftKey;
private Sound jump;
// sA[0] is -x, -y // sA[0] is -x, -y
// sA[1] is x, -y // sA[1] is x, -y
// sA[2] is -x, y // sA[2] is -x, y
// sA[3] is x, y // sA[3] is x, y
public BufferedImage[][][] spriteArray; 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); super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH);
jump = new Sound("sound/jump.wav");
this.upKey = upKey; this.upKey = upKey;
this.downKey = downKey; this.downKey = downKey;
this.leftKey = leftKey; this.leftKey = leftKey;
@ -82,6 +87,7 @@ public class Player extends GenericSprite {
xVelocity -= 1; xVelocity -= 1;
} }
if(upPressed&isGrounded){ if(upPressed&isGrounded){
jump.start();
yVelocity = -10; yVelocity = -10;
} }
xVelocity*=0.9; xVelocity*=0.9;

19
src/Sound.java Normal file
View File

@ -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();
}
}