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

📄 mycanvas.java

📁 Class Wizard to prepare and some commonly used methods, as well as how to use IO to read out the sys
💻 JAVA
字号:
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;


public class MyCanvas extends GameCanvas implements Runnable
{
	int w,h;
	Graphics g;
	Sprite sp,npc;
	Image img,map;
	int []up={0,1,2};
	int []down={6,7,8};
	int []left={9,10,11};
	int []right={3,4,5};
	int index;
	MyTiledLayer bj,za;
	int [][]data;
	int winx,winy;
	LayerManager lm;
	MyCanvas()
	{
		super(false);
		this.setFullScreenMode(true);
		w=this.getWidth();
		h=this.getHeight();
		g=this.getGraphics();
		try{
			img=Image.createImage("/sp.png");
			map=Image.createImage("/jiaowai.png");
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		sp=new Sprite(img,24,32);
		sp.defineCollisionRectangle(6, 8, 12, 16);
		bj=new MyTiledLayer(20,20,map,16,16,"/1.map");
		za=new MyTiledLayer(20,20,map,16,16,"/2.map");
		lm=new LayerManager();
		lm.append(sp);
		lm.append(za);
		lm.append(bj);
		new Thread(this).start();
	}
	public void moveWin(int dx,int dy)
	{
		winx+=dx;
		winy+=dy;
	}
	public void setWin(int x,int y)
	{
		winx=x;
		winy=y;
	}
	public void drawGame()
	{
		g.setColor(0x000000);
		g.fillRect(0, 0, w, h);
		lm.setViewWindow(winx, winy, w, h);
		lm.paint(g, 0, 0);
		this.flushGraphics();
	}
	public void updataGame()
	{
		updataPlayer();
		updataWin();
	}
	public void updataPlayer()
	{
		if((this.getKeyStates()&UP_PRESSED)!=0&&sp.getY()>=0)
		{
			sp.setFrame(index++%3);
			sp.move(0, -4);
			if(sp.collidesWith(za, false))
				sp.move(0, 4);
		}
		else if((this.getKeyStates()&DOWN_PRESSED)!=0&&sp.getY()<=bj.getHeight()-sp.getHeight())
		{
		
			sp.setFrame(index++%3+6);
			sp.move(0, 4);
			if(sp.collidesWith(za, false))
				sp.move(0, -4);
		}
		else if((this.getKeyStates()&LEFT_PRESSED)!=0&&sp.getX()>=0)
		{
			sp.setFrame(index++%3+9);
			sp.move(-4, 0);
			if(sp.collidesWith(za, false))
				sp.move(4, 0);
		}
		else if((this.getKeyStates()&RIGHT_PRESSED)!=0&&sp.getX()<=bj.getWidth()-sp.getWidth())
		{
		
			sp.setFrame(index++%3+3);
			sp.move(4, 0);
			if(sp.collidesWith(za, false))
				sp.move(-4, 0);
		}
	}
	public void updataWin()
	{
		
		if((this.getKeyStates()&UP_PRESSED)!=0&&winy>0)
		{
			if((sp.getY()-winy)<=h/2)
			{
				moveWin(0,-4);
			}
		}
		else if((this.getKeyStates()&DOWN_PRESSED)!=0&&winy<bj.getHeight()-h)
		{
			if((sp.getY()-winy)>=h/2)
			{
				moveWin(0,4);
			}
			
		}
		else if((this.getKeyStates()&LEFT_PRESSED)!=0&&winx>0)
		{
			if((sp.getX()-winx)<=w/2)
			{
				moveWin(-4,0);
			}
		}
		else if((this.getKeyStates()&RIGHT_PRESSED)!=0&&winx<bj.getWidth()-w)
		{
		
			if((sp.getX()-winx)>w/2)
			{
				moveWin(4,0);
			}
		}
	}
	
	public void run()
	{
		while(true)
		{
			try{
				Thread.sleep(10);
			}catch(Exception ex)
			{
				ex.printStackTrace();
			}
			drawGame();
			updataGame();
			
		}
	}
}

⌨️ 快捷键说明

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