⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snakebody.java

📁 3D snake game with intermediate graphic
💻 JAVA
字号:
/*
 *   Snake 3D by Krisna
 */
public class SnakeBody {
    
    
    
    
    private  Snakecube3D[] myBody=new Snakecube3D[400] ;
    private int[] colors=new int[]{0x880000,0xCC0000,0xFF0000,0xFF2F2F,0xFF4A4A,0xFF7979,0xFFA4A4};
    private  short len=5;
    private short score;
    private short value;
    private short direction=4;
    private float ycamera=30;
    private float xcamera;
    private float zcamera=30;
    public static boolean GameOver=false;
    public static boolean canChange=true;
    public static boolean GamePause;
    
    
    public SnakeBody() {
        myBody[0]=new Snakecube3D((short)0,(int)(0xFFCC00));
        for(short i=1;i<len;i++) {
            myBody[i]=new Snakecube3D(i,colors[i%7]);
        }
    }
    
    public void setGamePause(boolean GamePause) {
        this.GamePause = GamePause;
    }
    
    public boolean isGamePause() {
        return GamePause;
    }
    
    public void setDirection(short dir) {
        if(direction>=3&&dir<3) {
            direction = dir;
            canChange=false;
        } else if(direction<=2&&dir>2){
            direction = dir;
            canChange=false;
        }
        
    }
    
    
    public Snakecube3D[] getMyBody() {
        return myBody;
    }
    public  void update(SnakeFood myfood) {
        
        for(int i=len-1;i>0;i--) {
            myBody[i].setVertix(myBody[i-1].getVert());
        }
        
     /* arah :
      * Up  1
      * Down  2
      * Right 3
      * Left 4
      **/
        if(direction==1) {
            
            myBody[0].decreaseZ();
            zcamera-=5;
        } else
            if(direction==2) {
            myBody[0].increaseZ();
            zcamera+=5;
            } else if(direction==3) {
            myBody[0].increaseX();
            xcamera+=4;
            
            } else if(direction==4) {
            myBody[0].decreaseX();
            xcamera-=4;
            }
        
        
        GameOver=myBody[0].getCheckBound(direction);
        
        myfood.update(myBody[0],direction);
        
        checkAllBody();
        if(myfood.checkEaten()) {
            
            score+=value;
            len++;
            myBody[len-1]=new Snakecube3D((short)(len-1),colors[len%7]);
            myBody[len-1].setIVbcube(myBody[len-2].getIVbcube());
            myfood.generateFood();
            myfood.setEaten(false);
        }
    
        canChange=true;
    }
    public void checkAllBody() {
        for(int i=2;i<len;i++) {
            if(myBody[0].getCheckBodyCollision(direction,myBody[i],i)) {
                GameOver=true;
                
                return;
            }
        }
    }
    public short getScore() {
        return score;
    }
    
    
    public float getXcamera() {
        return xcamera;
    }
    
    public float getYcamera() {
        return ycamera;
    }
    
    public float getZcamera() {
        return zcamera;
    }
    
    public short getLen() {
        return len;
    }
    
    public void setValue(short value) {
        this.value = value;
    }
    
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -