⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enemyplane.java

📁 Java射击小游戏源代码和图片都在里面可以直接玩的!
💻 JAVA
字号:
import java.util.Random;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class EnemyPlane extends Plane {

	public final static int DOWN=0;
	public final static int UP=1;
	public int direction;
	private Random random;
	private int count;//控制子弹
	public int[] rightEnemyPlane={2};
    public int[] leftEnemyPlane={0};
    public int[] downEnemyPlane={1};
	public int[] downBlueEnemy={0};
	public int[] upBlueEnemy={4};
	public int[] downToUpBlueEnemy={0,1,2,3,4};
    public  Image imgBullet;
    public Vector bulletVector;
    private ShotPlaneGameCanvas canvas;
    public Image image;

	public EnemyPlane(Image image, int frameWidth, int frameHeight,int kind,ShotPlaneGameCanvas canvas) {
		super(image, frameWidth, frameHeight,kind);
		// TODO Auto-generated constructor stub
		switch (kind) {
		case Plane.ENEMYPLANE:
			this.image=image;
			if(imgBullet==null)
				imgBullet=ShotPlaneGameCanvas.createImage("/bullet.png");
			bullet=new Bullet(this,imgBullet,imgBullet.getWidth(),imgBullet.getHeight());
			bulletVector=new Vector();
			count=0;
			random=new Random();
			energy=3;
			defineCollisionRectangle(3,8, 26, 16);
			break;
		case Plane.BLUEENEMY:
			this.image=image;
			if(imgBullet==null)
				imgBullet=ShotPlaneGameCanvas.createImage("/bullet.png");
			bullet=new Bullet(this,imgBullet,imgBullet.getWidth(),imgBullet.getHeight());
			bulletVector=new Vector();
			this.canvas=canvas;
			random=new Random();
			energy=2;
			defineCollisionRectangle(2,8, 25, 16);
			direction=DOWN;
			break;
		}
		
		
	}

	public void setAlive(boolean isAlive) {
		// TODO Auto-generated method stub
		this.isAlive=isAlive;
		if(isAlive){
			setPosition(((random.nextInt()&0x7fffffff)%(canvasWidth-frameWidth)),-frameHeight);
		}
	}
	public void setAlive(boolean isAlive,int x,int y){
		this.isAlive=isAlive;
		if(isAlive){
			setPosition(x,y);
		}
	}
	public void fire(){
		bullet=new Bullet(this,imgBullet,imgBullet.getWidth(),imgBullet.getHeight());
		bullet.setCanvasSize(super.canvasWidth, super.canvasHeight);
		bullet.setAlive(this.getX()+frameWidth/2-imgBullet.getWidth()/2, this.getY()+frameHeight);
		bulletVector.addElement(bullet);
	}

	public void tick() {
		// TODO Auto-generated method stub
		switch (kind) {
		case Plane.ENEMYPLANE:
			count++;
			if (count >= 25 && isAlive && getY() > 0) {
				fire();
				count = 0;
			}
			if (getY() < canvasHeight) {
				move(0, speed);
			} else {
				isAlive = false;
			}
			if (energy <= 0) {
				isAlive = false;
			}
			bullet.tick();
			break;
		case Plane.BLUEENEMY:
			if (isAlive) {
				switch (direction) {
				case DOWN:
					if (getY() <= canvasHeight / 2 && getY() >= -frameHeight && getX()>=0 && getX()<=canvasWidth) {
						move(1, speed);
						if (getY()+frameHeight >= canvasHeight / 2) {
							nextFrame();
							direction = UP;
							fire();
						}
					}
					break;
				case UP:
					if (getY() >= -frameHeight&& getX()>=0 && getX()<=canvasWidth) {
						if(getFrame()<4){
							nextFrame();
						}
						move(1, -speed+2);
					}
					break;

				}
				if (energy <= 0) {
					isAlive = false;
				}
			}
//			if(getX()<0||getX()>canvasWidth||getY()<-frameHeight||getY()>canvasHeight){
//				isAlive=false;
//			}
			bullet.tick();
			break;
		}
	}
	
	public void init(){
		speed=4;
		isExplod=false;
		setAlive(true);
		bullet.setAlive(false);
		setFrameSequence(downEnemyPlane);
		setFrame(0);
		explodX=getX();
		explodY=getY();
		explodCount=0;
	}
	public void init(int x,int y){
		speed=8;
		isExplod=false;
		setAlive(true,x,y);
		setFrameSequence(downToUpBlueEnemy);
		setFrame(0);
		explodX=getX();
		explodY=getY();
		explodCount=0;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -