📄 alien.java
字号:
import javax.microedition.lcdui.*;
import java.util.Vector;
public class Alien
{
Image[] imageFrames;
Image image;
int imageWidth;
int imageHeight;
int frameWidth;
int frameHeight;
int canvasWidth;
int canvasHeight;
protected static final String IMAGE_NAME="alien";
protected static final int RAW_FRAMES=1;
protected int frame=0;
protected static final int FRAMES=2;
protected static final int START_FRAME=0;
protected static final int END_FRAME=1;
protected int xPos=0;
protected int yPos=0;
protected static int currentAlien=0;
protected static int rows=2;
protected static int cols=3;
protected static int colOffset=3;
protected static int rowOffset=3;
protected static int yOffset=0;
protected static VaderCanvas gameCanvas;
protected static VaderManager layerManager;
protected static boolean direction=true;
protected static boolean isEnd=true;
public Alien(int canvasWidth, int canvasHeight)
{
imageFrames=VaderManager.getFrames(IMAGE_NAME, RAW_FRAMES);
this.canvasHeight=canvasHeight;
this.canvasWidth=canvasWidth;
imageWidth=imageFrames[0].getWidth();
imageHeight=imageFrames[0].getHeight();
frameWidth=imageWidth/FRAMES;
frameHeight=imageHeight;
createImage();
setSpriteImage();
gameCanvas=layerManager.getGameCanvas();
}
public int getHeight() {return imageHeight;}
public int getWidth() {return imageWidth;}
public int getFrameHeight() {return frameHeight;}
public int getFrameWidth() {return frameWidth;}
public int getCanvasHeight() {return canvasHeight;}
public int getCanvasWidth() {return canvasWidth;}
public Image getImage() {return image;}
private void setX(int x) {xPos=x;}
private void setY(int y) {yPos=y;}
public int getX() {return xPos;}
public int getY() {return yPos;}
public static void setDirection(boolean dir) {direction=dir;}
public static void setEnd(boolean end) {isEnd=end;}
public static int getRows() {return rows;}
public static int getCols() {return cols;}
public static void setCols(int c) {cols=c;}
public static void setRows(int r) {rows=r;}
public static void setLayerManager(VaderManager manager) {layerManager=manager;}
public void setPosition(int x, int y)
{
setX(x);
setY(y);
}
private void move(int x, int y)
{
setX(getX()+x);
setY(getY()+y);
}
public void backwards()
{
move(-2, 0);
}
public void forwards()
{
move(2, 0);
}
public void setFrame()
{
if(++frame>=FRAMES)
{
frame=0;
}
}
private void setDirection()
{
if((getX()+getFrameWidth())>=canvasWidth)
{
setEnd(false);
}
else if(getX()<0)
{
setEnd(true);
}
}
private void setYOffset()
{
yOffset+=3;
//setPosition(getX(), getY()+yOffset);
//move(0,yOffset);
}
public void move()
{
if(direction)
{
forwards();
}
else
{
backwards();
}
}
public void tick()
{
currentAlien++;
setFrame();
move();
int aCount=layerManager.vtrAliens.size();
if(currentAlien>=aCount)
{
currentAlien=0;
setDirection(isEnd);
}
int bCount=layerManager.vtrBullets.size();
Bullet bullet;
for (int b=0;b<bCount;b++)
{
bullet=(Bullet)layerManager.vtrBullets.elementAt(b);
if(collidesWith(bullet))
{
layerManager.removeLayer(this);
layerManager.removeLayer(bullet);
layerManager.setScore(layerManager.getScore()+10);
if(--bCount<=0)
{
break;
}
}
}
setDirection();
}
public void draw()
{
Graphics g=layerManager.getGraphics();
if(imageFrames[0]!=null)
{
g.setClip(xPos,yPos,frameWidth,frameHeight);
g.drawImage(imageFrames[0],xPos-(frameWidth*frame),yPos,g.LEFT | g.TOP);
g.setClip(0,0,getCanvasWidth(),getCanvasHeight());
}
}
public boolean collidesWith(Bullet bullet)
{
boolean isCollision=false;
if((bullet.getX()>getX() && bullet.getY()>getY())
&& ((bullet.getX()+bullet.getWidth())<(getX()+getFrameWidth())
&& (bullet.getY()+bullet.getHeight())<getY()+getFrameHeight()))
{
isCollision=true;
}
return isCollision;
}
private void createImage()
{
// allocate memory to store the onscreen background image.
// assuming all tiles are the same size, make the buffer
// the screen size plus one row and column.
image=Image.createImage(getFrameWidth(), getFrameHeight());
}
private void setSpriteImage()
{
Graphics g = image.getGraphics();
g.drawImage(imageFrames[0],0,0,g.LEFT | g.TOP);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -