📄 ufo.java
字号:
import javax.microedition.lcdui.*;
import java.util.Random;
class UFO
extends Sprite {
private Image ufoImg=null, burstImg=null;
private MyShip myship;
private int missileCount=0;
private int direction;
static final int DIRECTION_RIGHT = 0 ;
static final int DIRECTION_LEFT = 1 ;
int id;//飞机类型图片
private Random random = new Random();
public UFO(MyShip myship) {
this.myship = myship;
if (ufoImg == null) {
try {
id = (random.nextInt() >>> 1)%2;
// System.out.println("id = "+id);
switch(id){//装载敌人飞机图片
case 0:
ufoImg = Image.createImage("/ufoImg.png");
break;
case 1:
ufoImg = Image.createImage("/n13.png");
break;
}
burstImg = Image.createImage("/burstImg.png");
}
catch (Exception e) {}
}
width = ufoImg.getWidth();
height = ufoImg.getHeight();
}
void setAlive(boolean isAlive){//飞机是否在界面类和是否还活着
if(isAlive){
this.isHit = false ;
missileCount = 0 ;
tickCount = 0 ;
}
super.setAlive(isAlive);
}
void doMove() {//飞机移动
if(isAlive){
if (isHit()) {
tickCount++;
if (tickCount > 4) {
setAlive(false);
}
}
else {
switch(direction){
case DIRECTION_RIGHT :
x= x + (width/2) + 1 ;
break;
case DIRECTION_LEFT :
x= x - (width/2) - 1 ;
break;
}
}
}
}
void doDraw(Graphics g) {//画飞机或者爆炸
if (isAlive){
if (isHit()) {
g.drawImage(burstImg, x, y, 20);
}
else {
g.drawImage(ufoImg, x, y, 20);
}
}
}
void setDirection(int direction){//飞机出来方向
this.direction = direction ;
}
boolean isDropBome(){//爆炸
if (missileCount <3){
int center = x+(width/2);
if (center >= myship.getX()&¢er <= myship.getX()+myship.getwidth()){
missileCount++;
return true ;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -