一、实现效果
二、题目要求
三、项目结构
四、节点信息 Node.java
package sakeGame;
public class Node {
private int i;
private int j;
public Node ( ) { }
public Node ( int i, int j) {
this . i = i;
this . j = j;
}
public int getI ( ) {
return i;
}
public void setI ( int i) {
this . i = i;
}
public int getJ ( ) {
return j;
}
public void setJ ( int j) {
this . j = j;
}
public int hashCode ( )
{
return i* 8 + j* 96 ;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
Node node = ( Node) o;
return i == node. i &&
j == node. j;
}
}
五、贪吃蛇主体 WormPanel.java
package sakeGame;
import java. util. HashSet;
import java. util. LinkedList;
import java. util. Random;
class WormPanel {
private Worm worm;
private int rows = 10 ;
private int cols = 32 ;
private HashSet< Node> foods = new HashSet < > ( ) ;
public WormPanel ( )
{
worm = new Worm ( ) ;
getFoods ( 40
) ;
}
public Worm getWorm ( ) {
return worm;
}
public void showPanel ( )
{
for ( int i= 0 ; i< rows; i++ )
{
for ( int j= 0 ; j< cols; j++ )
{
if ( i == 0 || i == rows- 1 )
{
System. out. print ( "-" ) ;
}
else if ( j == 0 || j == cols- 1 )
{
System. out. print ( "|" ) ;
}
else if ( worm. isContains ( i, j) )
{
System. out. print ( "#" ) ;
}
else if ( foods. contains ( new Node ( i, j) ) )
{
System. out. print ( "O" ) ;
}
else
System. out. print ( " " ) ;
}
System. out. println ( ) ;
}
}
public void getFoods ( int foodNum)
{
Random random = new Random ( ) ;
while ( true )
{
int i= random. nextInt ( rows- 2 ) + 1 ;
int j= random. nextInt ( cols- 2 ) + 1 ;
if ( worm. isContains ( i, j) )
continue ;
foods. add ( new Node ( i, j) ) ;
if ( foods. size ( ) == foodNum)
break ;
}
}
public class Worm
{
public static final int UP = - 10 ;
public static final int DOWN = 10 ;
public static final int LEFT = - 1 ;
public static final int RIGHT = 1 ;
private LinkedList< Node> nodes = new LinkedList < > ( ) ;
private int direction;
public Worm ( )
{
nodes. add ( new Node ( 3 , 9 ) ) ;
nodes. add ( new Node ( 4 , 9 ) ) ;
nodes. add ( new Node ( 5 , 9 ) ) ;
nodes. add ( new Node ( 5 , 10 ) ) ;
nodes. add ( new Node ( 5 , 11 ) ) ;
nodes. add ( new Node ( 6 , 11 ) ) ;
this . direction = RIGHT;
}
public void step ( )
{
if ( foods. size ( ) == 0 ) {
System. out. println ( "恭喜你赢了!" ) ;
System. exit ( 0 ) ;
}
Node head = nodes. getFirst ( ) ;
int i = head. getI ( ) + direction/ 10 ;
int j = head. getJ ( ) + direction% 10 ;
if ( i<= 0 || i>= rows- 1 || j<= 0 || j>= cols- 1 )
throw new RuntimeException ( "哎呀,你这么这么不小心,你撞墙了!" ) ;
head = new Node ( i, j) ;
for ( Node node : nodes)
{
if ( node. equals ( head) )
throw new RuntimeException ( "bang,你把自己吃了!" ) ;
}
nodes. addFirst ( head) ;
nodes. removeLast ( ) ;
if ( foods. remove ( new Node ( i, j) ) )
{
Node rail = new Node ( i, j) ;
nodes. addLast ( rail) ;
}
}
public void step ( int direction)
{
if ( this . direction + direction == 0 )
{
throw new RuntimeException ( "憨批,不能掉头!" ) ;
}
this . direction = direction;
step ( ) ;
}
public boolean isContains ( int i, int j)
{
return nodes. contains ( new Node ( i, j) ) ;
}
}
}
六、贪吃蛇控制面板 WormDemo.java
package sakeGame;
import java. util. Scanner;
public class WormDemo {
public static void main ( String[ ] args)
{
Scanner sc = new Scanner ( System. in) ;
WormPanel wormPanel = new WormPanel ( ) ;
WormPanel. Worm worm = wormPanel. getWorm ( ) ;
while ( true )
{
wormPanel. showPanel ( ) ;
System. out. println ( "W:向上移动\tS:向下移动" ) ;
System. out. println ( "A:向左移动\tD:向右移动" ) ;
String dir = sc. nextLine ( ) ;
if ( "w" . equalsIgnoreCase ( dir) )
worm. step ( WormPanel. Worm. UP) ;
else if ( "s" . equalsIgnoreCase ( dir) )
worm. step ( WormPanel. Worm. DOWN) ;
else if ( "a" . equalsIgnoreCase ( dir) )
worm. step ( WormPanel. Worm. LEFT) ;
else if ( "d" . equalsIgnoreCase ( dir) )
worm. step ( WormPanel. Worm. RIGHT) ;
else if ( "q" . equalsIgnoreCase ( dir) ) {
System. out. println ( "Game over" ) ;
break ;
}
else
worm. step ( ) ;
}
}
}