package snake;
import javax.swing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SnakeL extends JFrame implements KeyListener
{
SnakeBody sb;
Snake snake;


public SnakeL(String title){
super(title);
snake=new Snake();
sb=snake.sb;
Container container=getContentPane();
container.setLayout(new FlowLayout());


addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}});
setSize(300,300);
setLocation(100,100);
container.add(snake);
addKeyListener(this);
setVisible(true);
}

 public void keyPressed(KeyEvent e) 
   {
   
    int key=e.getKeyCode();
    switch(key)
    {
    case KeyEvent.VK_DOWN:
    sb.changeDirection(key);
    break;
   
    case KeyEvent.VK_UP:
      sb.changeDirection(key);
        break;
    case  KeyEvent.VK_LEFT:
    sb.changeDirection(key);
        break;
    case KeyEvent.VK_RIGHT:
    sb.changeDirection(key);
        break;
    case KeyEvent.VK_ESCAPE:
        sb.isAlive=false;
        break;
    case KeyEvent.VK_SPACE:
    snake.begin();
    System.out.println("snakel space key down");
    break;
   
    default:
        break;
    }
   
    }       
 public void keyReleased(KeyEvent e){}      
 public void keyTyped(KeyEvent e){}  
public static void main(String args[])
{
new SnakeL("贪吃蛇");
}

}







解决方案 »

  1.   

    package snake;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.image.*;
    class SnakeBody{
    //int cell=10;
    public Vector vector;
    static final int UP=1,DOWN=2,LEFT=5,RIGHT=10;
        //int direction=0;
        int currentDirection=0;
    int headx,heady;
    int head=-1;
    int tail=-1;
    int max=100;
    int body[][]=new int[max][2];
    boolean isAlive=true;
    Thread thread;
    public void init()
    {
    currentDirection=RIGHT;
    isAlive=true;
    head=2;
         tail=0;
         for(int i=tail;i<=head;i++)
         {
         body[tail][0]=tail+1;
         body[tail][1]=tail+2;
         }
         headx=body[head][0];
         heady=body[head][0];
        }
        public SnakeBody(){
         init();
          }

    //蛇前进
    public void go(){
    headx=body[head][0];
    heady=body[head][1];
    newHead();
    System.out.println("in SnakeBody.go()");

    }

    //蛇在前进中产生新的蛇头
    public void newHead(){
    switch(currentDirection)
    {
    case UP:
        heady--;
        break;
        case DOWN:
        heady++;
        break;
        case LEFT:
        headx--;
        break;
        case RIGHT:
        headx++;
        break;
        default:
        break;
      }
        if(hasDie())
        //stop();
        isAlive=false;
        if(++head==max)
        head=0;
        body[head][0]=headx;
        body[head][1]=heady;
        //currentDirection=direction;
    }

    //转变方向
    public void changeDirection(int direction)
    {
    //this.direction=direction;
    System.out.println("in changeDirection");
    if(direction==currentDirection)
    return ;
    if(direction/2==currentDirection)
    return ;
    if(direction*2==currentDirection)
    return ;

    currentDirection=direction;

    }


        
        //判断是否死了
    public boolean hasDie(){
    if(eatSelf()||outBoundary())//吃自己或者出界
    {
    isAlive=false;
    thread=null;
    System.out.println("headx:"+body[head][0]+"   heady:"+body[head][1]);
    System.out.println("game over!");
    return true;
    }
    return false;
    }

    //是否吃自己
    public boolean eatSelf(){
    if(contain(headx,heady))
    return true;
    return false;
    }


    //是否包含次节点
    public boolean contain(int x,int y){
    if(head>tail)
    {
    for(int i=tail;i<=head;i++)
    {
    if(body[i][0]==x&&body[i][1]==y)
    return true;
    }                    
    }
    if(head<tail)
    {
    for(int i=tail;i<max;i++)
    {
    if(body[i][0]==x&&body[i][1]==y)
    return true;
    }
    for(int i=0;i<=head;i++)
    {
    if(body[i][0]==x&&body[i][1]==y)
    return true;
    }
    }
    return false;
    }
    public boolean outBoundary(){
    if(headx<0||headx>9||heady<0||heady>9)
    return true;
    return false;
    }


    public void removeTail(){
    if(++tail==max)
    tail=0;
    } public boolean hasEat(int foodx,int foody)
    {
    if(foodx==headx&&foody==heady)
    return true;
    return false;
    }

    }