Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- 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.DataLine;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.UnsupportedAudioFileException;
- import javax.swing.JPanel;
- public class Board extends JPanel implements Runnable, KeyListener {
- private static final long serialVersionUID = -6980446336015276827L;
- private int leftscore = 0;
- private int rightscore = 0;
- private double bx = 340;
- private double bh = 340;
- private Thread thread;
- private double lx = 223, rx = 223;
- private boolean suspended = false, lup = false, rup = false, ldown = false, rdown = false, bdown = false, bup = false, right = false, left = false, rightServe = true, leftServe = false, win = false, canSpace = true;
- private Color trans = new Color(255, 255, 255, 0);
- private Color ballColor = trans;
- private Color rectColor = Color.white;
- private double ballspeed = 0.3;
- public Board() {
- super();
- addKeyListener(this);
- }
- public void lose() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
- File lose = new File("lose.wav");
- AudioInputStream streamlose = AudioSystem.getAudioInputStream(lose);
- DataLine.Info info = new DataLine.Info(Clip.class, streamlose.getFormat());
- Clip clip = (Clip) AudioSystem.getLine(info);
- clip.open(streamlose);
- clip.start();
- }
- public void wall() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
- File wall = new File("wall.wav");
- AudioInputStream streamwall = AudioSystem.getAudioInputStream(wall);
- DataLine.Info info = new DataLine.Info(Clip.class, streamwall.getFormat());
- Clip clip = (Clip) AudioSystem.getLine(info);
- clip.open(streamwall);
- clip.start();
- }
- public void paddle() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
- File paddle = new File("paddle.wav");
- AudioInputStream streampaddle = AudioSystem.getAudioInputStream(paddle);
- DataLine.Info info = new DataLine.Info(Clip.class, streampaddle.getFormat());
- Clip clip = (Clip) AudioSystem.getLine(info);
- clip.open(streampaddle);
- clip.start();
- }
- public void addNotify() {
- super.addNotify();
- thread = new Thread(this);
- thread.start();
- }
- public void paint(Graphics g) {
- super.paint(g);
- this.setFocusable(true);
- setBackground(Color.BLACK);
- g.setColor(Color.WHITE);
- for (int i = 0; i < 510; i += 16) {
- g.fillRect(340, i + 5, 1, 8);
- }
- g.fillRect(151, 34, 38, 64);
- g.setColor(Color.BLACK);
- if (leftscore == 0) {
- g.fillRect(161, 44, 18, 44);
- } else if (leftscore == 1) {
- g.fillRect(151, 34, 28, 64);
- } else if (leftscore == 2) {
- g.fillRect(151, 44, 28, 17);
- g.fillRect(161, 71, 28, 17);
- } else if (leftscore == 3) {
- g.fillRect(151, 44, 28, 17);
- g.fillRect(151, 71, 28, 17);
- } else if (leftscore == 4) {
- g.fillRect(161, 34, 18, 27);
- g.fillRect(151, 71, 28, 27);
- } else if (leftscore == 5) {
- g.fillRect(161, 44, 28, 17);
- g.fillRect(151, 71, 28, 17);
- } else if (leftscore == 6) {
- g.fillRect(161, 44, 28, 17);
- g.fillRect(161, 71, 18, 17);
- } else if (leftscore == 7) {
- g.fillRect(151, 44, 28, 54);
- } else if (leftscore == 8) {
- g.fillRect(161, 44, 18, 17);
- g.fillRect(161, 71, 18, 17);
- } else if (leftscore == 9) {
- g.fillRect(161, 44, 18, 17);
- g.fillRect(151, 71, 28, 27);
- } else if (leftscore == 10) {
- g.fillRect(161, 44, 18, 44);
- g.setColor(Color.WHITE);
- g.fillRect(131, 34, 10, 64);
- }
- g.setColor(Color.WHITE);
- g.fillRect(491, 34, 38, 64);
- g.setColor(Color.BLACK);
- if (rightscore == 0) {
- g.fillRect(501, 44, 18, 44);
- } else if (rightscore == 1) {
- g.fillRect(491, 34, 28, 64);
- } else if (rightscore == 2) {
- g.fillRect(491, 44, 28, 17);
- g.fillRect(501, 71, 28, 17);
- } else if (rightscore == 3) {
- g.fillRect(491, 44, 28, 17);
- g.fillRect(491, 71, 28, 17);
- } else if (rightscore == 4) {
- g.fillRect(501, 34, 18, 27);
- g.fillRect(491, 71, 28, 27);
- } else if (rightscore == 5) {
- g.fillRect(501, 44, 28, 17);
- g.fillRect(491, 71, 28, 17);
- } else if (rightscore == 6) {
- g.fillRect(501, 44, 28, 17);
- g.fillRect(501, 71, 18, 17);
- } else if (rightscore == 7) {
- g.fillRect(491, 44, 28, 54);
- } else if (rightscore == 8) {
- g.fillRect(501, 44, 18, 17);
- g.fillRect(501, 71, 18, 17);
- } else if (rightscore == 9) {
- g.fillRect(501, 44, 18, 17);
- g.fillRect(491, 71, 28, 27);
- } else if (rightscore == 10) {
- g.fillRect(501, 44, 18, 44);
- g.setColor(Color.WHITE);
- g.fillRect(471, 34, 10, 64);
- }
- g.setColor(rectColor);
- g.fillRect(80, (int) lx, 10, 64);
- g.fillRect(600, (int) rx, 10, 64);
- g.setColor(ballColor);
- g.fillRect((int) (bx), (int) (bh), 8, 8);
- }
- public void run() {
- while (true) {
- repaint();
- try {
- Thread.sleep(1);
- } catch (InterruptedException e) {
- }
- keyChecks();
- if (win) {
- win();
- }
- if (!win) {
- if (bup) {
- bh -= ballspeed;
- if (bh <= 0) {
- bdown = true;
- bup = false;
- if (left || right) {
- try {
- wall();
- } catch (UnsupportedAudioFileException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (LineUnavailableException e) {
- e.printStackTrace();
- }
- }
- }
- } else if (bdown) {
- bh += ballspeed;
- if (bh >= 475) {
- bup = true;
- bdown = false;
- if (left || right) {
- try {
- wall();
- } catch (UnsupportedAudioFileException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (LineUnavailableException e) {
- e.printStackTrace();
- }
- }
- }
- }
- if (right) {
- bx += 0.2;
- if (bx >= 590 && bx <= 600 && bh >= rx-8 && bh <= rx + 64) {
- if (bh >= rx + 16 && bh <= rx + 48) {
- ballspeed = 0.3;
- } else if (bh >= rx + 0 && bh <= rx + 16) {
- ballspeed = 0.4;
- } else if (bh >= rx + 48 && bh <= rx + 64) {
- ballspeed = 0.4;
- }
- if (rup) {
- bdown = true;
- bup = false;
- } else if (rdown) {
- bup = true;
- bdown = false;
- }
- left = true;
- right = false;
- try {
- paddle();
- } catch (UnsupportedAudioFileException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (LineUnavailableException e) {
- e.printStackTrace();
- }
- }
- if (bx > 680) {
- leftscore++;
- right = false;
- bx = 340;
- if (leftscore == 10) {
- win = true;
- bup = true;
- left = true;
- }
- ballColor = trans;
- rightServe = true;
- leftServe = false;
- canSpace = true;
- ballspeed = 0.3;
- try {
- lose();
- } catch (UnsupportedAudioFileException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (LineUnavailableException e) {
- e.printStackTrace();
- }
- }
- }
- if (left) {
- bx -= 0.2;
- if (bx <= 90 && bx >= 80 && bh >= lx-8 && bh <= lx + 64) {
- if (bh >= rx + 16 && bh <= rx + 48) {
- ballspeed = 0.3;
- } else if (bh >= rx + 0 && bh <= rx + 16) {
- ballspeed = 0.4;
- } else if (bh >= rx + 48 && bh <= rx + 64) {
- ballspeed = 0.4;
- }
- if (lup) {
- bdown = true;
- bup = false;
- } else if (ldown) {
- bup = true;
- bdown = false;
- }
- right = true;
- left = false;
- try {
- paddle();
- } catch (UnsupportedAudioFileException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (LineUnavailableException e) {
- e.printStackTrace();
- }
- }
- if (bx < 0) {
- rightscore++;
- left = false;
- bx = 340;
- if (rightscore == 10) {
- win = true;
- bup = true;
- left = true;
- }
- ballColor = trans;
- leftServe = true;
- rightServe = false;
- canSpace = true;
- ballspeed = 0.4;
- try {
- lose();
- } catch (UnsupportedAudioFileException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (LineUnavailableException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- private void win() {
- rectColor = trans;
- ballColor = Color.WHITE;
- if (bup) {
- bh -= 0.4;
- if (bh <= 0) {
- bdown = true;
- bup = false;
- }
- } else if (bdown) {
- bh += 0.4;
- if (bh >= 475) {
- bup = true;
- bdown = false;
- }
- }
- if (left) {
- bx -= 0.2;
- if (bx <= 0) {
- right = true;
- left = false;
- }
- } else if (right) {
- bx += 0.2;
- if (bx >= 672) {
- left = true;
- right = false;
- }
- }
- }
- public void keyTyped(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- if (key == KeyEvent.VK_UP) {
- rup = true;
- }
- if (key == KeyEvent.VK_DOWN) {
- rdown = true;
- }
- if (key == KeyEvent.VK_W) {
- lup = true;
- }
- if (key == KeyEvent.VK_S) {
- ldown = true;
- }
- if (key == KeyEvent.VK_ESCAPE) {
- if (!suspended) {
- thread.suspend();
- suspended = true;
- } else {
- thread.resume();
- suspended = false;
- }
- }
- if (key == KeyEvent.VK_SPACE) {
- bdown = true;
- if (canSpace) {
- if (rightServe)
- right = true;
- if (leftServe)
- left = true;
- ballColor = Color.white;
- canSpace = false;
- }
- if (win) {
- leftscore = 0;
- rightscore = 0;
- bx = 340;
- bh = 340;
- lx = 223;
- rx = 223;
- lup = false;
- rup = false;
- ldown = false;
- rdown = false;
- bup = false;
- bdown = false;
- right = false;
- left = false;
- rightServe = true;
- leftServe = false;
- canSpace = true;
- win = false;
- ballColor = trans;
- rectColor = Color.WHITE;
- }
- }
- }
- private void keyChecks() {
- if (rup && rx > 0) {
- rx -= 0.6;
- if (rx < 0)
- rx = 0;
- }
- if (lup && lx > 0) {
- lx -= 0.6;
- if (lx < 0)
- lx = 0;
- }
- if (ldown && lx < 418) {
- lx += 0.6;
- if (lx > 418)
- lx = 418;
- }
- if (rdown && rx < 418) {
- rx += 0.6;
- if (rx > 418)
- rx = 418;
- }
- }
- public void keyReleased(KeyEvent e) {
- int key = e.getKeyCode();
- if (key == KeyEvent.VK_UP) {
- rup = false;
- }
- if (key == KeyEvent.VK_DOWN) {
- rdown = false;
- }
- if (key == KeyEvent.VK_W) {
- lup = false;
- }
- if (key == KeyEvent.VK_S) {
- ldown = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement