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

📄 gameloop.java

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

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;
/*
 *   Snake 3D by Krisna
 */
public class GameLoop extends Canvas{
    
    private Graphics3D iG3D;
    private Camera iCamera;
    private Light iLight;
    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[]{"Mudah","Normal","Sulit"};
    private short ch;
    private myThread gameThread;
    
    
    public GameLoop() {
        transform = new Transform();
        
        transform.postTranslate(0.0f,10.0f,0.0f);
        transform.postRotate(-35.0f,1.0f,0.0f,0.0f);
        
        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) {
             
                gameMode=false;
                
            }
            iG3D.bindTarget(g,true,Graphics3D.TRUE_COLOR | Graphics3D.DITHER);
            
            
            
            Transform camtans = new Transform();
            
            iG3D.resetLights();
            
            iG3D.clear(iBackground);
            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("Tekan sembarang tombol untuk mulai",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;
        }

    }
    
}

⌨️ 快捷键说明

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