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

📄 spritedemo.java

📁 jBuilderX无线应用开发源代码
💻 JAVA
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class SpriteDemo extends MIDlet implements CommandListener {

  private SpriteCanvas gameCanvas=null; 
  private final static Command exit = new Command("Exit", Command.EXIT, 1);

  public void startApp(){
	  if (gameCanvas== null){
		  gameCanvas= new SpriteCanvas();
		  gameCanvas.addCommand(exit);
		  gameCanvas.setCommandListener(this);
	  }
	  gameCanvas.startGame();
	  Display.getDisplay(this).setCurrent(gameCanvas);
  }  

  public void pauseApp(){
	  if (gameCanvas != null){
		  gameCanvas.stopGame();
	  }
  }

  public void destroyApp(boolean unconditional) {
    if (gameCanvas != null){
      gameCanvas.stopGame();
	}
  }

  public void commandAction(Command c, Displayable s){
       if (c == exit) {
			destroyApp(false);
			notifyDestroyed();
		}
  }
}

//该类继承了GameCanvas类,并封装了游戏的运行逻辑
class SpriteCanvas extends GameCanvas implements Runnable{
	//指示游戏是否结束
	private boolean gameOver;	
	//绘制图像缓冲区的Graphics对象
	private Graphics g;
	//游戏中用到的Sprite对象
	private Sprite sprite;
	//Sprite的位置
	private int x,y;
	//Sprite的移动速度
	private int speed=5;
	//屏幕显示区域的宽度和高度
	private int width,height;
	//存储旋转角度的数组
	private int trans[];	
	//当前的旋转方向
	private int direction;	
	//Sprite中每帧图像的宽度和高度
	private static final int SPRIT_FRAME_WIDTH = 45;
	private static final int SPRIT_FRAME_HEIGHT = 18;
	
	public SpriteCanvas (){
		//阻止键盘事件产生
		super(true);
		//获得绘制图像缓冲区的Graphics对象
		g=getGraphics();

		//用图像文件mosquito.png创建Image对象
		Image img=null;
		try{		
			img=Image.createImage("/mosquito.png");	
		}
		catch(java.io.IOException e){
			System.out.println("can not open image file");
		}
		//用Image对象创建sprite对象
		sprite=new Sprite(img,SPRIT_FRAME_WIDTH,SPRIT_FRAME_HEIGHT);

		//把旋转中心设在Sprite的中心
		sprite.defineReferencePixel(sprite.getWidth()/2,sprite.getHeight()/2);
		//为旋转角度数组赋值
		trans=new int[4];
		trans[0]=sprite.TRANS_NONE;
		trans[1]=sprite.TRANS_ROT90;
		trans[2]=sprite.TRANS_ROT180;
		trans[3]=sprite.TRANS_ROT270;
		//初始化旋转方向
		direction=0;
		
		//设置Sprite的初始位置
		x=10;
		y=10;
		//获得屏幕显示区域的宽度和高度
		width=getWidth();
		height=getHeight();	
	}

	//开始运行游戏
	public void startGame(){
		gameOver=false;
		Thread t=new Thread(this);
		t.start();		
	}

	//停止游戏运行
	public void stopGame(){
		gameOver=true;
	}

	//游戏主循环
	public void run(){
	    while(!gameOver){
			//查询键盘状态
			int keyState=this.getKeyStates();

			//如果方向键按下,则移动Sprite,并保证Sprite在屏幕显示区域内
			if((keyState&LEFT_PRESSED)!=0){
				x-=speed;
				if(x<0)
					x=0;
			}
			if((keyState&RIGHT_PRESSED)!=0){
				x+=speed;
				if(x>width-SPRIT_FRAME_WIDTH)
					x=width-SPRIT_FRAME_WIDTH;
			}
			if((keyState&UP_PRESSED)!=0){
				y-=speed;
				if(y<0)
					y=0;
			}
			if((keyState&DOWN_PRESSED)!=0){
				y+=speed;
				if(y>height-SPRIT_FRAME_HEIGHT)
					y=height-SPRIT_FRAME_HEIGHT;
			}

			//如果FIRE键按下,则旋转Sprite
			if((keyState&FIRE_PRESSED)!=0){
				direction=(direction+1)%4;
				sprite.setTransform(trans[direction]);
			}

			//设置Sprite的新位置
			sprite.setPosition(x,y);

			//用背景色填充整个区域
			g.setColor(128,255,255);
			g.fillRect(0,0,width,height);	
			
			//Sprite后移一帧
			sprite.nextFrame();		
			//绘制Sprite
			sprite.paint(g);
			//将缓冲区复制到实际显示屏幕
			flushGraphics();
			
			//延时
			try {
				Thread.sleep(100); 
			}	
			catch (InterruptedException ie){
			}
		}
	}	
}

⌨️ 快捷键说明

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