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

📄 snakefood.java

📁 3D snake game with intermediate graphic
💻 JAVA
字号:

import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Fog;
import javax.microedition.m3g.Image2D;
import javax.microedition.m3g.IndexBuffer;
import javax.microedition.m3g.Material;
import javax.microedition.m3g.PolygonMode;
import javax.microedition.m3g.Texture2D;
import javax.microedition.m3g.Transform;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;
/*
 *   Snake 3D by Krisna
 */
public class SnakeFood {
    short[] vert={
        0,0,0, 0,5,0,  -5,0,0,  -5,5,0,
        0,0,-5, 0,5,-5,  0,0,0,  0,5,0 ,
        0,5,0, 0,5,-5,  -5,5,0,  -5,5,-5 ,
        -5,0,0, -5,5,0,  -5,0,-5,  -5,5,-5 ,
        -5,0,-5, -5,5,-5,  0,0,-5,  0,5,-5 ,
        
        
    };
    short[] verttemp={
        0,0,0, 0,5,0,  -5,0,0,  -5,5,0,
        0,0,-5, 0,5,-5,  0,0,0,  0,5,0 ,
        0,5,0, 0,5,-5,  -5,5,0,  -5,5,-5 ,
        -5,0,0, -5,5,0,  -5,0,-5,  -5,5,-5 ,
        -5,0,-5, -5,5,-5,  0,0,-5,  0,5,-5 ,
        
        
    };
    byte[] norm = {
        0, 0, 127,    0, 0, 127,    0, 0, 127,    0, 0, 127,
        0, 127, 0,    0, 127, 0,    0, 127, 0,    0, 127, 0,
        0, 0, 127,    0, 0, 127,    0, 0, 127,    0, 0, 127,
        -127, 0, 0,   -127, 0, 0,   -127, 0, 0,   -127, 0, 0,
        0, 0,-127,    0, 0,-127,    0, 0,-127,    0, 0,-127,
    };
    private boolean sign;
    private short factor=5;
    private short x;
    private short z;
    
    private   int [] stripLen={4,4,4,4,4};
    private Appearance iAppearancecube;
    
    private VertexBuffer iVbcube;
    
    private VertexArray verticeArray;
    
    private IndexBuffer iIbcube;
    
    private Transform transform;
    
    private VertexArray normArray;
    
    private Material  iMaterial;
    
    private Fog fm;
    
    
    private Random myrand=new Random();
    private boolean eaten;
    
    public SnakeFood() {
        transform = new Transform();
        transform.postTranslate(0.0f,0.0f,0.0f);//No Transpostion
        verticeArray=new VertexArray(vert.length/3,3,2);
        verticeArray.set(0,vert.length/3,vert);
        normArray = new VertexArray(norm.length / 3, 3, 1);
        normArray.set(0, norm.length/3, norm);
        
        iIbcube=new TriangleStripArray(0,stripLen);
        iAppearancecube= new Appearance();
        iMaterial=new Material();
        iMaterial.setColor(Material.DIFFUSE,0x0000FF);
      //  iMaterial.setColor(Material.SPECULAR,0x0000FF);
        
        iMaterial.setShininess(100.0f);
        
       /* fm=new Fog();
        fm.setColor(0xffffff);//white
        fm.setMode(Fog.LINEAR);// type of equation
        fm.setLinear(1,200);// nearest and farest objects to be seen
        fm.setDensity(0.5f);// how much fog*/
        
      //  iAppearancecube.setFog(fm);
        iAppearancecube.setMaterial(iMaterial);
        generateFood();
        
    }
    
    public IndexBuffer getIIbcube() {
        return iIbcube;
    }
    
    public VertexBuffer getIVbcube() {
        return iVbcube;
    }
    
    public Appearance getIAppearancecube() {
        return iAppearancecube;
    }
    
    public Transform getTransform() {
        return transform;
    }
    public void update(Snakecube3D head ,short dir) {
    
        switch(dir) {
            case 2:
            case 4:eaten=head.getCheckEat(dir,vert[24],vert[26]);break;
            case 1:
            case 3:eaten=head.getCheckEat(dir,vert[33],vert[35]);break;
        }
        
    }
    
    public void setEaten(boolean eaten) {
        this.eaten = eaten;
    }
    
    public boolean checkEaten() {
        return eaten;
    }
    public void generateFood() {
        
    
        x=(short)((myrand.nextInt(19)*factor)-45);
        z =(short)((myrand.nextInt(16)*factor)-40);
        
        
    
        for(int i=0;i<vert.length;i++) {
            vert[i]=verttemp[i];
        }
    
        for(int i=0;i<vert.length;i+=3) {
            vert[i]+=x;
            vert[i+2]+=z;
            
        }
    
        verticeArray=new VertexArray(vert.length/3,3,2);
        verticeArray.set(0,vert.length/3,vert);
        iVbcube=new VertexBuffer();
        
        iVbcube.setPositions(verticeArray,1.0f,null);
        iVbcube.setNormals(normArray);
        
    }
    
    
}

⌨️ 快捷键说明

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