📄 shrimpplayer.java
字号:
import javax.microedition.lcdui.game.Layer;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
public class ShrimpPlayer extends Sprite
{
public final static int Shrimp_UP=0;
public final static int Shrimp_DOWN=1;
public final static int Shrimp_LEFT=3;
public final static int Shrimp_RIGHT=2;
private LayerManager lm;
int dir=Shrimp_UP;
private int step=16;
private int[][] Shrimpapp=
{
{0,1},
{2,3},
{4,5},
{6,7}
};
public ShrimpPlayer(LayerManager lm)
{
super(ShrimpImage.shrimp,16,16);
this.lm=lm;
this.changeDir(dir);
new TankScroll().start();
}
public void setLayer(LayerManager lm)
{
this.lm=lm;
}
class TankScroll extends Thread
{
public void run()
{
while(true)
{
ShrimpPlayer.this.nextFrame();
try
{
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public void changeDir(int dir)
{
this.dir=dir;
switch(dir)
{
case Shrimp_UP:
this.setFrameSequence(Shrimpapp[0]);
break;
case Shrimp_DOWN:
this.setFrameSequence(Shrimpapp[1]);
break;
case Shrimp_LEFT:
this.setFrameSequence(Shrimpapp[3]);
break;
case Shrimp_RIGHT:
this.setFrameSequence(Shrimpapp[2]);
break;
}
}
public void move(LayerManager lm)
{
switch(dir)
{
case Shrimp_UP:
this.move(0,-step);
break;
case Shrimp_DOWN:
this.move(0,step);
break;
case Shrimp_LEFT:
this.move(-step,0);
break;
case Shrimp_RIGHT:
this.move(step,0);
break;
}
if(isTouch(lm))
{
moveR();
}
}
public void moveR()
{
switch(dir)
{
case Shrimp_UP:
this.move(0,step);
break;
case Shrimp_DOWN:
this.move(0,-step);
break;
case Shrimp_LEFT:
this.move(step,0);
break;
case Shrimp_RIGHT:
this.move(-step,0);
break;
}
}
public boolean isTouch(LayerManager lm)
{
boolean re=false;
for(int i=0;i<lm.getSize();i++)
{
{
Layer ly=lm.getLayerAt(i);
if(ly instanceof ShrimpLayer)
{
ShrimpLayer sl = (ShrimpLayer)ly;
if(sl.getName().equals("Wall"))
{
if(this.collidesWith(sl,false))
{
re=true;
break;
}
}
}
}
}
return re;
}
public boolean isDead(LayerManager lm)
{
boolean re=false;
for(int i=0;i<lm.getSize();i++)
{
{
Layer ly=lm.getLayerAt(i);
if(ly instanceof ShrimpLayer)
{
ShrimpLayer sl = (ShrimpLayer)ly;
if(sl.getName().equals("Lava"))
{
if(this.collidesWith(sl,false))
{
re=true;
break;
}
}
}
}
}
return re;
}
public boolean ishome(LayerManager lm)
{
boolean re=false;
for(int i=0;i<lm.getSize();i++)
{
{
Layer ly=lm.getLayerAt(i);
if(ly instanceof ShrimpLayer)
{
ShrimpLayer sl = (ShrimpLayer)ly;
if(sl.getName().equals("Home"))
{
if(this.collidesWith(sl,false))
{
re=true;
break;
}
}
}
}
}
return re;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -