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

📄 enemy1.java

📁 JVAV手机游戏单机版 连连看
💻 JAVA
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.Random;
public class Enemy1 extends Sprite
{
    private boolean isAlive;
    public boolean isBullet1Alive;
    public boolean isBullet2Alive;
    private boolean canAttack=false;
    private Random random;
    private Image img;
    private int []offsetX = 
    {
        8, 25, 42, 59, 76
    };
    private int offsetY = 12;
    public Sprite bullet1;
    public Sprite bullet2;
    public int energy=4;    
    private int frameWidth, frameHeight;
    private int CanvasWidth, CanvasHeight;
    int i;
    public Enemy1(Image image, int frameWidth, int frameHeight)
    {
        super(image, frameWidth, frameHeight);
        this.frameWidth = frameWidth;
        this.frameHeight = frameHeight;
        defineReferencePixel(frameWidth / 2, frameHeight / 2);
        random = new Random();
        img = RaidenMIDlet.createImage("/bullet2.png");
        bullet1 = new Sprite(img, img.getWidth(), img.getHeight());
        bullet2 = new Sprite(img, img.getWidth(), img.getHeight());
    }
    public void draw(Graphics g)
    {
        if (isAlive)
            paint(g);
        if(isBullet1Alive)bullet1.paint(g);
        if(isBullet2Alive)bullet2.paint(g);

    }
    public void setCanvasSize(int CanvasWidth, int CanvasHeight)
    {
        this.CanvasWidth = CanvasWidth;
        this.CanvasHeight = CanvasHeight;
    }
    public void setAlive(boolean isAlive)
    {
        this.isAlive = isAlive;

        if(isAlive) {setPosition(CanvasWidth / 2, 0);energy = 4;}
    }
    public boolean isAlive()
    {
        return isAlive;
    }
/*    public boolean isBulletAlive()
    {
        return isBulletAlive;
    }*/
    public void tick(int px)
    {
    	  if(isAlive){
        move(0, 2);
        if (getY() > CanvasHeight / 5-frameHeight){
            setPosition(getX(), CanvasHeight / 5-frameHeight);
            canAttack=true;
          }
        if (getX() > CanvasWidth - frameWidth)
            setPosition(CanvasWidth - frameWidth, getY());
        if (getX() < 0)
            setPosition(0, getY());
         if (px < getRefPixelX())
            move( - 1, 0);
        else
            move(1, 0);
        
        if ((random.nextInt() & 0x7fffffff) % 5 == 0 && isBullet1Alive == false&&isBullet2Alive == false&&canAttack==true)
        {   i = (random.nextInt() & 0x7fffffff) % 5;
        	  bullet1.setPosition(getX()+offsetX[i],getY()+offsetY);
            i=i+1;
            if(i>4)i=0;
            //if(i)
            bullet2.setPosition(getX()+offsetX[i],getY()+offsetY);
            isBullet1Alive = true;
            isBullet2Alive = true;
        }
        }
        if (isBullet1Alive)bullet1.move(0, 5);
        if (isBullet2Alive)bullet2.move(0, 5);
            
         
        if(bullet1.getY()>=CanvasHeight)isBullet1Alive=false;
        if(bullet2.getY()>=CanvasHeight)isBullet2Alive=false;

        
    }
    public void init()
    {
    	  isAlive = false;
    	  isBullet1Alive = false;
        isBullet2Alive = false;
        canAttack=false;
        energy=4;
    }

}

⌨️ 快捷键说明

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