├── .classpath ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── README.md ├── Snake2D.jar ├── bin └── com │ └── mtala3t │ └── snake2d │ ├── GameBoardPanel$GameThread.class │ ├── GameBoardPanel$TimerThread.class │ ├── GameBoardPanel.class │ ├── GameBoardWindow.class │ ├── InputManger.class │ ├── Main.class │ ├── MainScreen$MainScreenPanel.class │ ├── MainScreen.class │ ├── Snake.class │ ├── SnakeFood.class │ └── sound │ ├── LoopingByteInputStream.class │ ├── SoundManger$PlayThread.class │ └── SoundManger.class ├── src └── com │ └── mtala3t │ └── snake2d │ ├── GameBoardPanel.java │ ├── GameBoardWindow.java │ ├── InputManger.java │ ├── Main.java │ ├── MainScreen.java │ ├── Snake.java │ ├── SnakeFood.java │ └── sound │ ├── LoopingByteInputStream.java │ └── SoundManger.java └── start.wav /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Snake2D 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.6 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.6 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Snake Java 2D Game 2 | 3 | It is a simple Java 2D Snake game. The game is implemented using Java 2D graphics library and threads. 4 | 5 | It has three different levels (Easy, Normal, and Hard). The game has some basic features like player scoring, timer, and an interesting sound. 6 | 7 | The used IDE is Eclipse (I uploaded all project artifacts), also the game JAR file is uploaded. 8 | 9 | If you have any questions or want to continue this project feel free to do so :) 10 | 11 | Please visit my website. http://www.mohamedtalaat.net/ 12 | -------------------------------------------------------------------------------- /Snake2D.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/Snake2D.jar -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/GameBoardPanel$GameThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/GameBoardPanel$GameThread.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/GameBoardPanel$TimerThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/GameBoardPanel$TimerThread.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/GameBoardPanel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/GameBoardPanel.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/GameBoardWindow.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/GameBoardWindow.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/InputManger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/InputManger.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/Main.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/MainScreen$MainScreenPanel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/MainScreen$MainScreenPanel.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/MainScreen.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/MainScreen.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/Snake.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/Snake.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/SnakeFood.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/SnakeFood.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/sound/LoopingByteInputStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/sound/LoopingByteInputStream.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/sound/SoundManger$PlayThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/sound/SoundManger$PlayThread.class -------------------------------------------------------------------------------- /bin/com/mtala3t/snake2d/sound/SoundManger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/bin/com/mtala3t/snake2d/sound/SoundManger.class -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/GameBoardPanel.java: -------------------------------------------------------------------------------- 1 | package com.mtala3t.snake2d; 2 | 3 | import java.awt.Color; 4 | import java.awt.Font; 5 | import java.awt.Graphics; 6 | import java.awt.Graphics2D; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | import java.awt.geom.Ellipse2D; 10 | import java.awt.geom.Rectangle2D; 11 | 12 | import javax.swing.JPanel; 13 | import javax.swing.Timer; 14 | 15 | import com.mtala3t.snake2d.sound.SoundManger; 16 | 17 | /** 18 | * 19 | * @author Mohammed.Talaat (mtala3t) 20 | * @version 1.0 21 | */ 22 | @SuppressWarnings("serial") 23 | public class GameBoardPanel extends JPanel implements ActionListener { 24 | 25 | /** Creates a new instance of GameBoard */ 26 | 27 | private Snake snake; 28 | private SnakeFood snakeFood; 29 | 30 | private InputManger inputManager; 31 | private SoundManger soundManger = null; 32 | 33 | private Timer gameThread; 34 | private Timer timerThread; 35 | 36 | private boolean isGameOver = false; 37 | 38 | private int timer = 0; 39 | private int playerScore = 0; 40 | 41 | private String soundFilePath = "start.wav"; 42 | 43 | public GameBoardPanel(int level) { 44 | 45 | setBackground(Color.BLACK); 46 | setFocusable(true); 47 | 48 | snake = new Snake(); 49 | snakeFood = new SnakeFood(); 50 | 51 | inputManager = new InputManger(this); 52 | soundManger = new SoundManger(soundFilePath); 53 | 54 | gameThread = new Timer(getDelay(level), this); 55 | 56 | timerThread = new Timer(1000, new ActionListener() { 57 | 58 | @Override 59 | public void actionPerformed(ActionEvent e) { 60 | 61 | if (isGameOver()) { 62 | stopGame(); 63 | } 64 | 65 | timer++; 66 | } 67 | }); 68 | 69 | // timerThread.setLogTimers(true); 70 | // gameThread.setLogTimers(true); 71 | 72 | addKeyListener(inputManager); 73 | 74 | } 75 | 76 | private int getDelay(int level) { 77 | 78 | int delay = 0; 79 | 80 | if (level == 1) { 81 | delay = 140; 82 | } else if (level == 2) { 83 | delay = 70; 84 | 85 | } else if (level == 3) { 86 | delay = 40; 87 | 88 | } 89 | return delay; 90 | } 91 | 92 | public void paintComponent(Graphics g) { 93 | super.paintComponent(g); 94 | 95 | doDrawing(g); 96 | } 97 | 98 | public void doDrawing(Graphics g) { 99 | 100 | Graphics2D g2 = (Graphics2D) g; 101 | 102 | if (isGameRunning()) { 103 | 104 | snake.move(); 105 | 106 | checkCollision(); 107 | 108 | DrawSnakeFood(g2); 109 | 110 | } 111 | 112 | DrawStatusbar(g2); 113 | 114 | DrawBoundry(g2); 115 | 116 | DrawSnake(g2); 117 | 118 | } 119 | 120 | public void DrawBoundry(Graphics2D g2) { 121 | for (int i = 0; i < 17; i++) { 122 | Rectangle2D.Double rect = new Rectangle2D.Double(227.0 - i, 123 | 127.0 - i, 624, 480); 124 | 125 | g2.setColor(Color.YELLOW); 126 | 127 | g2.draw(rect); 128 | 129 | } 130 | } 131 | 132 | public void DrawSnake(Graphics2D g2) { 133 | 134 | for (int i = 0; i < snake.getSnakeBody().size(); i++) { 135 | 136 | if (i == 0) { 137 | g2.setColor(Color.RED); 138 | g2.fill(snake.getSnakeBody().get(i)); 139 | 140 | } else { 141 | g2.setColor(Color.ORANGE); 142 | g2.draw(snake.getSnakeBody().get(i)); 143 | } 144 | 145 | } 146 | } 147 | 148 | public void DrawSnakeFood(Graphics2D g2) { 149 | g2.setColor(Color.GREEN); 150 | g2.fill(snakeFood.getFood()); 151 | } 152 | 153 | public void DrawStatusbar(Graphics2D g2) { 154 | g2.setColor(Color.RED); 155 | g2.setFont(new Font("Comic Sans MS", Font.BOLD, 35)); 156 | g2.drawString("Snake2D Game", 390, 50); 157 | g2.setColor(Color.ORANGE); 158 | g2.drawString("mtala3t", 450, 100); 159 | 160 | g2.setFont(new Font("Comic Sans MS", Font.PLAIN, 15)); 161 | g2.setColor(Color.WHITE); 162 | g2.drawString("Press Esc for exit!", 5, 20); 163 | g2.drawString("Press Spacebar for pause!", 5, 50); 164 | 165 | g2.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); 166 | g2.drawString("Time: ", 210, 100); 167 | g2.drawString("Your Score: ", 680, 100); 168 | g2.setColor(Color.BLUE); 169 | g2.drawString("" + playerScore, 810, 100); 170 | g2.drawString("" + timer, 270, 100); 171 | 172 | if (isGameOver()) { 173 | g2.setColor(Color.WHITE); 174 | g2.drawString("Game Over!", 480, 350); 175 | 176 | } else if (!isGameRunning()) { 177 | g2.setColor(Color.WHITE); 178 | g2.drawString("Press SpaceBar to Start Game!", 400, 500); 179 | } 180 | 181 | } 182 | 183 | public void changeSnakeDirection(int direction) { 184 | this.snake.setDirection(direction); 185 | } 186 | 187 | public void checkCollision() { 188 | 189 | if (isSelfCollisioned() || isBoundryCollisioned()) { 190 | 191 | isGameOver = true; 192 | 193 | stopGame(); 194 | 195 | } 196 | 197 | if (isFoodCollisioned()) { 198 | 199 | snake.eat(); 200 | snakeFood = new SnakeFood(); 201 | playerScore += 5; 202 | } 203 | } 204 | 205 | public boolean isBoundryCollisioned() { 206 | if (snake.getDirection() == 1) { 207 | double centerY = ((Ellipse2D.Double) snake.getSnakeBody().get(0)) 208 | .getMinY(); 209 | return centerY < 127; 210 | } else if (snake.getDirection() == 2) { 211 | double centerY = ((Ellipse2D.Double) snake.getSnakeBody().get(0)) 212 | .getMaxY(); 213 | return centerY > 591; 214 | } else if (snake.getDirection() == 3) { 215 | double centerX = ((Ellipse2D.Double) snake.getSnakeBody().get(0)).x; 216 | return centerX > 819; 217 | } else if (snake.getDirection() == 4) { 218 | double centerX = ((Ellipse2D.Double) snake.getSnakeBody().get(0)) 219 | .getMinX(); 220 | return centerX < 227.0; 221 | } 222 | return false; 223 | } 224 | 225 | public boolean isSelfCollisioned() { 226 | 227 | if (snake.getDirection() == 1) { 228 | for (int i = 1; i < snake.getSnakeBody().size(); i++) { 229 | if ((((Ellipse2D.Double) snake.getSnakeBody().get(0)).getMinY() == ((Ellipse2D.Double) snake 230 | .getSnakeBody().get(i)).getMaxY()) 231 | && (((Ellipse2D.Double) snake.getSnakeBody().get(0)) 232 | .getCenterX() == ((Ellipse2D.Double) snake 233 | .getSnakeBody().get(i)).getCenterX())) { 234 | return true; 235 | } 236 | } 237 | 238 | } else if (snake.getDirection() == 2) { 239 | for (int i = 1; i < snake.getSnakeBody().size(); i++) { 240 | if ((((Ellipse2D.Double) snake.getSnakeBody().get(0)).getMaxY() == ((Ellipse2D.Double) snake 241 | .getSnakeBody().get(i)).getMinY()) 242 | && (((Ellipse2D.Double) snake.getSnakeBody().get(0)) 243 | .getCenterX() == ((Ellipse2D.Double) snake 244 | .getSnakeBody().get(i)).getCenterX())) { 245 | return true; 246 | } 247 | } 248 | 249 | } else if (snake.getDirection() == 3) { 250 | for (int i = 1; i < snake.getSnakeBody().size(); i++) { 251 | if ((((Ellipse2D.Double) snake.getSnakeBody().get(0)).getMaxX() == ((Ellipse2D.Double) snake 252 | .getSnakeBody().get(i)).getMinX()) 253 | && (((Ellipse2D.Double) snake.getSnakeBody().get(0)) 254 | .getCenterY() == ((Ellipse2D.Double) snake 255 | .getSnakeBody().get(i)).getCenterY())) { 256 | return true; 257 | } 258 | } 259 | 260 | } else if (snake.getDirection() == 4) { 261 | for (int i = 1; i < snake.getSnakeBody().size(); i++) { 262 | if ((((Ellipse2D.Double) snake.getSnakeBody().get(0)).getMinX() == ((Ellipse2D.Double) snake 263 | .getSnakeBody().get(i)).getMaxX()) 264 | && (((Ellipse2D.Double) snake.getSnakeBody().get(0)) 265 | .getCenterY() == ((Ellipse2D.Double) snake 266 | .getSnakeBody().get(i)).getCenterY())) { 267 | return true; 268 | } 269 | } 270 | } 271 | 272 | return false; 273 | 274 | } 275 | 276 | public boolean isFoodCollisioned() { 277 | 278 | boolean collisionedWithFood = false; 279 | 280 | int direction = snake.getDirection(); 281 | 282 | Ellipse2D.Double head = snake.getHead(); 283 | 284 | if (direction == 1) { 285 | if ((head.getCenterY() == snakeFood.getFood().getCenterY()) 286 | && (head.getCenterX() == snakeFood.getFood().getCenterX())) { 287 | collisionedWithFood = true; 288 | } else 289 | collisionedWithFood = false; 290 | } else if (direction == 2) { 291 | 292 | if ((head.getCenterY() == snakeFood.getFood().getCenterY()) 293 | && (head.getCenterX() == snakeFood.getFood().getCenterX())) { 294 | collisionedWithFood = true; 295 | } else 296 | collisionedWithFood = false; 297 | 298 | } else if (direction == 3) { 299 | if ((head.getCenterX() == snakeFood.getFood().getCenterX()) 300 | && (head.getCenterY() == snakeFood.getFood().getCenterY())) { 301 | collisionedWithFood = true; 302 | } else 303 | collisionedWithFood = false; 304 | } else if (direction == 4) { 305 | if ((head.getCenterX() == snakeFood.getFood().getCenterX()) 306 | && (head.getCenterY() == snakeFood.getFood().getCenterY())) { 307 | collisionedWithFood = true; 308 | } else 309 | collisionedWithFood = false; 310 | } 311 | 312 | return collisionedWithFood; 313 | 314 | } 315 | 316 | public void startGame() { 317 | 318 | if (gameThread.isRunning()) { 319 | gameThread.restart(); 320 | timerThread.restart(); 321 | soundManger.startSound(); 322 | 323 | } else { 324 | gameThread.start(); 325 | timerThread.start(); 326 | soundManger.startSound(); 327 | } 328 | 329 | } 330 | 331 | public void pauseGame() { 332 | 333 | gameThread.stop(); 334 | timerThread.stop(); 335 | soundManger.pauseSound(); 336 | repaint(); 337 | 338 | } 339 | 340 | public void stopGame() { 341 | 342 | gameThread.stop(); 343 | timerThread.stop(); 344 | soundManger.stopSound(); 345 | 346 | } 347 | 348 | public boolean isGameRunning() { 349 | return gameThread.isRunning() && !isGameOver(); 350 | } 351 | 352 | public boolean isGameOver() { 353 | return isGameOver; 354 | } 355 | 356 | @Override 357 | public void actionPerformed(ActionEvent arg0) { 358 | 359 | repaint(); 360 | 361 | } 362 | 363 | } 364 | -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/GameBoardWindow.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/src/com/mtala3t/snake2d/GameBoardWindow.java -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/InputManger.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/src/com/mtala3t/snake2d/InputManger.java -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/Main.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.mtala3t.snake2d; 5 | 6 | /** 7 | * 8 | * @author Mohammed.Talaat (mtala3t) 9 | * @version 1.0 10 | */ 11 | public class Main { 12 | 13 | /** 14 | * @param args 15 | */ 16 | public static void main(String[] args) { 17 | 18 | new MainScreen(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/MainScreen.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/src/com/mtala3t/snake2d/MainScreen.java -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/Snake.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/src/com/mtala3t/snake2d/Snake.java -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/SnakeFood.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/src/com/mtala3t/snake2d/SnakeFood.java -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/sound/LoopingByteInputStream.java: -------------------------------------------------------------------------------- 1 | package com.mtala3t.snake2d.sound; 2 | /* 3 | * To change this template, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author sherif 10 | */ 11 | import java.io.ByteArrayInputStream; 12 | import java.io.IOException; 13 | 14 | /** 15 | The LoopingByteInputStream is a ByteArrayInputStream that 16 | loops indefinitely. The looping stops when the close() method 17 | is called. 18 |

