Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

j2me Red Snake 3D

Sun May 06, 2007 11:22 pm

This is a j2me snake 3D .It is a netBeans Project using netBeans mobility package.
The game is easy to playing.Move the snake to the food.

The 3D graphices concepts are implemented already in java 3D package in j2me so what u have to learn is the API of this classes for example
(Camera,BackGround,Light,Vertixarray,Indexarray,Apperance,Transform,...)

The game contain 3 levels (Hard,Normal,Easy)
The Defferents between these levels are the values of Sleeep.



This is a netBeans Project , You can copy the jar file that in "dist" folder to your mobile and play.

The same is here : finished-projects/snake-3d-on-mobile-j2me-t617.html

( Snake 3d j2me ) Enjoy


Attachments
Red_Snake3D(2).rar
j2me Snake 3D
(234.34 KiB) Downloaded 2417 times

Re: j2me Red Snake 3D

Wed Oct 24, 2007 8:29 pm

I would like to have a look at the source for this if you don't mind, I'm planning on making a multiplayer snake over bluetooth for a project...currently just brainstorming though! :)

Thanx

Re: j2me Red Snake 3D

Thu Oct 25, 2007 4:05 pm

Here are the classes :
:n_w00t:

Code:
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/
public class MIDletMain extends MIDlet implements  CommandListener {
   
    static MIDletMain instance;
    GameLoop displayable= new GameLoop();
    Timer itimer= new Timer();
    public MIDletMain(){
        this.instance=this;
    }
   
    public void startApp() {
        Display.getDisplay(this).setCurrent(displayable);
       // itimer.schedule(new myTimerTask(),0,40);
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    }
    public static void quitApp(){
        instance.destroyApp(true);
        instance.notifyDestroyed();
        instance=null;
    }

    public void commandAction(Command command, Displayable displayable) {
    }
   /* class myTimerTask extends TimerTask{
        public void run() {
            if(displayable!=null){
                displayable.repaint();
                try {
                   
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
       
       
    }*/
}


/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/



Code:
HHimport java.io.IOException;
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;
/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/
public class Ground {
   
    /** Creates a new instance of ground */
    short[] vert={
        50,0,45, 50,0,-45,  -50,0,45,  -50,0,-45
    };
    byte[] norm = {
       
        0, 0, 127,    0, 0, 127,    0, 0, 127,    0, 0, 127,
       
    };
    private   int [] stripLen={4};
    private Appearance iAppearancegd;
    //A set of component objects that define the rendering attributes of a Mesh or Sprite3D.
    private Texture2D texture;
    //An Appearance component encapsulating a two-dimensional
    //texture image and a set of attributes specifying how the image is to be applied .
    private Image2D image2D;
    //A two-dimensional image that can be used as a texture, background .
    private VertexBuffer iVbgd;
    //VertexBuffer holds references to VertexArrays
    private VertexArray verticeArray;
    //An array of integer vectors representing vertex positions.
    private IndexBuffer iIbgd;
    //Defines how to connect vertices to form a geometric object
    private Transform transform;
    //A generic 4x4 floating point matrix, representing a transformation
    private VertexArray normArray;
    //An array of integer representing vectors normals.
    private Material  iMaterial;
    //An Appearance component encapsulating material attributes for lighting computations
    private Fog fm;
    //An Appearance component encapsulating attributes for fogging
    public Ground() {
        transform = new Transform();
        transform.postTranslate(0.0f,0.0f,0.0f);//No Transpostion
        //postTranslate(float x,float y,float z);
        verticeArray=new VertexArray(vert.length/3,3,2);
        //(int numVertices, int numComponents, int componentSize in bytes)
       
        verticeArray.set(0,vert.length/3,vert);
        VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);
        normArray.set(0, norm.length/3, norm);
        iVbgd=new VertexBuffer();
        iVbgd.setPositions(verticeArray,1.0f,null);
         /*
          *index - index of the texturing unit to assign these texture coordinates to
          *texCoords - a VertexArray with 2- or 3-component texture coordinates, or null to disable texture coordinates for the specified unit
          *scale - a constant uniform scale factor common to all texture coordinates
          *bias - a constant (X, Y, Z) offset to add to texture coordinates after scaling, or null to set a zero bias for all components
          **/
        iVbgd.setNormals(normArray);
       
       
       
        iIbgd=new TriangleStripArray(0,stripLen);
       /*
        *TriangleStripArray defines an array of triangle strips.
        *Make the rendering faster
        */
       
       
        iAppearancegd= new Appearance();
        iMaterial=new Material();
        iMaterial.setColor(Material.DIFFUSE,0x99CCFF);
        //Something General,Go every where color
       // iMaterial.setColor(Material.SPECULAR,0x99CCFF);
        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
        iAppearancegd.setMaterial(iMaterial);
        iAppearancegd.setFog(fm);
       
    }
   
    public short[] getVert() {
        return vert;
    }
   
    public IndexBuffer getIIbgd() {
        return iIbgd;
    }
   
    public Appearance getIAppearancegd() {
        return iAppearancegd;
    }
   
    public VertexBuffer getIVbgd() {
        return iVbgd;
    }
   
    public void setVert(short[] vert) {
        this.vert = vert;
    }
    public int getLenght() {
        return vert.length;
    }
   
    public Transform getTransform() {
        return transform;
    }
   
   
}/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/

Code:
/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/
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;// Snake body lenght
    private short score;// Player score
    private short value;// Denpends on the level
    private short direction=4;// To left-Control direction
    private float ycamera=30;
    private float xcamera;
    private float zcamera=30;
    public static boolean GameOver=false;//For game end status
    public static boolean canChange=true;//one action per update
    public static boolean GamePause;//For game pause
   
   
    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) {
        /*
         * Every snake the his neighbours hood
         *
         */
        for(int i=len-1;i>0;i--) {
            myBody[i].setVertix(myBody[i-1].getVert());
        }
       
     /* Snake Directions values :
      * 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;
            }
       
        /*
         Check the head of the Snake if it get out the maze
         **/
        GameOver=myBody[0].getCheckBound(direction);
        /*
         check head and food cube 3D intersection
         */
        myfood.update(myBody[0],direction);
            /*
         *Snake head  with all other body intersection
         **/
        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;
    }
   
   
}/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/

