Advertisement
Guest User

Board.java

a guest
Oct 30th, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.86 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  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.DataLine;
  12. import javax.sound.sampled.LineUnavailableException;
  13. import javax.sound.sampled.UnsupportedAudioFileException;
  14. import javax.swing.JPanel;
  15.  
  16. public class Board extends JPanel implements Runnable, KeyListener {
  17.     private static final long serialVersionUID = -6980446336015276827L;
  18.     private int leftscore = 0;
  19.     private int rightscore = 0;
  20.     private double bx = 340;
  21.     private double bh = 340;
  22.     private Thread thread;
  23.     private double lx = 223, rx = 223;
  24.     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;
  25.     private Color trans = new Color(255, 255, 255, 0);
  26.     private Color ballColor = trans;
  27.     private Color rectColor = Color.white;
  28.     private double ballspeed = 0.3;
  29.  
  30.     public Board() {
  31.         super();
  32.         addKeyListener(this);
  33.     }
  34.  
  35.     public void lose() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  36.         File lose = new File("lose.wav");
  37.         AudioInputStream streamlose = AudioSystem.getAudioInputStream(lose);
  38.  
  39.         DataLine.Info info = new DataLine.Info(Clip.class, streamlose.getFormat());
  40.         Clip clip = (Clip) AudioSystem.getLine(info);
  41.         clip.open(streamlose);
  42.         clip.start();
  43.     }
  44.  
  45.     public void wall() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  46.         File wall = new File("wall.wav");
  47.         AudioInputStream streamwall = AudioSystem.getAudioInputStream(wall);
  48.  
  49.         DataLine.Info info = new DataLine.Info(Clip.class, streamwall.getFormat());
  50.         Clip clip = (Clip) AudioSystem.getLine(info);
  51.         clip.open(streamwall);
  52.         clip.start();
  53.     }
  54.  
  55.     public void paddle() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  56.         File paddle = new File("paddle.wav");
  57.         AudioInputStream streampaddle = AudioSystem.getAudioInputStream(paddle);
  58.  
  59.         DataLine.Info info = new DataLine.Info(Clip.class, streampaddle.getFormat());
  60.         Clip clip = (Clip) AudioSystem.getLine(info);
  61.         clip.open(streampaddle);
  62.         clip.start();
  63.     }
  64.  
  65.     public void addNotify() {
  66.         super.addNotify();
  67.         thread = new Thread(this);
  68.         thread.start();
  69.     }
  70.  
  71.     public void paint(Graphics g) {
  72.         super.paint(g);
  73.  
  74.         this.setFocusable(true);
  75.         setBackground(Color.BLACK);
  76.         g.setColor(Color.WHITE);
  77.         for (int i = 0; i < 510; i += 16) {
  78.             g.fillRect(340, i + 5, 1, 8);
  79.         }
  80.  
  81.         g.fillRect(151, 34, 38, 64);
  82.         g.setColor(Color.BLACK);
  83.         if (leftscore == 0) {
  84.             g.fillRect(161, 44, 18, 44);
  85.         } else if (leftscore == 1) {
  86.             g.fillRect(151, 34, 28, 64);
  87.         } else if (leftscore == 2) {
  88.             g.fillRect(151, 44, 28, 17);
  89.             g.fillRect(161, 71, 28, 17);
  90.         } else if (leftscore == 3) {
  91.             g.fillRect(151, 44, 28, 17);
  92.             g.fillRect(151, 71, 28, 17);
  93.         } else if (leftscore == 4) {
  94.             g.fillRect(161, 34, 18, 27);
  95.             g.fillRect(151, 71, 28, 27);
  96.         } else if (leftscore == 5) {
  97.             g.fillRect(161, 44, 28, 17);
  98.             g.fillRect(151, 71, 28, 17);
  99.         } else if (leftscore == 6) {
  100.             g.fillRect(161, 44, 28, 17);
  101.             g.fillRect(161, 71, 18, 17);
  102.         } else if (leftscore == 7) {
  103.             g.fillRect(151, 44, 28, 54);
  104.         } else if (leftscore == 8) {
  105.             g.fillRect(161, 44, 18, 17);
  106.             g.fillRect(161, 71, 18, 17);
  107.         } else if (leftscore == 9) {
  108.             g.fillRect(161, 44, 18, 17);
  109.             g.fillRect(151, 71, 28, 27);
  110.         } else if (leftscore == 10) {
  111.             g.fillRect(161, 44, 18, 44);
  112.             g.setColor(Color.WHITE);
  113.             g.fillRect(131, 34, 10, 64);
  114.         }
  115.         g.setColor(Color.WHITE);
  116.         g.fillRect(491, 34, 38, 64);
  117.         g.setColor(Color.BLACK);
  118.         if (rightscore == 0) {
  119.             g.fillRect(501, 44, 18, 44);
  120.         } else if (rightscore == 1) {
  121.             g.fillRect(491, 34, 28, 64);
  122.         } else if (rightscore == 2) {
  123.             g.fillRect(491, 44, 28, 17);
  124.             g.fillRect(501, 71, 28, 17);
  125.         } else if (rightscore == 3) {
  126.             g.fillRect(491, 44, 28, 17);
  127.             g.fillRect(491, 71, 28, 17);
  128.         } else if (rightscore == 4) {
  129.             g.fillRect(501, 34, 18, 27);
  130.             g.fillRect(491, 71, 28, 27);
  131.         } else if (rightscore == 5) {
  132.             g.fillRect(501, 44, 28, 17);
  133.             g.fillRect(491, 71, 28, 17);
  134.         } else if (rightscore == 6) {
  135.             g.fillRect(501, 44, 28, 17);
  136.             g.fillRect(501, 71, 18, 17);
  137.         } else if (rightscore == 7) {
  138.             g.fillRect(491, 44, 28, 54);
  139.         } else if (rightscore == 8) {
  140.             g.fillRect(501, 44, 18, 17);
  141.             g.fillRect(501, 71, 18, 17);
  142.         } else if (rightscore == 9) {
  143.             g.fillRect(501, 44, 18, 17);
  144.             g.fillRect(491, 71, 28, 27);
  145.         } else if (rightscore == 10) {
  146.             g.fillRect(501, 44, 18, 44);
  147.             g.setColor(Color.WHITE);
  148.             g.fillRect(471, 34, 10, 64);
  149.         }
  150.  
  151.         g.setColor(rectColor);
  152.         g.fillRect(80, (int) lx, 10, 64);
  153.         g.fillRect(600, (int) rx, 10, 64);
  154.         g.setColor(ballColor);
  155.         g.fillRect((int) (bx), (int) (bh), 8, 8);
  156.     }
  157.  
  158.     public void run() {
  159.         while (true) {
  160.             repaint();
  161.             try {
  162.                 Thread.sleep(1);
  163.             } catch (InterruptedException e) {
  164.             }
  165.             keyChecks();
  166.             if (win) {
  167.                 win();
  168.             }
  169.             if (!win) {
  170.                 if (bup) {
  171.                     bh -= ballspeed;
  172.                     if (bh <= 0) {
  173.                         bdown = true;
  174.                         bup = false;
  175.                         if (left || right) {
  176.                             try {
  177.                                 wall();
  178.                             } catch (UnsupportedAudioFileException e) {
  179.                                 e.printStackTrace();
  180.                             } catch (IOException e) {
  181.                                 e.printStackTrace();
  182.                             } catch (LineUnavailableException e) {
  183.                                 e.printStackTrace();
  184.                             }
  185.                         }
  186.                     }
  187.                 } else if (bdown) {
  188.                     bh += ballspeed;
  189.                     if (bh >= 475) {
  190.                         bup = true;
  191.                         bdown = false;
  192.                         if (left || right) {
  193.                             try {
  194.                                 wall();
  195.                             } catch (UnsupportedAudioFileException e) {
  196.                                 e.printStackTrace();
  197.                             } catch (IOException e) {
  198.                                 e.printStackTrace();
  199.                             } catch (LineUnavailableException e) {
  200.                                 e.printStackTrace();
  201.                             }
  202.                         }
  203.                     }
  204.                 }
  205.                 if (right) {
  206.                     bx += 0.2;
  207.                     if (bx >= 590 && bx <= 600 && bh >= rx-8 && bh <= rx + 64) {
  208.                         if (bh >= rx + 16 && bh <= rx + 48) {
  209.                             ballspeed = 0.3;
  210.                         } else if (bh >= rx + 0 && bh <= rx + 16) {
  211.                             ballspeed = 0.4;
  212.                         } else if (bh >= rx + 48 && bh <= rx + 64) {
  213.                             ballspeed = 0.4;
  214.                         }
  215.                         if (rup) {
  216.                             bdown = true;
  217.                             bup = false;
  218.                         } else if (rdown) {
  219.                             bup = true;
  220.                             bdown = false;
  221.                         }
  222.                         left = true;
  223.                         right = false;
  224.                         try {
  225.                             paddle();
  226.                         } catch (UnsupportedAudioFileException e) {
  227.                             e.printStackTrace();
  228.                         } catch (IOException e) {
  229.                             e.printStackTrace();
  230.                         } catch (LineUnavailableException e) {
  231.                             e.printStackTrace();
  232.                         }
  233.                     }
  234.                     if (bx > 680) {
  235.                         leftscore++;
  236.                         right = false;
  237.                         bx = 340;
  238.                         if (leftscore == 10) {
  239.                             win = true;
  240.                             bup = true;
  241.                             left = true;
  242.                         }
  243.                         ballColor = trans;
  244.                         rightServe = true;
  245.                         leftServe = false;
  246.                         canSpace = true;
  247.                         ballspeed = 0.3;
  248.                         try {
  249.                             lose();
  250.                         } catch (UnsupportedAudioFileException e) {
  251.                             e.printStackTrace();
  252.                         } catch (IOException e) {
  253.                             e.printStackTrace();
  254.                         } catch (LineUnavailableException e) {
  255.                             e.printStackTrace();
  256.                         }
  257.                     }
  258.                 }
  259.                 if (left) {
  260.                     bx -= 0.2;
  261.                     if (bx <= 90 && bx >= 80 && bh >= lx-8 && bh <= lx + 64) {
  262.                         if (bh >= rx + 16 && bh <= rx + 48) {
  263.                             ballspeed = 0.3;
  264.                         } else if (bh >= rx + 0 && bh <= rx + 16) {
  265.                             ballspeed = 0.4;
  266.                         } else if (bh >= rx + 48 && bh <= rx + 64) {
  267.                             ballspeed = 0.4;
  268.                         }
  269.                         if (lup) {
  270.                             bdown = true;
  271.                             bup = false;
  272.                         } else if (ldown) {
  273.                             bup = true;
  274.                             bdown = false;
  275.                         }
  276.                         right = true;
  277.                         left = false;
  278.                         try {
  279.                             paddle();
  280.                         } catch (UnsupportedAudioFileException e) {
  281.                             e.printStackTrace();
  282.                         } catch (IOException e) {
  283.                             e.printStackTrace();
  284.                         } catch (LineUnavailableException e) {
  285.                             e.printStackTrace();
  286.                         }
  287.                     }
  288.                     if (bx < 0) {
  289.                         rightscore++;
  290.                         left = false;
  291.                         bx = 340;
  292.                         if (rightscore == 10) {
  293.                             win = true;
  294.                             bup = true;
  295.                             left = true;
  296.                         }
  297.                         ballColor = trans;
  298.                         leftServe = true;
  299.                         rightServe = false;
  300.                         canSpace = true;
  301.                         ballspeed = 0.4;
  302.                         try {
  303.                             lose();
  304.                         } catch (UnsupportedAudioFileException e) {
  305.                             e.printStackTrace();
  306.                         } catch (IOException e) {
  307.                             e.printStackTrace();
  308.                         } catch (LineUnavailableException e) {
  309.                             e.printStackTrace();
  310.                         }
  311.                     }
  312.                 }
  313.             }
  314.         }
  315.     }
  316.  
  317.     private void win() {
  318.         rectColor = trans;
  319.         ballColor = Color.WHITE;
  320.         if (bup) {
  321.             bh -= 0.4;
  322.             if (bh <= 0) {
  323.                 bdown = true;
  324.                 bup = false;
  325.             }
  326.         } else if (bdown) {
  327.             bh += 0.4;
  328.             if (bh >= 475) {
  329.                 bup = true;
  330.                 bdown = false;
  331.             }
  332.         }
  333.         if (left) {
  334.             bx -= 0.2;
  335.             if (bx <= 0) {
  336.                 right = true;
  337.                 left = false;
  338.             }
  339.         } else if (right) {
  340.             bx += 0.2;
  341.             if (bx >= 672) {
  342.                 left = true;
  343.                 right = false;
  344.             }
  345.         }
  346.     }
  347.  
  348.     public void keyTyped(KeyEvent e) {
  349.  
  350.     }
  351.  
  352.     public void keyPressed(KeyEvent e) {
  353.         int key = e.getKeyCode();
  354.  
  355.         if (key == KeyEvent.VK_UP) {
  356.             rup = true;
  357.         }
  358.         if (key == KeyEvent.VK_DOWN) {
  359.             rdown = true;
  360.         }
  361.         if (key == KeyEvent.VK_W) {
  362.             lup = true;
  363.         }
  364.         if (key == KeyEvent.VK_S) {
  365.             ldown = true;
  366.         }
  367.         if (key == KeyEvent.VK_ESCAPE) {
  368.             if (!suspended) {
  369.                 thread.suspend();
  370.                 suspended = true;
  371.             } else {
  372.                 thread.resume();
  373.                 suspended = false;
  374.             }
  375.         }
  376.         if (key == KeyEvent.VK_SPACE) {
  377.             bdown = true;
  378.             if (canSpace) {
  379.                 if (rightServe)
  380.                     right = true;
  381.                 if (leftServe)
  382.                     left = true;
  383.                 ballColor = Color.white;
  384.                 canSpace = false;
  385.             }
  386.             if (win) {
  387.                 leftscore = 0;
  388.                 rightscore = 0;
  389.                 bx = 340;
  390.                 bh = 340;
  391.                 lx = 223;
  392.                 rx = 223;
  393.                 lup = false;
  394.                 rup = false;
  395.                 ldown = false;
  396.                 rdown = false;
  397.                 bup = false;
  398.                 bdown = false;
  399.                 right = false;
  400.                 left = false;
  401.                 rightServe = true;
  402.                 leftServe = false;
  403.                 canSpace = true;
  404.                 win = false;
  405.                 ballColor = trans;
  406.                 rectColor = Color.WHITE;
  407.             }
  408.         }
  409.     }
  410.  
  411.     private void keyChecks() {
  412.         if (rup && rx > 0) {
  413.             rx -= 0.6;
  414.             if (rx < 0)
  415.                 rx = 0;
  416.         }
  417.         if (lup && lx > 0) {
  418.             lx -= 0.6;
  419.             if (lx < 0)
  420.                 lx = 0;
  421.         }
  422.         if (ldown && lx < 418) {
  423.             lx += 0.6;
  424.             if (lx > 418)
  425.                 lx = 418;
  426.         }
  427.         if (rdown && rx < 418) {
  428.             rx += 0.6;
  429.             if (rx > 418)
  430.                 rx = 418;
  431.         }
  432.  
  433.     }
  434.  
  435.     public void keyReleased(KeyEvent e) {
  436.         int key = e.getKeyCode();
  437.  
  438.         if (key == KeyEvent.VK_UP) {
  439.             rup = false;
  440.         }
  441.         if (key == KeyEvent.VK_DOWN) {
  442.             rdown = false;
  443.         }
  444.         if (key == KeyEvent.VK_W) {
  445.             lup = false;
  446.         }
  447.         if (key == KeyEvent.VK_S) {
  448.             ldown = false;
  449.         }
  450.     }
  451. }
  452.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement