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

📄 playerplane.java

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

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


public class PlayerPlane extends Plane {

    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int UP = 3;
    public static final int DOWN = 4;	
    private static final int INITIALIZATION_PLANE_SPEED=6;
    private static final int ACCELERATE_PLANE_SPEED=9;
    private boolean isSpeedUp;
    private int count=0;//加速多少时间
    public int[] right={2,3,4};
    public int[] left={2,1,0};
    public int[] up={2};
    public  Image imgBullet;
    public Vector bulletVector;
    public boolean canAttack;
    private int notAttackTime;
   
    
	public PlayerPlane(Image image, int frameWidth, int frameHeight,int kind) {
		super(image, frameWidth, frameHeight,kind);
		// TODO Auto-generated constructor stub
		imgBullet=ShotPlaneGameCanvas.createImage("/shots.png");
		bullet=new Bullet(this,imgBullet,imgBullet.getWidth(),imgBullet.getHeight(),1);
		bulletVector=new Vector();
		isExplod=false;
	}

	public void setAlive(boolean isAlive) {
		// TODO Auto-generated method stub
		this.isAlive=isAlive;
		if(isAlive){
			setPosition(canvasWidth/2-frameWidth/2, canvasHeight-frameHeight);
		}
	}
	public void fire(int x,int y,int bulletType){
		if (canAttack) {
			switch (bulletType) {
			case Bullet.SMALL:
			case Bullet.MIDDLE:
			case Bullet.BIG:
				bullet = new Bullet(this, imgBullet, imgBullet.getWidth(),
						imgBullet.getHeight(), bulletType);
				bullet.setCanvasSize(super.canvasWidth, super.canvasHeight);
				bulletVector.addElement(bullet);
				bullet.setAlive(x, y);
				break;
			case Bullet.DISPERSE:
				for (int i = 0; i <= 2; i++) {
					bullet = new Bullet(this, imgBullet, imgBullet.getWidth(),
							imgBullet.getHeight(), bulletType);
					bullet.setCanvasSize(super.canvasWidth, super.canvasHeight);
					bullet.playerBulletDirection = i;
					bullet.setAlive(x, y);
					bulletVector.addElement(bullet);
				}
				break;
			}
		}
	}
	public void speedUp(){
		speed=ACCELERATE_PLANE_SPEED;
		count=0;
		isSpeedUp=true;
	}

	public void tick() {
		// TODO Auto-generated method stub
		bullet.tick();
		if(isSpeedUp==true){
			speed=ACCELERATE_PLANE_SPEED;
			count++;
		}
		if(count==50){
			speed=INITIALIZATION_PLANE_SPEED;
			isSpeedUp=false;
			count=0;
		}
		notAttackTime++;
		if(notAttackTime==10){
			canAttack=true;
		}
	}
	public void move(int direction){
		if (direction == UP)
        {
            move(0,  - speed);
            if (getY() < 0)
                setPosition(getX(), 0);

        }
        if (direction == DOWN)
        {
            move(0, speed);
            if (getY() > canvasHeight - frameHeight)
                setPosition(getX(), canvasHeight - frameHeight);

        }
        if (direction == LEFT)
        {
            move( - speed, 0);
            if (getX() < 0)
                setPosition(0, getY());

        }
        if (direction == RIGHT)
        {
            move(speed, 0);
            if (getX() > canvasWidth - frameWidth)
                setPosition(canvasWidth - frameWidth, getY());

        }
	}
	public void draw(Graphics g) {
		if (isAlive) {
			if(!canAttack){
				setVisible(true);
				switch (notAttackTime) {
				case 0:	
				case 1:
				case 4:
				case 5:
				case 8:
				case 9:
					setVisible(false);
					break;
				case 2:
				case 3:
				case 6:
				case 7:
				case 10:
					setVisible(true);
				}
				paint(g);
			} else {
				setVisible(true);
				paint(g);
				g.setClip(0, 0, canvasWidth, canvasHeight);
				bullet.draw(g);
			}
		}
	}
	public void init(){
		isSpeedUp=false;
		speed=INITIALIZATION_PLANE_SPEED;
		count=0;
		setAlive(true);
		bullet.bulletType=Bullet.SMALL;
		setFrameSequence(up);
		setFrame(0);
		canAttack=false;
		notAttackTime=0;
		explodCount=0;
	}
	
}

⌨️ 快捷键说明

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