Code:
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Fog;
import javax.microedition.m3g.IndexBuffer;
import javax.microedition.m3g.Material;
import javax.microedition.m3g.Transform;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;
/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/
public class Snakecube3D {
    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 ,
       
    };
    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, 127, 0,    0, 127, 0,    0, 127, 0,    0, 127, 0,
        -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   int [] stripLen={4,4,4,4,4};
    private Appearance iAppearancecube;
    //A set of component objects that define the rendering attributes of a Mesh or Sprite3D.
    private VertexBuffer iVbcube;
    //VertexBuffer holds references to VertexArrays
    private VertexArray verticeArray;
    //An array of integer vectors representing vertex positions.
    private IndexBuffer iIbcube;
    //Defines how to connect vertices to form a geometric object
    private VertexArray normArray;
    //An array of integer representing vectors normals.
    private Material  iMaterial;
    //An Appearance component encapsulating material attributes for lighting computations
    private Transform transform;
    //A generic 4x4 floating point matrix, representing a transformation
    private  Fog fm;
    //An Appearance component encapsulating attributes for fogging
    private int color=0xFF0000;
    /** Creates a new instance of snakecube3D */
    public Snakecube3D() {
        init();
    }
    public Snakecube3D(short index,int color) {
        for(short i=0;i<vert.length;i+=3) {
            vert[i]=(short)(vert[i]+  5*index);
            //    norm[i]=(byte)(norm[i]+  5*index);
        }
       
        this.color=color;
        init();
    }
    public void  init() {
        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);
        iVbcube=new VertexBuffer();
        iVbcube.setPositions(verticeArray,1.0f,null);
        iVbcube.setNormals(normArray);
        iIbcube=new TriangleStripArray(0,stripLen);
        iAppearancecube= new Appearance();
        iMaterial=new Material();
        iMaterial.setColor(Material.DIFFUSE,color);
    //    iMaterial.setColor(Material.SPECULAR,color);
        iMaterial.setShininess(10.0f);
       
     /*   fm=new Fog();
        fm.setColor(0xffffff);//white
        fm.setMode(Fog.LINEAR);// type of equation
        fm.setLinear(1,100);// nearest and farest objects to be seen
        fm.setDensity(0.5f);// how much fog*/
       
        iAppearancecube.setMaterial(iMaterial);
        //iAppearancecube.setFog(fm);
    }
   
   
    public void increaseX() {
        /* When the snake Moving right with increase the X-axis
         **/
        for(short i=0;i<vert.length;i+=3) {
            vert[i]=(short)(vert[i]+5);
        }
        newVertexarr();
    }
    public void decreaseX() {
            /* When the snake Moving left with decrease the X-axis
             **/
        for(short i=0;i<vert.length;i+=3) {
            vert[i]=(short)(vert[i]-5);
        }
        newVertexarr();
    }
    public void increaseZ() {
               /* When the snake Moving down with increase the Z-axis
                **/
        for(short i=2;i<vert.length;i+=3) {
            vert[i]=(short)(vert[i]+5);
        }
        newVertexarr();
    }
    public void decreaseZ() {
            /* When the snake Moving up with decrease the Z-axis
             **/
        for(short i=2;i<vert.length;i+=3) {
            vert[i]=(short)(vert[i]-5);
        }
        newVertexarr();
    }
   
    public void newVertexarr() {
        /*
         *Create a new Vertix buffer with new Vertice array
         *
         **/
        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);
       
    }
    public boolean getCheckBound(short dir) {
        /*
         *Check if the snake get out of the Maze boundery
         *
         **/
        switch(dir) {
            case 1:    return  vert[35]<=-50;
            case 2: return vert[26]>=50;
            case 3:return vert[33]>=50;
            case 4:return vert[24]<=-50;
        }
       
        return false;
    }
    public short getVertat(short index) {
        return vert[index];
    }
    public boolean getCheckEat(short dir,short x,short  z) {
        /*
         *Check if the head get on the Food
         *Depeneds on the postions
         **/
        switch(dir) {
            case 1:
            case 3: return vert[33]==x&&vert[35]==z;
            case 2: return vert[27]==x&&vert[29]==z;
            case 4:return vert[24]==x&&vert[26]==z;
        }
       
        return false;
    }
    public void setVertix(short[] newver) {
        /*
         *set new short[] vert points
         **/
       
        for(int i=0;i<vert.length;i++) {
            vert[i]=newver[i];
        }
        newVertexarr();
       
    }
    public boolean getCheckBodyCollision(short dir,Snakecube3D bodypart,int i) {
       
       
        /*
         *Check if the snake hit it self
         *Check the head postions with the snakeCube postions
         */
       
        switch(dir) {
            case 1:
            case 4: return (vert[33]==bodypart.getVertat((short)33)&&vert[35]==bodypart.getVertat((short)35));
            case 2:
            case 3:return (vert[24]==bodypart.getVertat((short)24)&&vert[26]==bodypart.getVertat((short)26));
        }
       
        return false;
       
    }
    /*
     *Set and Get Methods
     */
    public void setIVbcube(VertexBuffer iVbcube) {
        this.iVbcube = iVbcube;
    }
   
    public void setIIbcube(IndexBuffer iIbcube) {
        this.iIbcube = iIbcube;
    }
   
   
    public short[] getVert() {
        return vert;
    }
   
    public VertexBuffer getIVbcube() {
        return iVbcube;
    }
   
    public Appearance getIAppearancecube() {
        return iAppearancecube;
    }
   
    public Transform getTransform() {
        return transform;
    }
   
    public IndexBuffer getIIbcube() {
        return iIbcube;
    }
   
}/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/


Code:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Transform;
/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/
public class GameLoop extends Canvas{
   
    private Graphics3D iG3D;
    private Camera iCamera;
    private Light iLight;//A scene graph node that represents different kinds of light sources
    private float iAngle=-45.0f;
    private Background iBackground= new Background();
    private Ground myg;
    private SnakeBody mybody;
    private Snakecube3D[] cubarr;
    private SnakeFood skfood;
    private boolean gameMode;
    private  Transform transform;
    private boolean gameStarted;
    private String[] choices=new String[]{"Easy","Normal","Hard"};
    private short ch;
    private myThread gameThread;
   
    /** Creates a new instance of myCan */
    public GameLoop() {
        transform = new Transform();
       
        transform.postTranslate(0.0f,10.0f,0.0f);
        transform.postRotate(-35.0f,1.0f,0.0f,0.0f);
        /*Camera is looking down with angle 35
         *
         */
        setCommandListener(new CommandListener() {
            public void commandAction(Command command, Displayable displayable) {
                if(command.getCommandType()==command.EXIT){
                    if(!gameMode)
                        MIDletMain.quitApp();
                    else {
                        mybody.GameOver=true;
                    }
                }
               
            }
        });
        try{
            addCommand(new Command("Exit",Command.EXIT,1));
        } catch(Exception e){
            e.printStackTrace();
        }
    }
   
   
   
