Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. package com.tutorial.main;
  2.  
  3. import java.awt.event.KeyAdapter;
  4. import java.awt.event.KeyEvent;
  5. import java.io.File;
  6. import java.io.IOException;
  7.  
  8. import javax.sound.sampled.AudioInputStream;
  9. import javax.sound.sampled.AudioSystem;
  10. import javax.sound.sampled.Clip;
  11. import javax.sound.sampled.LineUnavailableException;
  12. import javax.sound.sampled.UnsupportedAudioFileException;
  13. //this class handles player movement
  14. public class PlayerMovement extends KeyAdapter {
  15.  
  16.  
  17. private Handler handler;
  18.  
  19. private boolean moveUp = false; //we have to use booleans for this to avoid
  20. private boolean moveDown = false; //weird delay on holding down a key
  21. private boolean moveLeft = false;
  22. private boolean moveRight = false;
  23.  
  24. public PlayerMovement(Handler handler) { //this is a constructor
  25. this.handler = handler;
  26. //what we pass to this function
  27. //will be set to "handler" defined above
  28. }
  29.  
  30. public class BackGroundSound {
  31. Long currentFrame;
  32. Clip clip;
  33. String status;
  34. AudioInputStream audioInputStream;
  35. String filePath;
  36. public void main(String[] args) {
  37. try {
  38. filePath = "/Users/liangyiming/Desktop/Footsteps-SoundBible.com-534261997.wav";
  39. BackGroundSound audioPlayer = new BackGroundSound();
  40. audioPlayer.play();
  41. }
  42. catch (Exception ex) {
  43. System.out.println("Error with playing sound.");
  44. ex.printStackTrace();
  45. }
  46. }
  47. public BackGroundSound() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  48. // create AudioInputStream object
  49. audioInputStream = AudioSystem.getAudioInputStream(new File("/Users/liangyiming/Desktop/Footsteps-SoundBible.com-534261997.wav").getAbsoluteFile());
  50. // create clip reference
  51. clip = AudioSystem.getClip();
  52. // open audioInputStream to the clip
  53. clip.open(audioInputStream);
  54. clip.loop(Clip.LOOP_CONTINUOUSLY);
  55. }
  56. public void play() {
  57. clip.start();
  58. status = "play";
  59. }
  60. public void stop() throws UnsupportedAudioFileException,
  61. IOException, LineUnavailableException {
  62. currentFrame = 0L;
  63. clip.stop();
  64. clip.close();
  65. }
  66.  
  67. public void keyPressed(KeyEvent e) {
  68. int key = e.getKeyCode();
  69.  
  70. for (int i = 0; i < handler.object.size(); i++) { //loop through all game objects
  71. GameObject tempObjectKeyIn = handler.object.get(i);
  72. if (tempObjectKeyIn.getId() == ID.Player) {
  73. //key events that control Player1
  74. //THIS CODE GETS RUN WHEN A BUTTON IS PRESSED
  75. if(key == KeyEvent.VK_W) { //if W pressed
  76. moveUp = true; //set moveUp to true
  77. play();
  78. tempObjectKeyIn.setVelY(-5);
  79. System.out.println("w press");
  80. } else if (key == KeyEvent.VK_S) {//else if S pressed
  81. moveDown = true; //set moveDown to true
  82. play();
  83. tempObjectKeyIn.setVelY(5);
  84. System.out.println("S press");
  85. } else if (key == KeyEvent.VK_A) {
  86. moveLeft = true;
  87. play();
  88. System.out.println("1.0 a presses");
  89. tempObjectKeyIn.setVelX(-5);
  90. } else if (key == KeyEvent.VK_D) {
  91. moveRight = true;
  92. play();
  93. tempObjectKeyIn.setVelX(5);
  94. System.out.println("d press");
  95. }
  96. }
  97. }
  98. //System.out.println(key);
  99. }
  100.  
  101. public void keyReleased(KeyEvent e) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  102. int key = e.getKeyCode();
  103. /**
  104. * NOTE: As Y increases, things move down the screen. So Y = 0 is the top of the screen, I think.
  105. */
  106. for (int i = 0; i < handler.object.size(); i++) {
  107. GameObject tempObjectKeyIn = handler.object.get(i);
  108. if (tempObjectKeyIn.getId() == ID.Player) {
  109. if (key == KeyEvent.VK_W) { //when W key released
  110. System.out.println("w release");
  111. stop();
  112. moveUp = false; //set moveUp to false
  113. if (moveDown == true) { //but if you're moving down
  114. tempObjectKeyIn.setVelY(5); //keep doing it
  115. } else { //if not
  116. tempObjectKeyIn.setVelY(0); //then you're not moving vertically
  117. }
  118. } else if (key == KeyEvent.VK_S) { //S key released
  119. System.out.println("s release");
  120. moveDown = false;
  121. stop();
  122. if (moveUp == true) {
  123. tempObjectKeyIn.setVelY(-5);
  124. } else {
  125. tempObjectKeyIn.setVelY(0);
  126. }
  127. } else if (key == KeyEvent.VK_A) {
  128. System.out.println("a release");
  129. moveLeft = false;
  130. stop();
  131. if (moveRight == true) {
  132. tempObjectKeyIn.setVelX(5);
  133. } else {
  134. tempObjectKeyIn.setVelX(0);
  135. }
  136. } else if (key == KeyEvent.VK_D) {
  137. System.out.println("d release");
  138. stop();
  139. moveRight = false;
  140. if (moveLeft == true) {
  141. tempObjectKeyIn.setVelX(-5);
  142. } else {
  143. tempObjectKeyIn.setVelX(0);
  144. }
  145. }
  146. if (key == KeyEvent.VK_ESCAPE) System.exit(1); //press escape to close game, probably replace with a pause menu later
  147. }
  148. }
  149. }
  150. //System.out.println("o" + e);
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement