这个贪吃蛇有很多功能没有实现,比如说穿墙(本来可以实现,但是穿墙后,就会出现坐标混乱,吃不到食物了),还有碰到自己的身体死亡的情况也没有实现,现在我知道如何判断是否碰到身体,但是,我不知道,如何处理碰到身体后的情况。这些问题我想过段时间去解决。
1 import java.awt.Color; 2 import java.awt.event.KeyAdapter; 3 import java.awt.event.KeyEvent; 4 import java.util.LinkedList; 5 import java.util.Random; 6 7 import javax.swing.JFrame; 8 import javax.swing.JPanel; 9 10 public class Snake extends JFrame{ 11 static final int SIZE = 15; 12 private JPanel winPanel; 13 private JPanel food; 14 private LinkedListsnakeBody = new LinkedList (); 15 private MoveThread mt ; 16 17 public JPanel getFood() { 18 return food; 19 } 20 21 public void setWinPanel(JPanel winPanel) { 22 this. winPanel = winPanel; 23 } 24 25 public JPanel getWinPanel() { 26 return winPanel; 27 } 28 public LinkedList getSnakeBody() { 29 return snakeBody; 30 } 31 /** 32 * 初始化食物 33 */ 34 public void initFood(){ 35 food = new JPanel(); 36 food.setBackground(Color. red); 37 Random r = new Random(); 38 int x = r.nextInt( SIZE)*16; 39 int y = r.nextInt( SIZE)*16; 40 for (JPanel p : snakeBody) { 41 if(p.getX()== x && p.getY() == y){ 42 x = r.nextInt( SIZE)*16; 43 y = r.nextInt( SIZE)*16; 44 } 45 } 46 food.setBounds(x, y, SIZE, SIZE); 47 winPanel.add( food); 48 49 } 50 /** 51 * 初始化snake的身体 52 */ 53 public void initSnakePanel(){ 54 JPanel body1 = new JPanel(); 55 body1.setBounds(0, 0, SIZE, SIZE); 56 body1.setBackground(Color. yellow); 57 snakeBody.add(body1); 58 for ( int i = 0; i < 4; i++) { 59 JPanel body = new JPanel(); 60 JPanel last = snakeBody.getLast(); 61 body.setBounds(last.getX()+ SIZE+1, 0, SIZE, SIZE); 62 body.setBackground(Color. yellow); 63 snakeBody.add(body); 64 winPanel.add(body); 65 } 66 winPanel.add(body1); 67 } 68 /** 69 * 键盘监听 70 */ 71 private void myActiongLisenler(){ 72 this.addKeyListener( new KeyAdapter() { 73 @Override 74 public void keyPressed(KeyEvent e) { 75 int code = e.getKeyCode(); 76 if(code == 39 && mt == null){ 77 mt = new MoveThread(Snake. this); 78 mt.start(); 79 } 80 int nowWay = mt.getWay(); 81 if((nowWay + code) % 2 != 0) 82 mt.setWay(code); 83 } 84 }); 85 } 86 /** 87 * 初始化背景面板 88 */ 89 public void initWinPanel(){ 90 winPanel = new JPanel(); 91 winPanel.setLayout( null); 92 winPanel.setBackground(Color. black); 93 this.add( winPanel); 94 } 95 public Snake() { 96 super( "贪吃蛇"); 97 initWinPanel(); 98 initSnakePanel(); 99 initFood();100 myActiongLisenler();101 this.setSize(500, 500);102 this.setLocationRelativeTo( null);103 this.setVisible( true);104 this.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);105 }106 }
1 import java.awt.Color; 2 import java.util.LinkedList; 3 4 import javax.swing.JPanel; 5 6 public class MoveThread extends Thread{ 7 private Snake snake; 8 private int way; 9 10 public int getWay() {11 return way;12 }13 14 public void setWay( int way) {15 this. way = way;16 }17 18 public MoveThread(Snake snake) {19 this. snake = snake;20 }21 22 @Override23 public void run() {24 //循环的移动,循环一次走一步,并且延时25 while( true){26 move();27 try {28 Thread. sleep(100);29 } catch (InterruptedException e) {30 e.printStackTrace();31 }32 }33 }34 private void move(){35 LinkedListsnakeBody = snake.getSnakeBody();36 JPanel food = snake.getFood();37 JPanel winPanel = snake.getWinPanel();38 //获取到旧头39 JPanel oldHead = snakeBody.getLast();40 //获取旧头的坐标41 int x = oldHead.getX();42 int y = oldHead.getY();43 switch( way){44 case 37:45 x = x - ( snake. SIZE + 1);46 // if(x <= -15){47 // x = 515;48 // }49 break;50 case 38:51 y = y - ( snake. SIZE + 1);52 // if(y <= 0){53 // y = 515;54 // }55 //way = 40;56 break;57 case 39:58 x = x + snake. SIZE + 1;59 // if(x >= 515){60 // x = 0;61 // }62 break;63 case 40:64 y = y + snake. SIZE + 1;65 // if(y >= 515){66 // y = 0;67 // }68 //way = 38;69 break;70 }71 //创建新头72 JPanel newHead = new JPanel();73 newHead.setBounds(x, y, snake. SIZE, snake. SIZE);74 newHead.setBackground(Color. yellow);75 if(food.getX() == x && food.getY() == y){76 snakeBody.add(newHead);77 winPanel.add(newHead);78 //吃到食物后删掉旧的食物79 winPanel.remove(food);80 //调用初始化食物方法,产生新的食物81 snake.initFood();82 } else{83 snakeBody.add(newHead);84 winPanel.add(newHead);85 //去尾86 winPanel.remove(snakeBody.getFirst());87 snakeBody.removeFirst();88 }89 snake.repaint();90 }91 }
1 public class SnakeTest {2 3 /**4 * @param args5 */6 public static void main(String[] args) {7 Snake s = new Snake();8 }9 }