    protected void paint(Graphics g) {
        if(gameMode) {
           
            if(mybody.GameOver) {
                // Game ends
                gameMode=false;
               
            }
            iG3D.bindTarget(g,true,Graphics3D.TRUE_COLOR | Graphics3D.DITHER);
           
           
           
            Transform camtans = new Transform();
           
            iG3D.resetLights();//Resest Lights Computations
           
            iG3D.clear(iBackground);// Refresh the Background
            iG3D.addLight(iLight,transform);
           
           
            camtans.postTranslate(mybody.getXcamera(),mybody.getYcamera(),mybody.getZcamera());
            camtans.postRotate(iAngle,1.0f,0.0f,0.0f);
            iG3D.setCamera(iCamera,camtans);
           
           
           
            iG3D.render(myg.getIVbgd(),myg.getIIbgd(),myg.getIAppearancegd(),myg.getTransform());
           
            if(!mybody.isGamePause()&&gameStarted)
                mybody.update(skfood);
           
           
            for(int i=0;i<mybody.getLen();i++) {
                iG3D.render(cubarr[i].getIVbcube(),cubarr[i].getIIbcube(),cubarr[i].getIAppearancecube(),cubarr[i].getTransform());
            }
            if(!skfood.checkEaten())
                iG3D.render(skfood.getIVbcube(),skfood.getIIbcube(),skfood.getIAppearancecube(),skfood.getTransform());
           
           
           
            iG3D.releaseTarget();
            drawHeader(g);
           
            if(!gameMode) {
                gameStarted=false;
                //  gameMode=false;
                try {
                   
                    Thread.sleep(1000);
                   
                    gameThread.setGamemode(false);
                   
                   
                   
                    gameThread=null;//destroy thread;
                } catch (InterruptedException ex) {
                   
                }
            }
           
           
        } else {
            drawList(g);
        }
    }
    public void drawList(Graphics g) {
        g.fillRect(0,0,300,300);
       
        g.setColor(0xFF0000);
          g.fillRect(40,100+(ch*20),50,15);
        g.setColor(0xFFFFFF);//white
        for(int i=0;i<choices.length;i++)
            g.drawString(choices[i],50,100+(i*20),0);
       
        g.setColor(0xFF0000);
        g.fillRect(30,105+(ch*20),10,5);
         
     //    g.drawString(choices[i],50,100+(i*20),0);
       
       
        g.setColor(0x00FF00);
        g.drawString("Snake3D Game",60,10,0);
       
       
    }
    public void drawHeader(Graphics g) {
        g.setColor(0xFFFF00);
       
        g.drawRect(10,10,60,15);
        g.drawString("Score :"+mybody.getScore(),12,10,0);
       
        g.setColor(0x00FF99);
        if(mybody.GameOver) {
            g.setColor(0xFF0000);
            g.drawRect(155,10,70,15);
            g.drawString("Game Over",160,10,0);
        } else  if(mybody.isGamePause()) {
            g.drawRect(155,10,70,15);
            g.drawString("      Paused",160,10,0);
        } else if(gameStarted) {
            g.drawRect(155,10,70,15);
            g.drawString("Level :"+choices[ch],160,10,0);
        } else {
            g.drawString("Press any key to start",100,10,0);
        }
       
    }
    protected void keyPressed(int keyCode) {
       
        short action=(short)getGameAction(keyCode);
        if(gameMode) {
            if(gameMode)
                gameStarted=true;
            if(gameStarted) {
                /**
                 * Game Mode
                 *
                 **/
                if(mybody.canChange) {
                   
                    if(action==RIGHT) {
                       
                        mybody.setDirection((short)3);
                       
                       
                    } else if(action==LEFT) {
                        mybody.setDirection((short)4);
                       
                    }  else if(action==UP) {
                       
                        mybody.setDirection((short)1);
                    } else if(action==DOWN) {
                       
                        mybody.setDirection((short)2);
                    }
                }
               
                if(action==GAME_A) {
                    iAngle+=5f;
                } else if(action==GAME_B) {
                    iAngle-=5f;
                }
                if(action==FIRE) {
                    mybody.setGamePause(!mybody.isGamePause());
                   
                }
            }
           
           
        } else {
            /**
             * Main Menu
             *
             **/
            if(action==UP) {
               
                if(ch>0)
                    ch-=1;
               
               
            } else if(action==DOWN) {
               
                if(ch<2)
                   
                    ch+=1;
               
            } else if(action==FIRE) {
               
               
                {
                    gameMode=true;
                    gameStarted=false;
                    SnakeBody.GameOver=false;
                    SnakeBody.canChange=true;
                    SnakeBody.GamePause=false;
                    mybody=new SnakeBody();
                    skfood=new SnakeFood();
                    cubarr=mybody.getMyBody();
                    iG3D=Graphics3D.getInstance();
                    iCamera= new Camera();
                   
                    iCamera.setPerspective(100.0f,(float)getWidth()/(float)getHeight(),1.0f,1500.0f);
                    iLight=new Light();
                    iLight.setColor(0xFFFFFFFF);
                    iLight.setIntensity(1.20f);
                    myg=new Ground();
                   
                    iBackground.setColor(0x000000);
                   
                    gameThread=new myThread((short)(230-(ch*70)));
                    gameThread.start();
                    mybody.setValue((short)(2*(ch+1)));
                }
            }
            repaint();
        }
       
    }
   
