2017年7月29日 星期六

(Java) Breaker Ball (單人與雙人)







  1. BreakerBall  
  2. --------------------------------------------------------------------------------------------------------------------------  
  3. package breakerball;  
  4. import static com.sun.java.accessibility.util.AWTEventMonitor.addWindowListener;  
  5. import java.awt.Color;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.awt.event.WindowAdapter;  
  9. import java.util.logging.Level;  
  10. import java.util.logging.Logger;  
  11. import javax.swing.*;  
  12. import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;  
  13.   
  14. public class BreakerBall extends JFrame {//650 700  
  15.     static BreakerBall w1  ;  
  16.     static play2 p = new play2();  
  17.    private  BreakerBall(){  
  18.        super(" BreakerBall");  
  19.        addWindowListener(new java.awt.event.WindowAdapter() {  
  20.             public void windowOpened(java.awt.event.WindowEvent evt) {  
  21.                   w1 = new BreakerBall();  
  22.             }  
  23.             public void windowClosing(java.awt.event.WindowEvent evt) {  
  24.               System.out.println("AS2525");  
  25.             }  
  26.         });  
  27.    }  
  28.     public static void main(String[] args) {  
  29.      w1 = new BreakerBall();  
  30.      w1.setResizable(false);  
  31.      w1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
  32.      p.setVisible(true);  
  33.     p.jButton1.addActionListener(new ActionListener() {  
  34.          @Override  
  35.          public void actionPerformed(ActionEvent e) {  
  36.               Input();  
  37.          }  
  38.                
  39.         });  
  40.       p.jButton2.addActionListener(new ActionListener() {  
  41.          @Override  
  42.          public void actionPerformed(ActionEvent e) {  
  43.               Input2();  
  44.          }  
  45.                
  46.         });  
  47.        
  48.     }  
  49.     private static void Input(){  
  50.         try{  
  51.                 int r = Integer.parseInt(p.jTextField1.getText()) ;  
  52.                 int c = Integer.parseInt(p.jTextField2.getText()) ;  
  53.                 w1.setBounds(100,100,650,700);  
  54.                 w1.setVisible(true);  
  55.                 Play p1 = new Play(r,c) ;  
  56.                 p1.balldirX = -(p.jSlider1.getValue()) ;  
  57.                 p1.balldirY =  p1.balldirY + -(p.jSlider1.getValue()) ;  
  58.                 w1.add(p1);  
  59.                  
  60.         }catch(Exception e){  
  61.             JOptionPane.showMessageDialog(null"無效整數");  
  62.             e.printStackTrace();  
  63.         }  
  64.     }  
  65.      private static void Input2(){  
  66.          try{  
  67.              w1.setBounds(100,100,650,700);  
  68.              w1.setVisible(true);  
  69.              play3 p2 = new play3() ;  
  70.              p2.balldirX = -(p.jSlider1.getValue()) ;  
  71.              p2.balldirY =  p2.balldirY + -(p.jSlider1.getValue()) ;  
  72.              w1.add(p2);  
  73.          }catch(Exception e){  
  74.             e.printStackTrace();  
  75.         }  
  76.      }  
  77. }  
  78.   
  79. Play  
  80. --------------------------------------------------------------------------------------------------------------------------  
  81.   
  82. /* 
  83.  * To change this license header, choose License Headers in Project Properties. 
  84.  * To change this template file, choose Tools | Templates 
  85.  * and open the template in the editor. 
  86.  */  
  87. package breakerball;  
  88.   
  89. import static breakerball.BreakerBall.w1;  
  90. import static com.sun.java.accessibility.util.AWTEventMonitor.addWindowListener;  
  91. import java.awt.BasicStroke;  
  92. import java.awt.Color;  
  93. import java.awt.Font;  
  94. import java.awt.Graphics;  
  95. import java.awt.Graphics2D;  
  96. import java.awt.Rectangle;  
  97. import java.awt.event.ActionEvent;  
  98. import java.awt.event.ActionListener;  
  99. import java.awt.event.KeyEvent;  
  100. import java.awt.event.KeyListener;  
  101. import java.awt.event.WindowAdapter;  
  102. import java.awt.event.WindowEvent;  
  103. import java.awt.event.WindowListener;  
  104. import java.io.File;  
  105. import java.io.IOException;  
  106. import java.util.logging.Level;  
  107. import java.util.logging.Logger;  
  108. import javax.sound.sampled.AudioSystem;  
  109. import javax.sound.sampled.Clip;  
  110. import javax.sound.sampled.LineUnavailableException;  
  111. import javax.sound.sampled.UnsupportedAudioFileException;  
  112. import javax.swing.JPanel;  
  113. import javax.swing.Timer;  
  114.   
  115. public class Play extends JPanel implements ActionListener, KeyListener {  
  116.   
  117.     File Clip = new File("drum1.wav");  
  118.     File a = new File("pianoC.wav");  
  119.     int score = 0;  
  120.     boolean play = false;  
  121.     int totalbricks;     //總磚  
  122.     int playerX = 325, playerX2 = 150;  
  123.     int ballposX = 180;  
  124.     int ballposY = 380;  
  125.     int balldirX = 0;  
  126.     int balldirY = -1;  
  127.     Timer timer;  
  128.     Brick Brick;  
  129.   
  130.     public Play(int r, int c) {  
  131.         addKeyListener(this);  
  132.         Brick = new Brick(r, c);  
  133.         totalbricks = r * c;  
  134.         this.setFocusable(true);//設置可透過鍵盤獲得值  
  135.         this.setFocusTraversalKeysEnabled(true);//設置焦點穿越鍵啟用  
  136.         timer = new Timer(5this);// ActionListener , KeyListener  
  137.         timer.start();  
  138.     }  
  139.   
  140.     public void paint(Graphics G) {//Graphics2D 重製 類別繪圖      
  141.         //背景  
  142.         G.setColor(Color.BLACK);  
  143.         G.fillRect(11650700);  
  144.         Brick.draw((Graphics2D) G);  
  145.         //邊界  
  146.         G.setColor(Color.RED);  
  147.         G.fillRect(005695);  
  148.         G.fillRect(006455);  
  149.         G.fillRect(64105695);  
  150.         //Font  
  151.         G.setColor(Color.RED);  
  152.         G.setFont(new Font("標楷體"321));  
  153.         G.drawString("分數 :" + score, 10650);  
  154.         //槳  
  155.         G.setColor(Color.GREEN);  
  156.         G.fillRect(playerX, 60010012);  
  157.         //球  
  158.         G.setColor(Color.YELLOW);  
  159.         G.fillOval(ballposX, ballposY, 1515);  
  160.         //Over  
  161.         if (ballposY > 670) {  
  162.             ballposX = 120;  
  163.             ballposY = 380;  
  164.             playerX = 500;  
  165.             play = false;  
  166.             timer.stop();  
  167.             G.setColor(Color.RED);  
  168.             G.setFont(new Font("標楷體"348));  
  169.             G.drawString("遊戲結束重新開始 Enter"50450);  
  170.         }  
  171.         if (totalbricks == 0) {  
  172.             ballposX = 120;  
  173.             ballposY = 380;  
  174.             playerX = 500;  
  175.             play = false;  
  176.             timer.stop();  
  177.             G.setColor(Color.RED);  
  178.             G.setFont(new Font("標楷體"348));  
  179.             G.drawString("你贏了分數" + score, 50450);  
  180.         }  
  181.         G.dispose();  
  182.     }  
  183.   
  184.     public void actionPerformed(ActionEvent e) {  
  185.         if (play) {//鍵盤開始          
  186.             if (new Rectangle(ballposX, ballposY, 1515).intersects(new Rectangle(playerX, 6001008))) { //交叉  
  187.                 Plsy2(Clip);  
  188.                 balldirY = -balldirY;  
  189.             } //Y 變負 往內  
  190.   
  191.             ballposX += balldirX;  
  192.             ballposY += balldirY;  
  193.   
  194.             if (ballposX < 0) {  
  195.                 balldirX = -balldirX; //X 變正 往外  
  196.             }  
  197.             if (ballposY < 0) {  
  198.                 balldirY = -balldirY; //Y 變正 往外  
  199.             }  
  200.             if (ballposX > 630) {  
  201.                 balldirX = -balldirX; //X 變負 往內  
  202.             }  
  203.         }  
  204.         for (int i = 0; i < Brick.map.length; i++) {  
  205.             for (int j = 0; j < Brick.map[0].length; j++) {  
  206.                 if (Brick.map[i][j] > 0) {  
  207.                     int BrickX = j * Brick.BrickWinght + 80;  
  208.                     int BrickY = i * Brick.BrickHinght + 80;  
  209.                     int BrickWidth = Brick.BrickWinght;  
  210.                     int BrickHight = Brick.BrickHinght;  
  211.                     Rectangle ball = new Rectangle(ballposX, ballposY, 1515);  
  212.                     Rectangle Rect = new Rectangle(BrickX, BrickY, BrickWidth, BrickHight);  
  213.                     if (ball.intersects(Rect)) {  
  214.                         Plsy2(a);  
  215.                         totalbricks--;  
  216.                         score += 5;  
  217.                         Brick.setBrickvaule(i, j, 0);  
  218.                         if (ballposX <= Rect.x || ballposX >= Rect.x + Rect.width) // <= 方塊 =>  
  219.                         {  
  220.                             balldirX = -balldirX;  //反彈  
  221.                         } else {  
  222.                             balldirY = -balldirY;  
  223.                         }  
  224.                     }  
  225.                 }  
  226.             }  
  227.         }  
  228.         this.repaint();//重製  
  229.     }  
  230.   
  231.     @Override  
  232.     public void keyReleased(KeyEvent e) {  
  233.   
  234.     }  
  235.   
  236.     @Override  
  237.     public void keyTyped(KeyEvent e) {  
  238.   
  239.     }  
  240.   
  241.     @Override  
  242.     public void keyPressed(KeyEvent e) {  
  243.         if (timer.isRunning()) {  
  244.             if (e.getKeyCode() == KeyEvent.VK_RIGHT) {  
  245.                 if (playerX > 520) {  
  246.                     playerX = 520;  
  247.                 } else {  
  248.                     MoveRight(1);  
  249.                 }  
  250.   
  251.             } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {  
  252.                 if (playerX < 13) {  
  253.                     playerX = 13;  
  254.                 } else {  
  255.                     MoveLeft(1);  
  256.                 }  
  257.             }  
  258.   
  259.         }  
  260.         if (play == false && e.getKeyCode() == KeyEvent.VK_ENTER) {  
  261.             score = 0;  
  262.             ballposX = 180;  
  263.             ballposY = 395;  
  264.             playerX = 400;  
  265.             play = true;  
  266.             balldirX = -balldirX;  
  267.             balldirY = -balldirY;  
  268.             totalbricks = 21;  
  269.             Brick = new Brick(37);  
  270.             timer.start();  
  271.             repaint();  
  272.         }  
  273.     }  
  274.   
  275.     private void MoveRight(int i) {  
  276.         play = true;  
  277.         if (i == 1) {  
  278.             playerX += 30;  
  279.         } else {  
  280.             playerX2 += 30;  
  281.         }  
  282.     }  
  283.   
  284.     private void MoveLeft(int i) {  
  285.         play = true;  
  286.         if (i == 1) {  
  287.             playerX -= 30;  
  288.         } else {  
  289.             playerX2 -= 30;  
  290.         }  
  291.     }  
  292.   
  293.     public static void Plsy2(File Cli) {  
  294.   
  295.         try {  
  296.             Clip a = AudioSystem.getClip();  
  297.             a.open(AudioSystem.getAudioInputStream(Cli));  
  298.             a.start();  
  299.             Thread.sleep(0);//間隔  
  300.         } catch (Exception ex) {  
  301.             ex.printStackTrace();  
  302.         }  
  303.     }  
  304.   
  305. }  
  306.   
  307. Brick  
  308.   
  309. --------------------------------------------------------------------------------------------------------------------------  
  310.   
  311. /* 
  312.  * To change this license header, choose License Headers in Project Properties. 
  313.  * To change this template file, choose Tools | Templates 
  314.  * and open the template in the editor. 
  315.  */  
  316. package breakerball;  
  317.   
  318. import java.awt.BasicStroke;  
  319. import java.awt.Color;  
  320. import java.awt.Font;  
  321. import java.awt.Graphics;  
  322. import java.awt.Graphics2D;  
  323. import java.awt.Stroke;  
  324.   
  325. /** 
  326.  * 
  327.  * @author user 
  328.  */  
  329. public class Brick {  
  330.     int[][] map  ;  
  331.     int BrickWinght ;   
  332.     int BrickHinght ;  
  333.     Brick(int r , int com ){  
  334.       map =  new  int[r][com] ;   
  335.       for(int i = 0 ; i < map.length ; i++){  
  336.            for(int j = 0 ; j < map[0].length ; j++){  
  337.                map[i][j] = 1 ;  
  338.            }  
  339.       }  
  340.        BrickWinght = 500/com ;//7  
  341.        BrickHinght = 120/r ;//3  
  342.     }  
  343.     public void draw(Graphics2D G){  
  344.         for(int i = 0 ; i < map.length ; i++){  
  345.            for(int j = 0 ; j < map[0].length ; j++){  
  346.               if(map[i][j] > 0){  
  347.                   G.setColor(Color.ORANGE);  
  348.                   G.fillRect(j* BrickWinght+80, i* BrickHinght+80, BrickWinght,  BrickHinght);  
  349.                   G.setColor(Color.black);  
  350.                   G.setStroke(new BasicStroke(3));  
  351.                   G.drawRect(j* BrickWinght+80, i* BrickHinght+80,BrickWinght,  BrickHinght);  
  352.               }   
  353.            }  
  354.       }  
  355.     }  
  356.     public void setBrickvaule(int r , int c  , int vaule){  
  357.         map[r][c] = vaule ;  
  358.     }  
  359. }  
  360.   
  361. play2  
  362.   
  363. --------------------------------------------------------------------------------------------------------------------------  
  364.   
  365. /* 
  366.  * To change this license header, choose License Headers in Project Properties. 
  367.  * To change this template file, choose Tools | Templates 
  368.  * and open the template in the editor. 
  369.  */  
  370. package breakerball;  
  371.   
  372. import static breakerball.Play.Plsy2;  
  373. import java.awt.BasicStroke;  
  374. import java.awt.Color;  
  375. import java.awt.Graphics;  
  376. import java.awt.Graphics2D;  
  377. import java.awt.Rectangle;  
  378. import java.awt.event.ActionEvent;  
  379. import java.awt.event.ActionListener;  
  380. import java.awt.event.KeyEvent;  
  381. import java.awt.event.KeyListener;  
  382. import java.io.File;  
  383.   
  384. import javax.swing.JPanel;  
  385. import javax.swing.Timer;  
  386.   
  387. /** 
  388.  * 
  389.  * @author 1 
  390.  */  
  391. public class play3 extends JPanel implements ActionListener , KeyListener{  
  392. File Clip2 = new File("small_drum1.wav");  
  393. int score = 0 , score2 = 0 ;  
  394. boolean play = false ;  
  395. int playerX = 325 , playerX2 = 365;   
  396. int ballposX = 350 ;  
  397. int ballposY = 350 ;    
  398. int balldirX =  0;  
  399. int balldirY = -1 ;  
  400. Timer timer ;  
  401. public play3(){  
  402.      addKeyListener(this);    
  403.      this.setFocusable(true);//設置可透過鍵盤獲得值  
  404.      this.setFocusTraversalKeysEnabled(true);//設置焦點穿越鍵啟用  
  405.      timer = new Timer(15this);// ActionListener , KeyListener   
  406.      timer.start();  
  407. }  
  408.   public void paint(Graphics G){  
  409.         
  410.       //背景  
  411.        G.setColor(Color.BLACK);  
  412.        G.fillRect(11650700);  
  413.        
  414.        //邊界  
  415.         G.setColor(Color.RED);  
  416.         G.fillRect(00,5695);  
  417.         G.fillRect(64105695);  
  418.         //界線  
  419.         G.setColor(Color.WHITE);  
  420.         G.fillRect(6350,6333);  
  421.         G.drawOval(220248200200);  
  422.         //槳   
  423.         G.setColor(Color.RED);  
  424.         G.fillRect(playerX2, 6010012);  
  425.         G.setColor(Color.GREEN);  
  426.         G.fillRect(playerX, 60010012);  
  427.         //球  
  428.         G.setColor(Color.YELLOW);  
  429.         G.fillOval(ballposX, ballposY, 1515);  
  430.         //分數  
  431.         G.setFont(new java.awt.Font("華康粗黑體(P)"018));  
  432.         G.drawString("分數  "+score2, 10340);  
  433.         G.drawString("分數  "+score, 570375);  
  434.         if(score > 15){    
  435.              G.drawString("玩家一獲勝 "350375);  
  436.              timer.stop();  
  437.         }else if(score2 >15){  
  438.              G.drawString("玩家二獲勝 "350340);  
  439.              timer.stop();  
  440.         }  
  441.               
  442.           
  443.           
  444.         G.dispose();  
  445.   }  
  446.     @Override  
  447.     public void actionPerformed(ActionEvent e) {  
  448.         if(play){  
  449.             Rectangle R = new Rectangle(ballposX, ballposY, 1515) ;  
  450.           if(R.intersects(new Rectangle(playerX, 6001008))){ //交叉  
  451.               balldirY = -balldirY ;  
  452.               Plsy2(Clip2); } //Y 變負 往內  
  453.           if(R.intersects(new Rectangle(playerX2, 601008))){ //交叉  
  454.               balldirY = -balldirY ;  
  455.               Plsy2(Clip2);} //Y 變負 往內  
  456.      
  457.           ballposX +=  balldirX ;  
  458.           ballposY +=  balldirY ;  
  459.             
  460.           if(ballposX < 0)  
  461.               balldirX = -balldirX; //X 變正 往外  
  462.           if(ballposX > 630)  
  463.               balldirX = -balldirX ; //X 變負 往內  
  464.             
  465.           if(ballposY <= 16){  
  466.           ballposX =  -balldirX ;  
  467.           ballposY =  -balldirY ;  
  468.           ballposX = 355 ;  
  469.           ballposY = 75  ;  
  470.           timer.start();  
  471.           play = false ;  
  472.           playerX = 325 ;  
  473.           playerX2 = 355;  
  474.               score ++;    
  475.           }  
  476.           if(ballposY >= 630){  
  477.           ballposX =  -balldirX ;  
  478.           ballposY =  -balldirY ;  
  479.           ballposX = 355 ;  
  480.           ballposY = 585  ;  
  481.           timer.start();  
  482.           play = false ;  
  483.           playerX = 325 ;  
  484.           playerX2 = 355;  
  485.               score2 ++;   
  486.           }   
  487.         }  
  488.         this.repaint();  
  489.     }  
  490.   
  491.   
  492.   
  493.     @Override  
  494.     public void keyPressed(KeyEvent e) {   
  495.       if(timer.isRunning()){    
  496.          if(e.getKeyCode() == KeyEvent.VK_RIGHT){    
  497.             if(playerX >  520)  
  498.               playerX = 520 ;   
  499.             else   
  500.                 MoveRight(1);    
  501.              
  502.         }else if (e.getKeyCode() == KeyEvent.VK_LEFT){    
  503.              if(playerX < 13)  
  504.               playerX = 13 ;   
  505.             else   
  506.                 MoveLeft(1);    
  507.            
  508.     }  
  509.       if(e.getKeyCode() == KeyEvent.VK_D){    
  510.             if(playerX2 >  520)  
  511.               playerX2 = 520 ;   
  512.             else   
  513.                 MoveRight(2);    
  514.              
  515.         }else if (e.getKeyCode() == KeyEvent.VK_A){   
  516.              if(playerX2 < 13)  
  517.               playerX2 = 13 ;   
  518.             else   
  519.                 MoveLeft(2);    
  520.            
  521.     }  
  522.       }  
  523.      if(e.getKeyCode() == KeyEvent.VK_ENTER){//重來  
  524.           Reset();          
  525.           score = 0; score2 = 0;  
  526.      }  
  527.   
  528.   
  529.   
  530. }  
  531.   
  532.     private void MoveRight(int i) {  
  533.         play = true ;   
  534.         if(i == 1)  
  535.         playerX += 30 ;  
  536.         else  
  537.         playerX2 += 30 ;   
  538.     }  
  539.   
  540.     private void MoveLeft(int i) {  
  541.          play = true ;  
  542.          if(i == 1)  
  543.         playerX -= 30 ;  
  544.           else  
  545.         playerX2 -= 30 ;    
  546.     }  
  547.     private void Reset(){  
  548.           ballposX =  -balldirX ;  
  549.           ballposY =  -balldirY ;  
  550.           ballposX = 350 ;  
  551.           ballposY = 350  ;  
  552.           timer.start();  
  553.           play = false ;  
  554.           playerX = 325 ;  
  555.           playerX2 = 355;  
  556.             
  557.     }  
  558.   
  559.     @Override  
  560.     public void keyTyped(KeyEvent e) {  
  561.           
  562.     }  
  563.   
  564.     @Override  
  565.     public void keyReleased(KeyEvent e) {  
  566.          
  567.     }  
  568.   
  569.      
  570.   
  571.       
  572. }  
  573.   
  574. play2  
  575.   
  576. -------------------------------------------------------------------------------------------------------------------------  
  577.   
  578. /* 
  579.  * To change this license header, choose License Headers in Project Properties. 
  580.  * To change this template file, choose Tools | Templates 
  581.  * and open the template in the editor. 
  582.  */  
  583. package breakerball;  
  584.   
  585. import java.io.File;  
  586. import javax.swing.ImageIcon;  
  587.   
  588. /** 
  589.  * 
  590.  * @author user 
  591.  */  
  592. public class play2 extends javax.swing.JFrame {  
  593.   
  594.     /** 
  595.      * Creates new form play2 
  596.      */  
  597.     public play2() {  
  598.         initComponents();    
  599.     }  
  600.   
  601.     /** 
  602.      * This method is called from within the constructor to initialize the form. 
  603.      * WARNING: Do NOT modify this code. The content of this method is always 
  604.      * regenerated by the Form Editor. 
  605.      */  
  606.     @SuppressWarnings("unchecked")  
  607.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                            
  608.     private void initComponents() {  
  609.   
  610.         jTextField1 = new javax.swing.JTextField();  
  611.         jTextField2 = new javax.swing.JTextField();  
  612.         jSlider1 = new javax.swing.JSlider();  
  613.         jButton2 = new javax.swing.JButton();  
  614.         jButton1 = new javax.swing.JButton();  
  615.         jLabel1 = new javax.swing.JLabel();  
  616.   
  617.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  618.         setBounds(new java.awt.Rectangle(45045000));  
  619.         setResizable(false);  
  620.         addWindowListener(new java.awt.event.WindowAdapter() {  
  621.             public void windowClosing(java.awt.event.WindowEvent evt) {  
  622.                 formWindowClosing(evt);  
  623.             }  
  624.             public void windowOpened(java.awt.event.WindowEvent evt) {  
  625.                 formWindowOpened(evt);  
  626.             }  
  627.         });  
  628.         addKeyListener(new java.awt.event.KeyAdapter() {  
  629.             public void keyPressed(java.awt.event.KeyEvent evt) {  
  630.                 formKeyPressed(evt);  
  631.             }  
  632.             public void keyTyped(java.awt.event.KeyEvent evt) {  
  633.                 formKeyTyped(evt);  
  634.             }  
  635.         });  
  636.         getContentPane().setLayout(new java.awt.GridLayout(22));  
  637.   
  638.         jTextField1.setFont(new java.awt.Font("標楷體"036)); // NOI18N  
  639.         jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);  
  640.         jTextField1.setText("列數");  
  641.         getContentPane().add(jTextField1);  
  642.   
  643.         jTextField2.setFont(new java.awt.Font("標楷體"036)); // NOI18N  
  644.         jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);  
  645.         jTextField2.setText("行數");  
  646.         getContentPane().add(jTextField2);  
  647.   
  648.         jSlider1.setFont(new java.awt.Font("Calibri"114)); // NOI18N  
  649.         jSlider1.setMajorTickSpacing(3);  
  650.         jSlider1.setMaximum(8);  
  651.         jSlider1.setMinimum(1);  
  652.         jSlider1.setOrientation(javax.swing.JSlider.VERTICAL);  
  653.         jSlider1.setPaintLabels(true);  
  654.         jSlider1.setPaintTicks(true);  
  655.         jSlider1.setValue(2);  
  656.         getContentPane().add(jSlider1);  
  657.   
  658.         jButton2.setFont(new java.awt.Font("Arial"036)); // NOI18N  
  659.         jButton2.setText("2P Start");  
  660.         jButton2.addKeyListener(new java.awt.event.KeyAdapter() {  
  661.             public void keyPressed(java.awt.event.KeyEvent evt) {  
  662.                 jButton2KeyPressed(evt);  
  663.             }  
  664.         });  
  665.         getContentPane().add(jButton2);  
  666.   
  667.         jButton1.setFont(new java.awt.Font("Aharoni"036)); // NOI18N  
  668.         jButton1.setText("Start");  
  669.         jButton1.addActionListener(new java.awt.event.ActionListener() {  
  670.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  671.                 jButton1ActionPerformed(evt);  
  672.             }  
  673.         });  
  674.         jButton1.addKeyListener(new java.awt.event.KeyAdapter() {  
  675.             public void keyPressed(java.awt.event.KeyEvent evt) {  
  676.                 jButton1KeyPressed(evt);  
  677.             }  
  678.         });  
  679.         getContentPane().add(jButton1);  
  680.   
  681.         jLabel1.setFont(new java.awt.Font("Arial Black"048)); // NOI18N  
  682.         jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);  
  683.         jLabel1.setText("XIN");  
  684.         getContentPane().add(jLabel1);  
  685.   
  686.         pack();  
  687.     }// </editor-fold>                          
  688.   
  689.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  690.       
  691.     }                                          
  692.   
  693.     private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {                                      
  694.         // TODO add your handling code here:  
  695.     }                                     
  696.   
  697.     private void formKeyTyped(java.awt.event.KeyEvent evt) {                                
  698.            
  699.     }                               
  700.   
  701.     private void formWindowClosing(java.awt.event.WindowEvent evt) {                                     
  702.           
  703.     }                                    
  704.   
  705.     private void formWindowOpened(java.awt.event.WindowEvent evt) {                                    
  706.         // TODO add your handling code here:  
  707.     }                                   
  708.   
  709.     private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {                                      
  710.         
  711.     }                                     
  712.   
  713.     private void formKeyPressed(java.awt.event.KeyEvent evt) {                                  
  714.            
  715.     }                                 
  716.   
  717.     /** 
  718.      * @param args the command line arguments 
  719.      */  
  720.     public static void main(String args[]) {  
  721.         /* Set the Nimbus look and feel */  
  722.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">  
  723.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
  724.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html  
  725.          */  
  726.         try {  
  727.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {  
  728.                 if ("Nimbus".equals(info.getName())) {  
  729.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());  
  730.                     break;  
  731.                 }  
  732.             }  
  733.         } catch (ClassNotFoundException ex) {  
  734.             java.util.logging.Logger.getLogger(play2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  735.         } catch (InstantiationException ex) {  
  736.             java.util.logging.Logger.getLogger(play2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  737.         } catch (IllegalAccessException ex) {  
  738.             java.util.logging.Logger.getLogger(play2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  739.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {  
  740.             java.util.logging.Logger.getLogger(play2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  741.         }  
  742.         //</editor-fold>  
  743.   
  744.         /* Create and display the form */  
  745.         java.awt.EventQueue.invokeLater(new Runnable() {  
  746.             public void run() {  
  747.                 new play2().setVisible(true);  
  748.             }  
  749.         });  
  750.     }  
  751.   
  752.     // Variables declaration - do not modify                       
  753.     public javax.swing.JButton jButton1;  
  754.     public javax.swing.JButton jButton2;  
  755.     private javax.swing.JLabel jLabel1;  
  756.     public javax.swing.JSlider jSlider1;  
  757.     public javax.swing.JTextField jTextField1;  
  758.     public javax.swing.JTextField jTextField2;  
  759.     // End of variables declaration                     
  760. }  
  761.   
  762. play3  
  763.   
  764. --------------------------------------------------------------------------------------------------------------------------  
  765.   
  766. /* 
  767.  * To change this license header, choose License Headers in Project Properties. 
  768.  * To change this template file, choose Tools | Templates 
  769.  * and open the template in the editor. 
  770.  */  
  771. package breakerball;  
  772.   
  773. import static breakerball.Play.Plsy2;  
  774. import java.awt.BasicStroke;  
  775. import java.awt.Color;  
  776. import java.awt.Graphics;  
  777. import java.awt.Graphics2D;  
  778. import java.awt.Rectangle;  
  779. import java.awt.event.ActionEvent;  
  780. import java.awt.event.ActionListener;  
  781. import java.awt.event.KeyEvent;  
  782. import java.awt.event.KeyListener;  
  783. import java.io.File;  
  784.   
  785. import javax.swing.JPanel;  
  786. import javax.swing.Timer;  
  787.   
  788. /** 
  789.  * 
  790.  * @author 1 
  791.  */  
  792. public class play3 extends JPanel implements ActionListener , KeyListener{  
  793. File Clip2 = new File("small_drum1.wav");  
  794. int score = 0 , score2 = 0 ;  
  795. boolean play = false ;  
  796. int playerX = 325 , playerX2 = 365;   
  797. int ballposX = 350 ;  
  798. int ballposY = 350 ;    
  799. int balldirX =  0;  
  800. int balldirY = -1 ;  
  801. Timer timer ;  
  802. public play3(){  
  803.      addKeyListener(this);    
  804.      this.setFocusable(true);//設置可透過鍵盤獲得值  
  805.      this.setFocusTraversalKeysEnabled(true);//設置焦點穿越鍵啟用  
  806.      timer = new Timer(15this);// ActionListener , KeyListener   
  807.      timer.start();  
  808. }  
  809.   public void paint(Graphics G){  
  810.         
  811.       //背景  
  812.        G.setColor(Color.BLACK);  
  813.        G.fillRect(11650700);  
  814.        
  815.        //邊界  
  816.         G.setColor(Color.RED);  
  817.         G.fillRect(00,5695);  
  818.         G.fillRect(64105695);  
  819.         //界線  
  820.         G.setColor(Color.WHITE);  
  821.         G.fillRect(6350,6333);  
  822.         G.drawOval(220248200200);  
  823.         //槳   
  824.         G.setColor(Color.RED);  
  825.         G.fillRect(playerX2, 6010012);  
  826.         G.setColor(Color.GREEN);  
  827.         G.fillRect(playerX, 60010012);  
  828.         //球  
  829.         G.setColor(Color.YELLOW);  
  830.         G.fillOval(ballposX, ballposY, 1515);  
  831.         //分數  
  832.         G.setFont(new java.awt.Font("華康粗黑體(P)"018));  
  833.         G.drawString("分數  "+score2, 10340);  
  834.         G.drawString("分數  "+score, 570375);  
  835.         if(score > 15){    
  836.              G.drawString("玩家一獲勝 "350375);  
  837.              timer.stop();  
  838.         }else if(score2 >15){  
  839.              G.drawString("玩家二獲勝 "350340);  
  840.              timer.stop();  
  841.         }  
  842.               
  843.           
  844.           
  845.         G.dispose();  
  846.   }  
  847.     @Override  
  848.     public void actionPerformed(ActionEvent e) {  
  849.         if(play){  
  850.             Rectangle R = new Rectangle(ballposX, ballposY, 1515) ;  
  851.           if(R.intersects(new Rectangle(playerX, 6001008))){ //交叉  
  852.               balldirY = -balldirY ;  
  853.               Plsy2(Clip2); } //Y 變負 往內  
  854.           if(R.intersects(new Rectangle(playerX2, 601008))){ //交叉  
  855.               balldirY = -balldirY ;  
  856.               Plsy2(Clip2);} //Y 變負 往內  
  857.      
  858.           ballposX +=  balldirX ;  
  859.           ballposY +=  balldirY ;  
  860.             
  861.           if(ballposX < 0)  
  862.               balldirX = -balldirX; //X 變正 往外  
  863.           if(ballposX > 630)  
  864.               balldirX = -balldirX ; //X 變負 往內  
  865.             
  866.           if(ballposY <= 16){  
  867.           ballposX =  -balldirX ;  
  868.           ballposY =  -balldirY ;  
  869.           ballposX = 355 ;  
  870.           ballposY = 75  ;  
  871.           timer.start();  
  872.           play = false ;  
  873.           playerX = 325 ;  
  874.           playerX2 = 355;  
  875.               score ++;    
  876.           }  
  877.           if(ballposY >= 630){  
  878.           ballposX =  -balldirX ;  
  879.           ballposY =  -balldirY ;  
  880.           ballposX = 355 ;  
  881.           ballposY = 585  ;  
  882.           timer.start();  
  883.           play = false ;  
  884.           playerX = 325 ;  
  885.           playerX2 = 355;  
  886.               score2 ++;   
  887.           }   
  888.         }  
  889.         this.repaint();  
  890.     }  
  891.   
  892.   
  893.   
  894.     @Override  
  895.     public void keyPressed(KeyEvent e) {   
  896.       if(timer.isRunning()){    
  897.          if(e.getKeyCode() == KeyEvent.VK_RIGHT){    
  898.             if(playerX >  520)  
  899.               playerX = 520 ;   
  900.             else   
  901.                 MoveRight(1);    
  902.              
  903.         }else if (e.getKeyCode() == KeyEvent.VK_LEFT){    
  904.              if(playerX < 13)  
  905.               playerX = 13 ;   
  906.             else   
  907.                 MoveLeft(1);    
  908.            
  909.     }  
  910.       if(e.getKeyCode() == KeyEvent.VK_D){    
  911.             if(playerX2 >  520)  
  912.               playerX2 = 520 ;   
  913.             else   
  914.                 MoveRight(2);    
  915.              
  916.         }else if (e.getKeyCode() == KeyEvent.VK_A){   
  917.              if(playerX2 < 13)  
  918.               playerX2 = 13 ;   
  919.             else   
  920.                 MoveLeft(2);    
  921.            
  922.     }  
  923.       }  
  924.      if(e.getKeyCode() == KeyEvent.VK_ENTER){//重來  
  925.           Reset();          
  926.           score = 0; score2 = 0;  
  927.      }  
  928.   
  929.   
  930.   
  931. }  
  932.   
  933.     private void MoveRight(int i) {  
  934.         play = true ;   
  935.         if(i == 1)  
  936.         playerX += 30 ;  
  937.         else  
  938.         playerX2 += 30 ;   
  939.     }  
  940.   
  941.     private void MoveLeft(int i) {  
  942.          play = true ;  
  943.          if(i == 1)  
  944.         playerX -= 30 ;  
  945.           else  
  946.         playerX2 -= 30 ;    
  947.     }  
  948.     private void Reset(){  
  949.           ballposX =  -balldirX ;  
  950.           ballposY =  -balldirY ;  
  951.           ballposX = 350 ;  
  952.           ballposY = 350  ;  
  953.           timer.start();  
  954.           play = false ;  
  955.           playerX = 325 ;  
  956.           playerX2 = 355;  
  957.             
  958.     }  
  959.   
  960.     @Override  
  961.     public void keyTyped(KeyEvent e) {  
  962.           
  963.     }  
  964.   
  965.     @Override  
  966.     public void keyReleased(KeyEvent e) {  
  967.          
  968.     }  
  969.   
  970.      
  971.   
  972.       
  973. }  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。