📄 microetanksprite.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
//自定义一个敌方坦克类-------------------------
public class MicroETankSprite extends Sprite implements Runnable
{
private int mDirection;
private int mKX, mKY;
private int x, y;
private int delta3;
private int mLastDelta;
private int efx = 4;//敌方坦克初始移动方向
private int esd = 2;//敌方坦克初始移动量
private boolean firsttry = false;
public boolean elift = true;//敌方坦克活着
public static BulletsSprite ebullets; //BulletsSprite子弹类
private static final int[] kTransformLookup = { Sprite.TRANS_MIRROR_ROT270,
Sprite.TRANS_MIRROR_ROT270, Sprite.TRANS_ROT90, Sprite.TRANS_NONE,
Sprite.TRANS_ROT180 };
//--------------构造函数-------------------------
public MicroETankSprite(Image image, int eWidth, int eHeight)
{
super(image, eWidth, eHeight);
defineReferencePixel(eWidth / 2, eHeight / 2);//自定义本身的参考点
elift = true;
try
{
ebullets = createBullets();//创建敌方子弹
ebullets.setVisible(false);//不可见
}
catch (IOException ioe)
{
}
catch (Exception e)
{
}
}
//------------------子弹的创建---------------------
private BulletsSprite createBullets() throws IOException
{
Image image = Image.createImage("/bullets.png");
return new BulletsSprite(image, 3, 3);
}
//---------------------启动敌方坦克线程-----------------
public void start()
{
Thread t = new Thread(this);
t.start();
}
public void run()
{
while (elift)//当敌方坦克还活着时执行
{
long times_s = System.currentTimeMillis();
forward(esd, efx);// 移动量与移动方向 esd, efx在后面会被传入的参数改变
// System.out.println("敌方坦克移动量 "+esd);
startfire();//
long times_e = System.currentTimeMillis();
long times = times_e - times_s;
if (times > 80)
{
times = 80;
}
try
{
Thread.sleep(80 - times);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}
//-------------控制移动--------------------
private void forward(int delta, int delta2)
{
if (delta2 == 1)
{
fineMove(delta, 0);
}
else if (delta2 == 2)
{
fineMove(delta, 0);
}
else if (delta2 == 3)
{
fineMove(0, delta);
}
else if (delta2 == 4)
{
fineMove(0, delta);
}
setTransform(kTransformLookup[delta2]);
delta3 = delta2;
mLastDelta = delta;
}
private void startfire()
{
if (this != null)
{
ebullets.start(this.getX(), this.getY(), efx, esd * 2, 0);//0表示敌方坦克 此为子弹的移动,包括方向和移动量
}
}
//--------------------碰撞的处理,此方法只处理,不作检测,检测由外部类检测(敌方坦克)-----------------
public void undo()
{
if ((efx >= 4) && firsttry)//
{
efx = 0;
firsttry = false;
efx += 1;
}
else if ((efx >= 4) && (!firsttry))
{
efx = 0;
firsttry = true;
}
else if ((efx == 2) && firsttry)
{
efx = 1;
firsttry = false;
}
else if ((efx == 1) && !firsttry)
{
efx = 3;
firsttry = true;
}
efx += 1;//如果碰到障碍,则另找方向
if (efx == 1)//1为向左
{
esd = -2;//左
}
else if (efx == 2)//2为向右
{
esd = 2;//向右
}
else if (efx == 3)//3为向上
{
esd = -2;//上
}
else if (efx == 4)//4为向下
{
esd = 2;//向下
}
x = x - 3;
y = y - 3;
}
public static void stop()
{
Thread.yield();//敌方坦克暂停一下
}
//------------坦克移动操作---------------------
private void fineMove(int kx, int ky)
{
x = this.getX();
y = this.getY();
x += kx;
y += ky;
if (y >= 240)
{
y = y - 3;
undo();
}
else if (y <= 0)
{
y = y + 3;
undo();
}
if (x <= 0)
{
x = x + 3;
undo();
}
else if (x >= 232)
{
x = x - 3;
undo();
}
this.setPosition(x, y);//重新设置位置
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -