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

📄 plane.java

📁 飞机游戏源码实例
💻 JAVA
字号:
package planegame;
import javax.microedition.lcdui.*;
/**************************************************
*类功能介绍:控制飞机
**************************************************/
public class plane 
{
	private Image img;           //飞机
	private int width,height;    //屏幕的宽度高度
	private Graphics g;			 //画布
	private static int x=0;      //x轴的位置
	private setPlane mp=null;    //飞机移动线程
	private cortrol ctrl=null;   //主控
	private int bearing;         //0为往左1为往右2为终止移动
	public plane(cortrol ctrl,int width,int height)
	{
		this.g=ctrl.bg;
		this.width=width;
		this.height=height;
		this.ctrl=ctrl;	
		x=width /2;
		try
		{
			img=Image.createImage("/planegame/plane.png");
		}
		catch(Exception e)
		{
		}
		
	}
	/**************************************************
	*功能介绍:将飞机显示在屏幕上
	*输入参数:
	*返回参数:
	**************************************************/
	public void refreshPlane()
	{
		g.drawImage(img,x,height-50,g.TOP|g.LEFT);

	}

	/**************************************************
	功能介绍:当键盘按下时设置飞机位置,调用setPlane以平
	         滑移动
	输入参数:0为左 1为右
	返回参数:
	**************************************************/
	public void movePlane(int bearing)
	{
		mp=new setPlane(bearing);
		mp.start();
	}

	/**************************************************
	*功能介绍:当键盘提起时停止移动
	*输入参数:
	*返回参数:
	**************************************************/
	public void stopMove()
	{
		bearing=2;
	}
	/**************************************************
	*功能介绍:取得飞机当前的x轴的位置
	*输入参数:无
	*返回参数:x轴
	**************************************************/
	public static int  getX()
	{
		return x+10;
	}



    /**************************************************
    *类功能介绍:用于平滑移动飞机,键盘按下时激活,提起时
	失效
    **************************************************/
	public class setPlane extends Thread      
	{
		
		public setPlane(int _bearing)
		{
			bearing=_bearing;
		}
		public void run()
		{
			
			
			while(bearing==0 || bearing==1)
			{
				try
				{
					if (bearing==0)
					{
						if (x >0)
						{
							x=x-2;
						}
						
					}
					else
					{
						if (x+15<width)
						{
							x=x+2;
						}						
					}		
			
					ctrl.repaint();  //重刷屏幕
					
					sleep(1);
				}
				catch(Exception e)
				{
				}
			}
		}
	};
};

⌨️ 快捷键说明

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