public void keyPressed(int keyCode) {
    //      
    if ( !gameOverFlag ) {
        switch (keyCode) {
            // ,      ,
            //     
            case KEY_NUM2:
checkMove(xHead, yHead-imageSize); moveUp(); break;
            case KEY_NUM4:
checkMove(xHead- imageSize, yHead); moveLeft(); break;
            case KEY_NUM6:
checkMove(xHead+ imageSize, yHead); moveRight(); break;
            case KEY_NUM8:
checkMove(xHead, yHead+ imageSize); moveDown(); break;
        }
        //   
        repaint();
    }
}
//   
//     (xH,yH)
public void checkMove(int xH, int yH) {
    //    
    for ( int i=3; i<snake.size()-1; i++ ) {
        //       
        if ( xH == ((SnakePart)(snake.elementAt(i))).getX() &&
            yH == ((SnakePart)(snake.elementAt(i))).getY())
            //    
        gameOverFlag=true;
    }
}
//  
private void moveLeft() {
    //     
    if ( direction!=RIGHT) {
        //    
        xHead-=imageSize;
        //     
        if ( xHead < 4) { gameOverFlag=true; return; }
        //    
        SnakePart head = (SnakePart)(snake.firstElement());
        //    
        if ( direction==UP )
            //       
            head.setPartDir(ACLOCKWISE_TURN,LEFT);
        else {
            //    
            if ( direction==DOWN )
                //       
                head.setPartDir(CLOCKWISE_TURN,LEFT);
            else //    
                //    
                head.setPartDir(BODY,LEFT);
        }
        //     
        SnakePart sPart = new SnakePart(xHead,yHead,HEAD,LEFT);
        //     
        snake.insertElementAt(sPart,0);
        //  
        //   
        snake.removeElement(snake.lastElement());
        //    
        ((SnakePart)snake.lastElement()).setPartDir(TAIL,-1);
        //   
        direction = LEFT;
    }
}
