Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tutorial.main;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.io.File;
- import java.io.IOException;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.UnsupportedAudioFileException;
- //this class handles player movement
- public class PlayerMovement extends KeyAdapter {
- private Handler handler;
- private boolean moveUp = false; //we have to use booleans for this to avoid
- private boolean moveDown = false; //weird delay on holding down a key
- private boolean moveLeft = false;
- private boolean moveRight = false;
- public PlayerMovement(Handler handler) { //this is a constructor
- this.handler = handler;
- //what we pass to this function
- //will be set to "handler" defined above
- }
- public class BackGroundSound {
- Long currentFrame;
- Clip clip;
- String status;
- AudioInputStream audioInputStream;
- String filePath;
- public void main(String[] args) {
- try {
- filePath = "/Users/liangyiming/Desktop/Footsteps-SoundBible.com-534261997.wav";
- BackGroundSound audioPlayer = new BackGroundSound();
- audioPlayer.play();
- }
- catch (Exception ex) {
- System.out.println("Error with playing sound.");
- ex.printStackTrace();
- }
- }
- public BackGroundSound() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
- // create AudioInputStream object
- audioInputStream = AudioSystem.getAudioInputStream(new File("/Users/liangyiming/Desktop/Footsteps-SoundBible.com-534261997.wav").getAbsoluteFile());
- // create clip reference
- clip = AudioSystem.getClip();
- // open audioInputStream to the clip
- clip.open(audioInputStream);
- clip.loop(Clip.LOOP_CONTINUOUSLY);
- }
- public void play() {
- clip.start();
- status = "play";
- }
- public void stop() throws UnsupportedAudioFileException,
- IOException, LineUnavailableException {
- currentFrame = 0L;
- clip.stop();
- clip.close();
- }
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- for (int i = 0; i < handler.object.size(); i++) { //loop through all game objects
- GameObject tempObjectKeyIn = handler.object.get(i);
- if (tempObjectKeyIn.getId() == ID.Player) {
- //key events that control Player1
- //THIS CODE GETS RUN WHEN A BUTTON IS PRESSED
- if(key == KeyEvent.VK_W) { //if W pressed
- moveUp = true; //set moveUp to true
- play();
- tempObjectKeyIn.setVelY(-5);
- System.out.println("w press");
- } else if (key == KeyEvent.VK_S) {//else if S pressed
- moveDown = true; //set moveDown to true
- play();
- tempObjectKeyIn.setVelY(5);
- System.out.println("S press");
- } else if (key == KeyEvent.VK_A) {
- moveLeft = true;
- play();
- System.out.println("1.0 a presses");
- tempObjectKeyIn.setVelX(-5);
- } else if (key == KeyEvent.VK_D) {
- moveRight = true;
- play();
- tempObjectKeyIn.setVelX(5);
- System.out.println("d press");
- }
- }
- }
- //System.out.println(key);
- }
- public void keyReleased(KeyEvent e) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
- int key = e.getKeyCode();
- /**
- * NOTE: As Y increases, things move down the screen. So Y = 0 is the top of the screen, I think.
- */
- for (int i = 0; i < handler.object.size(); i++) {
- GameObject tempObjectKeyIn = handler.object.get(i);
- if (tempObjectKeyIn.getId() == ID.Player) {
- if (key == KeyEvent.VK_W) { //when W key released
- System.out.println("w release");
- stop();
- moveUp = false; //set moveUp to false
- if (moveDown == true) { //but if you're moving down
- tempObjectKeyIn.setVelY(5); //keep doing it
- } else { //if not
- tempObjectKeyIn.setVelY(0); //then you're not moving vertically
- }
- } else if (key == KeyEvent.VK_S) { //S key released
- System.out.println("s release");
- moveDown = false;
- stop();
- if (moveUp == true) {
- tempObjectKeyIn.setVelY(-5);
- } else {
- tempObjectKeyIn.setVelY(0);
- }
- } else if (key == KeyEvent.VK_A) {
- System.out.println("a release");
- moveLeft = false;
- stop();
- if (moveRight == true) {
- tempObjectKeyIn.setVelX(5);
- } else {
- tempObjectKeyIn.setVelX(0);
- }
- } else if (key == KeyEvent.VK_D) {
- System.out.println("d release");
- stop();
- moveRight = false;
- if (moveLeft == true) {
- tempObjectKeyIn.setVelX(-5);
- } else {
- tempObjectKeyIn.setVelX(0);
- }
- }
- if (key == KeyEvent.VK_ESCAPE) System.exit(1); //press escape to close game, probably replace with a pause menu later
- }
- }
- }
- //System.out.println("o" + e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement