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

📄 m3gcanvas.java

📁 3D手机游戏开发实例源代码 3D迷宫游戏的演示程序
💻 JAVA
字号:


import java.util.*;

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;


public class M3GCanvas extends Canvas implements Runnable 
{
  private static final int FLOOR_LEN = 8;   // should be even
  private Graphics3D g3d;
  private World world;
  private MobileGunCamera mobCam;
  private ShotManager shotMng;
  private Terrerist1 terrerist1;
  private Terrerist2 terrerist2;
  private int iStartTime;
  private int currentTime;
  private boolean isKilled1 = false;
  private boolean isKilled2 = false;
  private int xHit;       // x-coord of hit image on screen
  public M3GCanvas() 
  { 
    g3d = Graphics3D.getInstance();
    world = new World();   // create world graph
    buildScene();
    Thread t = new Thread(this);
    t.start();
  }  // end of M3GCanvas()


 

  private void buildScene()
  // add elements to the world
  {
    addCamera();
    addLights();
    addBackground();
    addFloor();
 
    addTerrerist1();
    addTerrerist2();
    shotMng = new ShotManager(world, mobCam); // add explosions stuff
  } // end of buildScene()

  private void addTerrerist1(){
    terrerist1 = new Terrerist1("Kitty" , 341, 0, 0);
   
    world.addChild( terrerist1.getModel() );   // add the model
        
  }
  private void addTerrerist2(){
    terrerist2 = new Terrerist2("Jack" , 319, 2, 0);
  
    world.addChild( terrerist2.getModel() );   // add the model
   // world.addChild(terrerist2);
  }
  private void addCamera()
  // the camera is a MobileGunCamera object
  {
    mobCam = new MobileGunCamera( getWidth(), getHeight());
    world.addChild( mobCam.getCameraGroup() );
    world.setActiveCamera( mobCam.getCamera() ); 
  }  // end of addCamera()


  private void addLights()
  {
    Light light = new Light();  // default white, directional light
    light.setIntensity(1.25f);  // make it a bit brighter
    light.setOrientation(-45.0f, 1.0f, 0, 0); // pointing down and into world
    world.addChild(light);
  }  // end of addLights()


  private void addBackground()
  /* Use a background image. The choice of image
     is made at random from 3 possibilities.
  */
  { Background backGnd = new Background();



   
 Image2D backIm = loadImage("/2d/background.png"); 
    
 

    if (backIm != null)  
      backGnd.setImage( backIm );
    else 
      backGnd.setColor(0x00bffe); // a light blue background

    world.setBackground(backGnd);
  } // end of addBackground()


  private void addFloor()
  /* Tile an image over an FLOOR_LEN-by-FLOOR_LEN sized floor. 
     The choice of image is made at random from 4 possibilities.
  */
  { Image2D floorIm = null;

      floorIm = loadImage("/2d/grass.png"); 


    TiledFloor f = new TiledFloor( floorIm, FLOOR_LEN);   
      // FLOOR_LEN by FLOOR_LEN size, made up of 1 by 1 tiles

    world.addChild( f.getFloorMesh() );
  }  // end of addFloor()





  // ---------------image loading -----------------------------


  private Image2D loadImage(String fn)
  // load the image stored in fn
  { Image2D im = null;
    try {
       im = (Image2D)Loader.load(fn)[0];
    }
    catch (Exception e)
    { System.out.println("Cannot make image from " + fn); }
    return im;
  }  // end of loadImage()





  // ------------ process key presses and releases ------------

  protected void keyPressed(int keyCode)
  { 
    int gameAction = getGameAction(keyCode);
    if (gameAction == Canvas.FIRE)
      shotMng.shootGun();
    else
      mobCam.pressedKey(gameAction);
  } // end of keyPressed()


  protected void keyReleased(int keyCode)
  { int gameAction = getGameAction(keyCode);
    mobCam.releasedKey(gameAction);
  }


  // ------------------ update and paint the canvas ------------------

  public void run() {
	        while(true){
    mobCam.update();    // update the camera
    shotMng.update();   // update the shot manager
    update();
    repaint();
           // flushGraphics();
        }

	}
	private void update(){
		 TerreristInfo tInfo;
		 if(isKilled1 == false){
	    tInfo = (TerreristInfo)terrerist1.getModel().getUserObject();
	    if (tInfo != null) {
        if(tInfo.getNumLives()==0){
      
          iStartTime = (int) System.currentTimeMillis();
          isKilled1 = true;
        }
      }
    }else{
       currentTime = ((int) System.currentTimeMillis()-iStartTime)/3 +800;
       if(currentTime >= 1149){
  	     terrerist1.getModel().setRenderingEnable(false);
  	   }else{
        terrerist1.getModel().animate(currentTime);
      }
    }
    
   if(isKilled2 == false){
	    tInfo = (TerreristInfo)terrerist2.getModel().getUserObject();
	    if (tInfo != null) {
        if(tInfo.getNumLives()==0){
       
          iStartTime = (int) System.currentTimeMillis();
          isKilled2 = true;
        }
      }
    }else{
       currentTime = ((int) System.currentTimeMillis()-iStartTime)*2 +6000;
       if(currentTime >= 9000){
  	     terrerist2.getModel().setRenderingEnable(false);
  	   }else{
        terrerist2.getModel().animate(currentTime);
      }
    }
    
	}

  protected void paint(Graphics g)
  {
    g3d.bindTarget(g);
    try {
      g3d.render(world);
    }
    catch(Exception e)
    { e.printStackTrace();  }
    finally {
      g3d.releaseTarget();
    }

    // maybe show a hit message (an image and text)
    if ( shotMng.hitMade() ) {


      // large and bold
	    g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, 
                             Font.STYLE_BOLD, Font.SIZE_LARGE));
      g.setColor(0xFF0000);  // red
      g.drawString( "ID of Terrerist: " + shotMng.getHitID(), 5,5, 
                                      Graphics.TOP|Graphics.LEFT);
     

    }
      g.setColor(0x00FF00);  // green
      g.drawLine(getWidth()/2-5, getHeight()/2, getWidth()/2+5,getHeight()/2);
      g.drawLine(getWidth()/2, getHeight()/2-5, getWidth()/2,getHeight()/2+5);
  } // end of paint()

}  // end of M3GCanvas class

⌨️ 快捷键说明

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