欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

J2me游戏梦幻西游——建邺城

程序员文章站 2022-07-09 14:58:53
...
public class Jianyecheng extends GameCanvas implements Runnable{
	
	private Display dis;
	private Graphics g;
	
	private TiledLayer map;
	private Zhujiao zhujiao;
	private LayerManager lm;
	public Jianyecheng(Zhujiao zj,Display dis) {
		super(true);
		this.setFullScreenMode(true);
		this.dis=dis;
		
		g=this.getGraphics();
		lm=new LayerManager();
		
		/********主角初始化***********/
		if(zj!=null){
			this.zhujiao=zj;
		}else{
			Image img=null;
			try {
				img=Image.createImage("/renwuImg/xiaoyao.png");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
			this.zhujiao=new Zhujiao(new Sprite(img,img.getWidth()/4,img.getHeight()/4));
			
		}
		this.zhujiao.setFrame(0);
		this.zhujiao.setPosition(10, 65);
		lm.append(this.zhujiao);
		
		/********地图初始化***********/
		Image img=null;
		try {
			img=Image.createImage("/map/jiangnan.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map=new TiledLayer(1,1,img,img.getWidth(),img.getHeight());
		map.setCell(0, 0, 1);
		lm.append(map);
		
		lm.paint(g, 0, 0);
		this.flushGraphics();
		new Thread(this).start();
		
		//启动地图滚动管理器
		new Thread(new MapContor(lm,map,this.zhujiao,this.getWidth(),this.getHeight())).start();
	}

	public void run() {
		boolean flag=true;
		while(flag){
			int state=this.getKeyStates();
			if((state&this.LEFT_PRESSED)!=0){
				lm.remove(zhujiao);
				zhujiao.run(Zhujiao.LEFT);
				lm.insert(zhujiao, 0);
			}else if((state&this.RIGHT_PRESSED)!=0){
				lm.remove(zhujiao);
				zhujiao.run(Zhujiao.RIGHT);
				lm.insert(zhujiao, 0);
			}
			else if((state&this.UP_PRESSED)!=0){
				zhujiao.run(Zhujiao.UP);
			}else if((state&this.DOWN_PRESSED)!=0){
				zhujiao.run(Zhujiao.DOWN);
			}
			
			lm.paint(g, 0, 0);
			this.flushGraphics();
			try {
				Thread.currentThread().sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}//end run方法
	
	
	

}//end Jianyecheng
相关标签: 游戏 thread UP