2017年9月16日 星期六

貪食蛇


  1. package snake;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.Color;  
  5. import java.awt.Font;  
  6. import java.awt.Graphics;  
  7. import java.awt.Graphics2D;  
  8. import java.awt.Rectangle;  
  9. import java.awt.event.ActionEvent;  
  10. import java.awt.event.ActionListener;  
  11. import java.awt.event.KeyEvent;  
  12. import java.awt.event.KeyListener;  
  13. import java.io.File;  
  14. import java.util.logging.Level;  
  15. import java.util.logging.Logger;  
  16. import javax.sound.sampled.AudioSystem;  
  17. import javax.sound.sampled.Clip;  
  18. import javax.swing.ImageIcon;  
  19. import javax.swing.JPanel;  
  20. import javax.swing.Timer;  
  21.   
  22. public class PanelSnake extends JPanel implements ActionListener , KeyListener{  
  23.    ImageIcon ico , snake;  
  24.    int lengthofsnake = 3;  
  25.    int moves = 0;  
  26.    int score = 0 ;  
  27.    int[] snakexlungth = new int[750];  
  28.    int[] snakeylungth = new int[750];  
  29.    int[] emtxpos = {40,70,100,130,160,190,220,250,280,310,340,370,400,430,460,490,520};  
  30.    int[] emtypos = {130,160,190,220,250,280,310,340,370,400,430,460,490,520,550,580,610};  
  31.    int emtx = (int) (Math.random()*17);  
  32.    int emty = (int) (Math.random()*17);  
  33.    boolean right = false ;  
  34.    boolean left = false ;  
  35.    boolean up = false ;  
  36.    boolean down = false ;  
  37.    ImageIcon rightmouth;  
  38.    ImageIcon leftmouth;  
  39.    ImageIcon upmouth;  
  40.    ImageIcon downmouth;  
  41.    Timer timer ;  
  42.    int delay = 350 ;  
  43.       
  44.     public  PanelSnake(){  
  45.        addKeyListener(this);  
  46.        this.setFocusable(true);//設置可透過鍵盤獲得值  
  47.        this.setFocusTraversalKeysEnabled(true);//設置焦點穿越鍵啟用  
  48.        timer = new Timer(delay, this);  
  49.        timer.start();  
  50.     }  
  51.      public void paint(Graphics G){  
  52.          if(moves == 0 ){  
  53.              snakexlungth[2] = 10;  
  54.              snakexlungth[1] = 40;  
  55.              snakexlungth[0] = 70;  
  56.              snakeylungth[2] = 100;  
  57.              snakeylungth[1] = 100;  
  58.              snakeylungth[0] = 100;  
  59.          }  
  60.          //背景  
  61.          G.setColor(Color.PINK);  
  62.          G.fillRect(00600700);  
  63.          ico = new  ImageIcon("SANKE.png");  
  64.          ico.paintIcon(this, G, 12);  
  65.          //邊界  
  66.          G.setColor(Color.orange);  
  67.          draw((Graphics2D) G);  
  68.          //地板  
  69.          G.setColor(Color.GREEN);  
  70.          G.fillRect(1,86581569);  
  71.          //  
  72.          G.setColor(Color.BLACK);  
  73.          G.setFont(new Font("標楷體"316));  
  74.          G.drawString("分數 :"+score, 500600);  
  75.          G.drawString("步數 :"+moves, 500620);  
  76.         //蛇頭  
  77.         if(moves == 0){  
  78.         rightmouth = new ImageIcon("right.png");  
  79.         rightmouth.paintIcon(this, G, snakexlungth[0] ,  snakeylungth[0]);      
  80.          }  
  81.         
  82.          //方向  
  83.          for(int i = 0 ; i < lengthofsnake ; i++){  
  84.              //頭  
  85.              if(i == 0 && right){  
  86.                   rightmouth = new ImageIcon("right.png");  
  87.                   rightmouth.paintIcon(this, G, snakexlungth[0] ,  snakeylungth[0]);  
  88.              }  
  89.              if(i == 0 && left){  
  90.                   leftmouth = new ImageIcon("left.png");  
  91.                   leftmouth.paintIcon(this, G, snakexlungth[0] ,  snakeylungth[0]);  
  92.              }      
  93.              if(i == 0 && down){  
  94.                   downmouth = new ImageIcon("down.png");  
  95.                   downmouth.paintIcon(this, G, snakexlungth[0] ,  snakeylungth[0]);  
  96.              }  
  97.              if(i == 0 && up){  
  98.                   upmouth = new ImageIcon("up.png");  
  99.                   upmouth.paintIcon(this, G, snakexlungth[0] ,  snakeylungth[0]);  
  100.              }  
  101.              //身體  
  102.              if(i != 0 ){  
  103.                  snake = new ImageIcon("1.png");  
  104.                  snake.paintIcon(this, G, snakexlungth[i] ,  snakeylungth[i]);  
  105.              }  
  106.          }  
  107.         if(!timer.isRunning()) {  
  108.             G.setColor(Color.BLACK);  
  109.             G.setFont(new Font("標楷體"348));  
  110.             G.drawString("最高紀錄 :" +score, 300400);  
  111.         }  
  112.         if(new Rectangle(emtxpos[emtx], emtypos[emty], 1515).intersects(new Rectangle(snakexlungth[0], snakeylungth[0], 1515))){  
  113.             lengthofsnake++;  
  114.             score++;  
  115.             delay-= 4;  
  116.             PlayMusic(new File("small_drum1.wav"));  
  117.             timer.setDelay(delay);  
  118.             emtx = (int) (Math.random()*17);  
  119.             emty = (int) (Math.random()*17);  
  120.            /* if(lengthofsnake == 25){ 
  121.                 G.setColor(Color.BLACK); 
  122.                 G.setFont(new Font("標楷體", 3, 48)); 
  123.                 G.drawString("YOU WIN", 310, 400); 
  124.                 timer.stop() ; 
  125.             }*/  
  126.         }  
  127.         G.setColor(Color.red);  
  128.         G.fillOval(emtxpos[emtx], emtypos[emty], 1515);  
  129.           
  130.         G.dispose();  
  131.      }  
  132.     @Override  
  133.     public void actionPerformed(ActionEvent e) {  
  134.          
  135.         if(right){       
  136.                 moves ++;  
  137.                 for(int i =  lengthofsnake-1 ; i >=0 ; i--){  
  138.                     snakeylungth[i+1] = snakeylungth[i];  
  139.                 }  
  140.                 for(int i =  lengthofsnake ; i >=0 ; i--){  
  141.                     if(i == 0){  
  142.                         snakexlungth[i] =  snakexlungth[i]+30;  
  143.                     }else{  
  144.                         snakexlungth[i] =  snakexlungth[i-1];  
  145.                     }  
  146.                     if( snakexlungth[i] > 560)  
  147.                        timer.stop() ;  
  148.                 }  
  149.              
  150.         }  
  151.         if(left){  
  152.             moves ++;  
  153.             for(int i =  lengthofsnake-1 ; i >=0 ; i--){  
  154.                 snakeylungth[i+1] = snakeylungth[i];  
  155.             }  
  156.             for(int i =  lengthofsnake ; i >=0 ; i--){  
  157.                if(i == 0){  
  158.                     snakexlungth[i] =  snakexlungth[i]-30;  
  159.                }else{  
  160.                     snakexlungth[i] =  snakexlungth[i-1];  
  161.                }  
  162.                if( snakexlungth[i] < 10 )  
  163.                     timer.stop() ;  
  164.             }  
  165.         }  
  166.         if(up){  
  167.             moves++;  
  168.             for(int i =  lengthofsnake-1 ; i >=0 ; i--){  
  169.                 snakexlungth[i+1] = snakexlungth[i];  
  170.             }  
  171.             for(int i =  lengthofsnake ; i >=0 ; i--){  
  172.                if(i == 0){  
  173.                     snakeylungth[i] =  snakeylungth[i]-30;  
  174.                }else{  
  175.                     snakeylungth[i] =  snakeylungth[i-1];  
  176.                }  
  177.                if( snakeylungth[i] < 100 )  
  178.                    timer.stop() ;  
  179.             }  
  180.         }  
  181.          if(down){  
  182.           moves++;  
  183.            for(int i =  lengthofsnake-1 ; i >=0 ; i--){  
  184.                 snakexlungth[i+1] = snakexlungth[i];  
  185.             }  
  186.             for(int i =  lengthofsnake ; i >=0 ; i--){  
  187.                if(i == 0){  
  188.                     snakeylungth[i] =  snakeylungth[i]+30;  
  189.                }else{  
  190.                     snakeylungth[i] =  snakeylungth[i-1];  
  191.                }  
  192.                if( snakeylungth[i] > 620 )  
  193.                     timer.stop() ;  
  194.             }  
  195.         }  
  196.           
  197.           
  198.           
  199.           
  200.         repaint();  
  201.     }  
  202.   
  203.     @Override  
  204.     public void keyTyped(KeyEvent e) {  
  205.         
  206.     }  
  207.   
  208.     @Override  
  209.     public void keyPressed(KeyEvent e) {  
  210.        if(e.getKeyCode() == KeyEvent.VK_RIGHT){  
  211.            right = true ;  
  212.            if(!left){  
  213.                right = true ;             
  214.            }else{  
  215.                right = false ;  
  216.            }  
  217.            up = false ;  
  218.            down = false ;  
  219.        }  
  220.        if(e.getKeyCode() == KeyEvent.VK_LEFT){  
  221.            left = true ;  
  222.            if(!right){  
  223.                left = true ;             
  224.            }else{  
  225.                left = false ;  
  226.            }  
  227.            up = false ;  
  228.            down = false ;  
  229.        }  
  230.        if(e.getKeyCode() == KeyEvent.VK_UP){  
  231.            up = true ;  
  232.            if(!down){  
  233.                up = true ;             
  234.            }else{  
  235.                up = false ;  
  236.            }  
  237.            right = false ;  
  238.            left = false ;  
  239.        }  
  240.        if(e.getKeyCode() == KeyEvent.VK_DOWN){  
  241.            down = true ;  
  242.            if(!up){  
  243.                down = true ;             
  244.            }else{  
  245.                down = false ;  
  246.            }  
  247.            right = false ;  
  248.            left = false ;  
  249.        }  
  250.        if(e.getKeyCode() == KeyEvent.VK_ENTER && !timer.isRunning()){  
  251.            moves = 0;  
  252.            score = 0 ;  
  253.            lengthofsnake = 3 ;  
  254.            up = false ;left = false ;  
  255.            down = false ;right= false ;  
  256.            timer.start();  
  257.        }  
  258.   
  259.     }  
  260.   
  261.     @Override  
  262.     public void keyReleased(KeyEvent e) {  
  263.           
  264.     }  
  265.       
  266.     public static void  PlayMusic(File Cli) {  
  267.   
  268.         try {  
  269.             Clip a = AudioSystem.getClip();  
  270.             a.open(AudioSystem.getAudioInputStream(Cli));  
  271.             a.start();  
  272.             Thread.sleep(0);//間隔  
  273.         } catch (Exception ex) {  
  274.             ex.printStackTrace();  
  275.         }  
  276.     }  
  277.   
  278.     private void draw(Graphics2D G) {      
  279.         G.setStroke(new BasicStroke(10));  
  280.         G.drawRect(085582,570);  
  281.         G.drawRect(0058275);  
  282.     }  
  283. }  

沒有留言:

張貼留言

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