    class myThread extends Thread {
        private short sleeptime;
        private boolean gamemode;
        myThread(short time) {
            sleeptime=time;
            gamemode=true;
        }
        public void run() {
            while(gamemode) {
               
               
                try {
                    repaint();
                    Thread.sleep(sleeptime);
                } catch (InterruptedException ex) {
                    //ex.printStackTrace();
                }
            }
        }

        public void setGamemode(boolean gamemode) {
            this.gamemode = gamemode;
        }

    }
   
}


/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/

Code:
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;
/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/

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;
    //A set of component objects that define the rendering attributes of a Mesh or Sprite3D.
    private VertexBuffer iVbcube;
    //VertexBuffer holds references to VertexArrays
    private VertexArray verticeArray;
    //An array of integer vectors representing vertex positions.
    private IndexBuffer iIbcube;
    //Defines how to connect vertices to form a geometric object
    private Transform transform;
    //A generic 4x4 floating point matrix, representing a transformation
    private VertexArray normArray;
    //An array of integer representing vectors normals.
    private Material  iMaterial;
    //An Appearance component encapsulating material attributes for lighting computations
    private Fog fm;
    //An Appearance component encapsulating attributes for fogging
   
    private Random myrand=new Random();
    private boolean eaten;
    /** Creates a new instance of SnakeFood */
    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) {
        /*
         * check head and food cube 3D intersection
         **/
        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() {
       
        /*
         *Generate Random Points to the new Food Postion
         **/
        x=(short)((myrand.nextInt(19)*factor)-45);
        z =(short)((myrand.nextInt(16)*factor)-40);
       
       
        /*
         *Copy orginal place to array
         *
         **/
        for(int i=0;i<vert.length;i++) {
            vert[i]=verttemp[i];
        }
        /*
         *Add the new Point to each point in array
         *
         **/
        for(int i=0;i<vert.length;i+=3) {
            vert[i]+=x;
            vert[i+2]+=z;
           
        }
          /*
           *Create the new vertex Array with the new Array
           *
           **/
        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);
       
    }
   
   
}
/*
*   *       Please Visit us at www.codemiles.com     *
*  This Program was Developed by www.codemiles.com forums Team
*  *           Please Don't Remove This Comment       *
* Mohamed Sami Ismail
* mail: [email protected]
* Codemiles.com Team
*/

:gOOd: :gOOd: :gOOd: :gOOd: :gOOd:

Post a reply
  Related Posts  to : j2me Red Snake 3D
 Snake 3d on mobile (J2me)     -  
 Need Snake Game implemented using J2me on Mobile     -  
 Snake applet     -  
 Simple Snake Game     -  
 Snake Game 2D (J2SE)     -  
 Snake Java (SE) Game     -  
 need help in java 1.4 snake game project     -  
 J2ME     -  
 What is the J2ME!!!!     -  
 looking for J2ME opportunities     -