Possible ideas to extend this class:

21 | */ 22 | public class LoopingByteInputStream extends ByteArrayInputStream { 23 | 24 | private boolean closed; 25 | 26 | /** 27 | Creates a new LoopingByteInputStream with the specified 28 | byte array. The array is not copied. 29 | */ 30 | public LoopingByteInputStream(byte[] buffer) { 31 | super(buffer); 32 | closed = false; 33 | } 34 | 35 | 36 | /** 37 | Reads length bytes from the array. If the 38 | end of the array is reached, the reading starts over from 39 | the beginning of the array. Returns -1 if the array has 40 | been closed. 41 | */ 42 | public int read(byte[] buffer, int offset, int length) { 43 | if (closed) { 44 | return -1; 45 | } 46 | int totalBytesRead = 0; 47 | 48 | while (totalBytesRead < length) { 49 | int numBytesRead = super.read(buffer, 50 | offset + totalBytesRead, 51 | length - totalBytesRead); 52 | 53 | if (numBytesRead > 0) { 54 | totalBytesRead += numBytesRead; 55 | } 56 | else { 57 | reset(); 58 | } 59 | } 60 | return totalBytesRead; 61 | } 62 | 63 | 64 | /** 65 | Closes the stream. Future calls to the read() methods 66 | will return 1. 67 | */ 68 | public void close() throws IOException { 69 | super.close(); 70 | closed = true; 71 | } 72 | 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/com/mtala3t/snake2d/sound/SoundManger.java: -------------------------------------------------------------------------------- 1 | package com.mtala3t.snake2d.sound; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import javax.sound.sampled.AudioFormat; 7 | import javax.sound.sampled.AudioInputStream; 8 | import javax.sound.sampled.AudioSystem; 9 | import javax.sound.sampled.DataLine; 10 | import javax.sound.sampled.LineUnavailableException; 11 | import javax.sound.sampled.SourceDataLine; 12 | import javax.sound.sampled.UnsupportedAudioFileException; 13 | 14 | /** 15 | * 16 | * @author Mohammed.Talaat (mtala3t) 17 | * @version 1.0 18 | */ 19 | public class SoundManger { 20 | 21 | private PlayThread playThread; 22 | 23 | public SoundManger(String soundFilePath) { 24 | playThread = new PlayThread(soundFilePath); 25 | 26 | } 27 | 28 | @SuppressWarnings("deprecation") 29 | public void startSound() { 30 | if (playThread.isAlive()) 31 | playThread.resume(); 32 | else 33 | playThread.start(); 34 | } 35 | 36 | @SuppressWarnings("deprecation") 37 | public void pauseSound() { 38 | playThread.suspend(); 39 | } 40 | 41 | @SuppressWarnings("deprecation") 42 | public void stopSound() { 43 | playThread.stop(); 44 | } 45 | 46 | class PlayThread extends Thread { 47 | 48 | private AudioFormat audioFormat; 49 | private AudioInputStream audioInputStream; 50 | private SourceDataLine sourceDataLine; 51 | 52 | private String filePath = null; 53 | 54 | public PlayThread(String filePath) { 55 | this.filePath = filePath; 56 | } 57 | 58 | byte tempBuffer[] = new byte[10000]; 59 | 60 | public void run() { 61 | 62 | File soundFile = new File(filePath); 63 | 64 | while (true) { 65 | try { 66 | audioInputStream = AudioSystem 67 | .getAudioInputStream(soundFile); 68 | audioFormat = audioInputStream.getFormat(); 69 | 70 | DataLine.Info dataLineInfo = new DataLine.Info( 71 | SourceDataLine.class, audioFormat); 72 | 73 | sourceDataLine = (SourceDataLine) AudioSystem 74 | .getLine(dataLineInfo); 75 | sourceDataLine.open(audioFormat); 76 | } catch (LineUnavailableException ex) { 77 | ex.printStackTrace(); 78 | } catch (UnsupportedAudioFileException ex) { 79 | ex.printStackTrace(); 80 | } catch (IOException ex) { 81 | ex.printStackTrace(); 82 | } 83 | sourceDataLine.start(); 84 | try { 85 | int cnt; 86 | while ((cnt = audioInputStream.read(tempBuffer, 0, 87 | tempBuffer.length)) != -1) { 88 | if (cnt > 0) { 89 | sourceDataLine.write(tempBuffer, 0, cnt); 90 | } 91 | } 92 | sourceDataLine.flush(); 93 | sourceDataLine.close(); 94 | Thread.sleep(2000); 95 | } catch (Exception e) { 96 | e.printStackTrace(); 97 | System.exit(0); 98 | } 99 | } 100 | } 101 | 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /start.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtala3t/Snake-Java-2D-Game/cad6b88f63dd625724e894a99984b1892de1b48f/start.wav --------------------------------------------------------------------------------