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