?? plane.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 + -