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

📄 mygamecanvas.java

📁 Kjava游戏架构代码 新手可以借鉴学习
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.IOException;

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.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

public class MyGameCanvas extends GameCanvas implements Runnable, CommandListener {


    private LayerManager lm;
    private Sprite c1 ;
//    private Sprite c2 ;

    TiledLayer b1;
    TiledLayer b2;
    TiledLayer b3;
	Image img = null;

	//scene cells 尺寸
	int cellsx=20;
	int cellsy=20;
	int tiledx=32;
	int tiledy=32;
	//role初始位置
	int x = tiledx*cellsx/2 ;
    int y = tiledy*cellsy/2 ;    
	//屏幕宽高
	private int nWidth = getWidth();
	private int nHeight = getHeight();
//todo    
	int [][]heroseq={{0,1},{2,3}}; 
	private Sprite pMen;
	int [] hero1={0,1,2}; //walk
	int [] hero2={3,4,5};
	int [] hero3={6,7,8};
	int [] hero4={9,10,11};
	int [] hero5={12,13,14}; //fire
	int [] hero6={15,16,17};
	int [] hero7={18,19,20};
	int [] hero8={21,22,23};
	Sprite effect=null;
	Sprite effectbut=null;
    public MyGameCanvas()
    {
            super(true) ;
            
    		addCommand(new Command("辅助说明",Command.HELP,1)) ;
    		addCommand(new Command("返回",Command.BACK,1)) ;
    		setCommandListener(this) ;

    		lm = new LayerManager();
            c1 = createCharacter("/res/boss.png") ;
            c1.setPosition(200,200);

            System.out.println("c1层的高度"+c1.getHeight());
            System.out.println("c1层的宽度"+c1.getWidth());
//            c2 = createCharacter("/res/herookl.png") ;
//            c2.setPosition(x,y);
//            
//            System.out.println("c2层的高度"+c2.getHeight());
//            System.out.println("c2层的宽度"+c2.getWidth());
            pMen = createMen("/res/NPC_013.png");
            System.out.println("pman:NPC_013.png");
            pMen.setFrameSequence(hero1);
            
            pMen.setPosition(x,y);
            
        	try {
    			img = Image.createImage("/res/xyymap1.png");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
            b1=createBackGround(mapDepth);
            System.out.println("background:b1");
            b2=createBackGround(mapDepth1);
            System.out.println("background:\b2");
            b3=createBackGround(mapDepth2);
            System.out.println("background:\b3");
//           	effect = createEffect("/res/xyyeffect2.png");
//            System.out.println("xyyeffect1.png");
//            effect.setVisible(false);
            lm.append(b3);
            lm.append(c1);
//            lm.append(c2);     
            lm.append(pMen);
//            lm.append(effect);
            lm.append(b2);
            lm.append(b1);

    }

	private Sprite createMen(String pic) {
		// TODO Auto-generated method stub
		Image img = null ;
	     try
       {
               img = Image.createImage(pic);
       }catch(Exception exp)
       {
               System.out.println(exp);
       }
       return new Sprite(img,32,48)  ;
		
	}
	public void commandAction(Command c,Displayable s)
	{
		Navigator.flow(c.getLabel()) ;
	}            

	private int [][] mapDepth = 
	{
			{ 241, 241, 241, 241, 241, 241, 241, 241, 241, 55, 55, 55, 55, 55, 55, 55, 
				55, 55, 55, 55 },
				{ 241, 241, 134, 241, 241, 241, 56, 58, 58, 57, 57, 57, 55, 55, 55, 55, 
				55, 55, 242, 55 },
				{ 241, 241, 241, 241, 68, 18, 69, 187, 187, 187, 187, 30, 122, 55, 55, 55, 
				55, 55, 242, 55 },
				{ 241, 241, 120, 68, 187, 187, 187, 187, 187, 187, 187, 187, 187, 87, 55, 55, 
				134, 55, 55, 55 },
				{ 241, 262, 93, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 70, 96, 122, 
				55, 55, 55, 55 },
				{ 242, 262, 184, 187, 187, 187, 146, 187, 187, 187, 187, 187, 187, 187, 187, 109, 
				55, 55, 55, 55 },
				{ 275, 262, 158, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 122, 
				123, 55, 55, 55 },
				{ 264, 262, 171, 186, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 32, 
				32, 87, 55, 55 },
				{ 263, 275, 106, 185, 186, 187, 187, 82, 125, 187, 187, 187, 187, 187, 187, 173, 
				32, 187, 87, 55 },
				{ 264, 262, 119, 187, 187, 187, 187, 95, 147, 148, 158, 187, 187, 187, 146, 187, 
				174, 30, 74, 55 },
				{ 242, 262, 145, 187, 187, 187, 187, 159, 160, 161, 172, 187, 187, 187, 187, 187, 
				187, 30, 87, 55 },
				{ 280, 262, 158, 94, 94, 187, 187, 95, 94, 187, 187, 187, 187, 187, 187, 187, 
				32, 175, 136, 55 },
				{ 211, 210, 171, 187, 32, 94, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 
				32, 188, 55, 55 },
				{ 211, 211, 184, 187, 187, 32, 32, 187, 187, 187, 146, 187, 187, 187, 32, 32, 
				175, 197, 55, 55 },
				{ 212, 210, 158, 159, 187, 187, 187, 187, 187, 146, 187, 187, 187, 187, 32, 32, 
				61, 55, 55, 55 },
				{ 55, 55, 171, 172, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 
				74, 55, 55, 55 },
				{ 55, 55, 132, 198, 198, 94, 187, 187, 187, 187, 187, 187, 175, 176, 136, 158, 
				87, 55, 55, 55 },
				{ 55, 55, 55, 242, 210, 197, 186, 32, 32, 32, 187, 187, 188, 54, 55, 134, 
				148, 55, 55, 55 },
				{ 55, 55, 242, 55, 55, 55, 198, 199, 198, 199, 199, 199, 199, 54, 55, 55, 
				55, 55, 55, 55 },
				{ 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 
				55, 55, 55, 55 }
				};

	private int [][] mapDepth1 = {
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 
				76, 0, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 
				76, 0, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				75, 76, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				134, 134, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 75, 76 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 75 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 0, 141, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 153, 154, 155, 156, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 
				0, 0, 0, 0 },
				{ 62, 75, 76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 102, 
				103, 31, 0, 0 },
				{ 62, 75, 76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 115, 
				116, 117, 0, 0 },
				{ 76, 76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 75, 76, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				75, 76, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 51, 52, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 }
				};
				

		private int [][] mapDepth2 = {
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 
					63, 0, 0, 62 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					62, 63, 0, 75 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 62, 63 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 62 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 128, 129, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 140, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 62, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 
					90, 0, 0, 0 },
					{ 0, 62, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 
					0, 104, 0, 0 },
					{ 63, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 62, 63, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					62, 63, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 38, 39, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 }
					};

    public TiledLayer createBackGround(int [][] mapDepth)
    {
    
    	TiledLayer tileLayer=new TiledLayer(cellsx,cellsy,img,tiledx,tiledy);
    	for(int j=0;j<cellsx;j++)
    	{
    		for(int i=0;i<cellsy;i++)
    		{
    			int ii=mapDepth[i][j];
    			tileLayer.setCell(j,i,ii);
    			
    		}
    	}
    	return tileLayer;
    }
    
    private Sprite createCharacter(String pic)
    {
            Image img = null ;
            try
            {
                    img = Image.createImage(pic);
            }catch(Exception exp)
            {
                    System.out.println(exp);
            }
            return new Sprite(img,64,48)  ;
    }
    private Sprite createEffect(String pic)
    {

⌨️ 快捷键说明

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