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

📄 map.java

📁 使用j2me编写的手机平台上一个游戏的地图绘制代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class map extends MIDlet implements AppExiter
{
   protected void startApp(){
      // create the screen and pass a reference to so that EXIT can be performed
      // tell the Display Manager to display our screen
      Display.getDisplay(this).setCurrent(new mapcanvas(this));
   }
   protected void pauseApp(){}
   protected void destroyApp(boolean unconditional){
      notifyDestroyed();
   }
   public void exitApp(){
      destroyApp(true);
   }
}

class mapcanvas extends Canvas implements CommandListener{
   // save some calculated values for speed
    public int sw;
    public int sh;
    public int x0;
    public int y0;
    public int dx;
    public int dy;
    public int i0;
    public int j0;
    public int t;
   private AppExiter    m_appExiter;
    private Graphics bg;            //缓冲区图像设备
    private Image buf;              //缓冲区图像
    private Image tiles[];
    int w = 19;
    int h = 13;
    int mapdata[] = {
                        153, 154, 135, 135, 135, 146, 147, 135, 135, 135, 141, 144, 128,  //0
            155, 28, 2, 2, 2, 17, 20, 2, 2, 2, 14, 143, 128,  //1
            135, 1, 136, 128, 135, 1, 128, 128, 128, 128, 1, 135, 128,  //2
            135, 1, 128, 128, 135, 1, 157, 136, 136, 157, 1, 135, 128,  //3
            135, 1, 157, 157, 135, 1, 128, 128, 136, 128, 1, 135, 128,  //4
            135, 1, 128, 128, 135, 1, 136, 157, 128, 128, 1, 135, 128,  //5
            146, 19, 136, 128, 137, 10, 2, 2, 2, 2, 9, 138, 128,  //6
            145, 20, 136, 128, 139, 140, 135, 135, 135, 135, 11, 140, 128,  //7
            135, 1, 128, 157, 128, 136, 128, 128, 157, 128, 1, 135, 128,  //8
            135, 1, 128, 128, 128, 128, 128, 136, 128, 136, 1, 135, 128,  //9
            135, 1, 157, 128, 136, 128, 157, 128, 136, 136, 5, 134, 128,  //10
            137, 10, 2, 2, 18, 19, 2, 2, 2, 2, 3, 132, 128,  //11
            139, 12, 135, 135, 145, 148, 135, 135, 135, 135, 1, 135, 128,  //12
            135, 1, 136, 128, 157, 128, 157, 128, 136, 136, 1, 135, 128,  //13
            135, 1, 128, 136, 128, 136, 128, 128, 157, 128, 1, 135, 128,  //14
            135, 1, 157, 128, 136, 128, 136, 128, 128, 136, 1, 135, 128,  //15
            141, 16, 2, 2, 2, 5, 6, 2, 2, 2, 18, 147, 128,  //16
            142, 143, 135, 135, 135, 131, 132, 135, 135, 135, 145, 148, 128,  //17
            128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,  //18
            };
            /**
    * The constructor assigns the AppExiter, load the image, and initialies 
    * all the constants.  It also adds the EXIT Command, and registers itself
    * as a command listener.
    */
   public mapcanvas(AppExiter exiter){
      m_appExiter = exiter;
      
      // add the EXIT command to the Canvas
      addCommand(AppExiter.EXIT);
      // register the Canavas as the listener for Commands, the action for
      // the Commands will be handled in the commandAction() method
      setCommandListener(this);
      
        int height = getHeight();
        int width = getWidth();  
        sh = (height>>4)+((height&15)>0?1:0);
        sw = (width>>4)+((width&15)>0?1:0);
        buf = Image.createImage(sw<<4, sh<<4); //按显示屏幕大小建立缓冲对象
                                      //此处也可以设为重复绘制的矩形区域  
                                      //将缓冲图像的图形设备赋给bg
        i0 = 0;
        j0 = 0;
        x0 = 0;
        y0 = 0;
        dx = width;
        dy = height;
        bg = buf.getGraphics();
        tiles = new Image[45];
        InitImageArray();
        drawmap();
   }

   public void InitImageArray(){    
	Image ImageTemp = null;      //存放大图片
	Graphics g1 = null;          
	try
	{
	    ImageTemp = Image.createImage("/tiles.png"); //加载整张大图片
	}
	catch(Exception exception) { 
             return;
	}
	for(int i = 0; i < 45; i++)
	{
	    tiles[i] = Image.createImage(16, 16);//作为截图的小图片
	    g1 = tiles[i].getGraphics();     //获取小图片的图形设备
	    g1.drawImage(ImageTemp, -16 * i, 0, 20); //开始截图
	}
	ImageTemp = null;
	g1 = null;
    }  

    public void drawmap(){
        int x;
        int y;
        int c;
            for(int i=i0;i<i0+sw;i++)
            {
                for(int j=j0;j<j0+sh;j++)
                {
                    x = (i-i0)<<4;
                    y = (j-j0)<<4;
                    c= mapdata[i*h+j]&0x07F;   
                    bg.drawImage(tiles[c], x, y, Graphics.TOP | Graphics.LEFT); //在缓冲区的图形设备中循环绘制图像
                }
            }
            repaint();//将缓冲区的图像重绘到屏幕上
    }
   protected void paint(Graphics g){
        g.drawImage(buf, 0, 0, Graphics.TOP | Graphics.LEFT); //绘制缓冲区图像
   }

      protected void doaction(int keyCode){
       switch(keyCode){
           case UP:
               j0=(j0==0)?j0:j0-1;
               break;
           case DOWN:
               j0=((j0+sh)>=h)?h-sh:j0+1;
               break;
           case LEFT:
               i0=(i0==0)?i0:i0-1;
               break;
           case RIGHT:
               i0=((i0+sw)>=w)?w-sw:i0+1;
               break;
       }
       drawmap();
       return;
   }

   protected void keyRepeated(int keyCode){
      //if (hasRepeatEvents()){
          doaction(getGameAction(keyCode));
      //}
   }
   protected void keyPressed(int keyCode){
      //if (!hasRepeatEvents()){
     //}
     // else
     //{
          doaction(getGameAction(keyCode));
      //}
   }

   protected void keyReleased(int keyCode){
      if (!hasRepeatEvents()){
      }
   }

   public void commandAction(Command c, Displayable d){
      if (c == map.EXIT){
         m_appExiter.exitApp();
      }
   }
}
interface AppExiter{
   public static final Command EXIT = new Command("Exit", Command.EXIT, 1);
   public void exitApp();
}

⌨️ 快捷键说明

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