JAVAヘビ食いしん坊ケース

6552 ワード

package snake;

import java.awt.Color;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.LineBorder;

/**
 *    
 */
public class SnakeFrame extends JFrame implements Runnable{
	private static final long serialVersionUID = 1L;
	private JButton food;//    
	private Random rd = new Random();//    
	private LinkedList snake = new LinkedList<>();//     
	private boolean game = false;//          
	private int code;
	public SnakeFrame() {
		//    
		setTitle("   ");
		//    
		setBounds(300, 50, 800, 600);
		//              
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//        
		setResizable(false);
		//      
		setLayout(null);
		//      
		//  add()          frame   ,                  ,
		//                       ,           getContentPane()           
		getContentPane().setBackground(Color.BLACK);
		//         ,              ,           。              ,            
		initSnake();
		//    
		setVisible(true);
		//          ,             /           
		KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); 
		//                                                  
		manager.addKeyEventPostProcessor(new KeyEventPostProcessor() {
			public boolean postProcessKeyEvent(KeyEvent e) {
				//            
				//enter  10
				//  38   40   37    39
				int code = e.getKeyCode();
				String code2 = e.paramString() ;
				//    enter          
				if (code==10&&code2.contains("KEY_PRESSED")) {
					//            
					game=!game;
				}
				//    
				if (game&&(code==37||code==38||code==39||code==40)&&code2.contains("KEY_PRESSED")) {
					SnakeFrame.this.code = code;
				}
				return true;
			}
		});
	}
	public static void main(String[] args) {
		SnakeFrame snake = new SnakeFrame();
		Thread t = new Thread(snake);
		t.start();
	}

	/**
	 *     
	 */
	public void run() {
		while(true) {
			//            
			createFood();
			//                   
			if (game) {
				snakeRun();
			}
			//    
			collision();
			repaint();
			try {
				Thread.sleep(200);
			} catch (Exception e) {
			}
		}
	}
	/**
	 *     
	 */
	private void collision() {
		/**
		 *        
		 *         ,                  ,                          
		 * 1.       ,       
		 * 2.       
		 * 3.        ,          
		 * 4.     ,     null,    ,            
		 */
		//1.      
		JButton head = snake.getFirst();
		//2.        
		//               ,      
		int x = head.getX();
		int y = head.getY();
		{
			//3.      
			int x2 = food.getX();
			int y2 = food.getY();
			//4.        
			/**
			 *         
			 * 
			 *           x                
			 * 
			 *                          ,          
			 */
			//    
			if ((x>=x2&&x<=x2+50&&y>=y2&&y<=y2+50)//              
					||(x>=x2&&x<=x2+50&&y+50>=y2&&y+50<=y2+50)//               
					||(x+50>=x2&&x+50<=x2+50&&y>=y2&&y<=y2+50)//               
					||(x+50>=x2&&x+50<=x2+50&&y+50>=y2&&y+50<=y2+50)//               
					) {
				//       
				remove(food);
				//           
				food = null;
				//          
				JButton btn = new JButton();
				btn.setSize(50,50);
				btn.setBackground(Color.green);
				btn.setBorder(new LineBorder(Color.PINK,1));
				//           
				snake.addLast(btn);
				//       
				add(btn);
			}
		}
		/**
		 *        
		 * 
		 *      ,                     
		 */
		/**
		 *       
		 */
	}
	/**
	 *       
	 * 
	 * 
	 * 
	 */
	private void snakeRun() {
		//    
		int x = 0;//               
		int y = 0;//               
		JButton head = snake.getFirst();
		x = head.getX();
		y = head.getY();
		if (code==38) {
			head.setLocation(x,y-50);
		}else if (code==40) {
			head.setLocation(x,y+50);
		}else if (code==37) {
			head.setLocation(x-50,y);
		}else {
			head.setLocation(x+50,y);
		}
		//   
		for (int i = 1; i < snake.size(); i++) {
			JButton btn = snake.get(i);
			int x2 = btn.getX();
			int y2 = btn.getY();
			//          
			btn.setLocation(x,y);
			//          
			x = x2;
			y = y2;
		}
		validate();
		revalidate();
	}
	/**
	 *        
	 * 1.                               ,                
	 * ,             ,   ,        
	 * 2.  (         ,         (JButton        ,   )  ,             )            
	 * ,             。                ,                 (       )。          
	 *               。
	 * 3.             ,                       linkedList    ,           
	 */
	private void initSnake() {
		JButton btn_1 = new JButton();
		btn_1.setBounds(0, 0, 50, 50);
		btn_1.setBackground(Color.green);
		//             1   
		btn_1.setBorder(new LineBorder(Color.PINK,1));
		add(btn_1);
		
		
		JButton btn_2 = new JButton();
		btn_2.setBounds(50, 0, 50, 50);
		btn_2.setBackground(Color.green);
		btn_2.setBorder(new LineBorder(Color.PINK,1));
		add(btn_2);
		
		
		JButton btn_3 = new JButton();
		btn_3.setBounds(100, 0, 50, 50);
		btn_3.setBackground(Color.green);
		btn_3.setBorder(new LineBorder(Color.PINK,1));
		add(btn_3);
		
		//        
		snake.addLast(btn_2);
		snake.addLast(btn_1);
		//   3      ,    
		snake.addFirst(btn_3);
	}
	
	/**
	 *          
	 * 1.             。                   x:0-800  (           )  y:0-600
	 * 2.                       (            )
	 * 3.      ,                        
	 * 	a.        ,            null
	 * 		i. null             
	 * 		ii.  null         
	 * 	b.                 ,       (null) ,          。            
	 */
	private void createFood() {
		//                
		if (food==null) {
			//      
			food = new JButton();
			//    
			//                      [0,800)
			int x =rd.nextInt(800);
			int y =rd.nextInt(600);
			food.setBounds(x, y, 50, 50);
			//      
			food.setBackground(Color.RED);
			//    
			food.setBorder(null);
			//           
			add(food);
		}
	}
	/**
	 *       ,       ,                 (      )
	 * @author Administrator
	 *1.     enter                
	 *2.       ,        
	 *	a.
	 *	b.
	 */
	class MyKeyListener implements KeyListener{
		public void keyTyped(KeyEvent e) {
			System.err.println(1);
		}
		public void keyPressed(KeyEvent e) {
			System.err.println(2);
		}
		public void keyReleased(KeyEvent e) {
			System.err.println(3);
		}
		
	}
}

比较的简単....