![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.myproj.snake; 2 3 public class Node { 4 private int i,j; 5 public Node(){} 6 public Node(int i, int j) { 7 super(); 8 this.i = i; 9 this.j = j; 10 } 11 12 public int getI() { 13 return i; 14 } 15 16 public void setI(int i) { 17 this.i = i; 18 } 19 20 public int getJ() { 21 return j; 22 } 23 24 public void setJ(int j) { 25 this.j = j; 26 } 27 @Override 28 public int hashCode() { 29 final int prime = 31; 30 int result = 1; 31 result = prime * result + i; 32 result = prime * result + j; 33 return result; 34 } 35 @Override 36 public boolean equals(Object obj) { 37 if (this == obj) 38 return true; 39 if (obj == null) 40 return false; 41 if (getClass() != obj.getClass()) 42 return false; 43 Node other = (Node) obj; 44 if (i != other.i) 45 return false; 46 if (j != other.j) 47 return false; 48 return true; 49 } 50 51 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.myproj.snake; 2 3 import java.util.LinkedList; 4 5 public class Snake { 6 private LinkedList<Node> nodes=new LinkedList<Node>(); 7 public static final int UP=-5; 8 public static final int DOWN=5; 9 public static final int LEFT=-1; 10 public static final int RIGHT=1; 11 private int dir;//当前方向; 12 public Snake(){ 13 nodes.add(new Node(3,9)); 14 nodes.add(new Node(4,9)); 15 nodes.add(new Node(5,9)); 16 nodes.add(new Node(5,10)); 17 nodes.add(new Node(5,11)); 18 nodes.add(new Node(6,11)); 19 nodes.add(new Node(7,11)); 20 this.dir=RIGHT; 21 } 22 public boolean contains(int i,int j){ 23 return nodes.contains(new Node(i,j)); 24 } 25 public void move(){ 26 nodes.removeLast(); 27 Node head=nodes.getFirst(); 28 int i=head.getI()+dir/5; 29 int j=head.getJ()+dir%5; 30 nodes.addFirst(new Node(i,j)); 31 } 32 public void move(int dir){ 33 if(this.dir+dir==0){ 34 System.out.println("不能逆向行驶!"); 35 System.out.println("游戏终止!"); 36 System.exit(0); 37 } 38 this.dir=dir; 39 move(); 40 } 41 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.myproj.snake; 2 3 public class SnakePan { 4 private Snake snake=new Snake(); 5 public Snake getSnake() { 6 return snake; 7 } 8 public void setSnake(Snake snake) { 9 this.snake = snake; 10 } 11 public void paint(){ 12 for(int i=0;i<=15;i++){ 13 for(int j=0;j<=40;j++){ 14 if(i==0||i==15){ 15 System.out.print("-"); 16 }else if(j==0||j==40){ 17 System.out.print("|"); 18 }else if(snake.contains(i,j)){ 19 System.out.print("■"); 20 }else{ 21 System.out.print(" "); 22 } 23 } 24 System.out.println(); 25 } 26 } 27 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.myproj.snake; 2 3 import java.util.Scanner; 4 5 public class SnakeTest { 6 public static void main(String[] args){ 7 SnakePan snakePan=new SnakePan(); 8 Snake snake=snakePan.getSnake(); 9 snakePan.paint(); 10 /*for(int i=0;i<10;i++){ 11 snakePan.paint(); 12 snake.move(); 13 try { 14 Thread.sleep(1000); 15 } catch (InterruptedException e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } 19 }*/ 20 21 Scanner scanner=new Scanner(System.in); 22 System.out.println("请输入方向【上(U)、下(D)、左(L)、右(R)】:"); 23 while(true){ 24 String dir=scanner.nextLine(); 25 if(dir.equalsIgnoreCase("U")){ 26 snake.move(snake.UP); 27 }else if(dir.equalsIgnoreCase("D")){ 28 snake.move(snake.DOWN); 29 }else if(dir.equalsIgnoreCase("L")){ 30 snake.move(snake.LEFT); 31 }else if(dir.equalsIgnoreCase("R")){ 32 snake.move(snake.RIGHT); 33 }else{ 34 System.exit(0); 35 } 36 snakePan.paint(); 37 } 38 } 39 }