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

📄 boss.java

📁 手机RPG游戏
💻 JAVA
字号:
package obj;
import javax.microedition.lcdui.*;
import java.util.Vector;
import main.*;
import javax.microedition.media.*;
import java.io.*;
public class Boss {


  private Image bossImg;
  public Vector bs;
  private  int x;//boss的起坐标
  private  int y;
  private  int d;//Boss的移动量
  private  int space;//距边界的距离
  private  int imgw;
  private  int imgh;
  private  int bw;//Boss闪电的宽度
  private  int bwx;//Boss闪电的x坐标
  private  int showlevin;//开始出闪电的点
  private  int area;//闪电的范围
  private  int w;
  private  int h;
  private  int id;//图片的号
  private  int max;//子弹的最大数
  private  int natality;//产生子弹的间隔
  private  int k;//计数器
  private  int speed;

int tw=0;//用来做碰撞检测的。
int th=0;
int bossHp;
int ss=Cntrl.load.bossScore;//打破Boss得分。
private boolean bossalive=false;
public boolean isdrawLevin=false;//画闪电的状态
private int levin_x;
private int levin_w;
public Boss(int boardw,int boardh){//构造函数
    bs=new Vector();
    w=boardw;
    h=boardh;
    this.bossInit();

}
public void bossInit(){

  max=Cntrl.load.bossBulletMax;//Boss产生子弹的最大数
  id=0;//图片的号
  //id=Cntrl.load.pass;//图片的号
  d=Cntrl.load.d;//Boss的移动量
  area=Cntrl.load.area;//闪电的范围
  space=Cntrl.load.space;//距边界的距离
  speed=Cntrl.load.bossBulletSpeed;//子弹的速度。
  natality=Cntrl.load.bossBulletNatality;//产生子弹的间隔
  bossImg=Image.createImage(Cntrl.load.bossImg[id]);

  x=w/2;
  y=space;
  imgw=bossImg.getWidth();
  imgh=bossImg.getHeight();
  bw=2*(Cntrl.load.pass+2);
  bw=bw>imgw?imgw-2:bw;//防止闪电的宽度太大。
  bwx=imgw/2-bw/2;
  showlevin=(w-imgw)/3;
  bossHp=Cntrl.load.bossHp;

  }
  private void drawLevin(Graphics g){//画闪电

      if((x<(showlevin+area)&&x>(showlevin-area))|(x<2*showlevin+area&&x>2*showlevin-area))
          { isdrawLevin=true;//画闪电的状态,在碰撞中用到.
            g.setColor(0x0000ff);
            g.fillRect(x+bwx,imgh+y,bw,h-imgh-y);
            g.setColor(0xffffff);
            levin_x=x+bwx+bw/4;
            levin_w=bw/2;
            g.fillRect(levin_x,imgh+y,levin_w,h-imgh-y);//闪电的宽度与关数有关.
            Cntrl.midt.bossBullet1Mid_play=true;
          }//end if
        else {
          isdrawLevin=false;
        }
    }
////////////////
public void drawself(Graphics g,boolean isShow,Bullets mybs){
   if(isShow)
    { drawBoss(g,mybs);//画Boss
      drawLevin(g);//画闪电
      drawBullets(g);
    }
  }
///////////////////////
private void makeBullet(){//增加子弹
   k++;
   if(k%natality==0){
     k=0;
     add(new BossBullet(x+bwx,imgh+y,speed,w,h));
   }//end if
   }//end addAerolite

private void drawBullets(Graphics g){//画子弹
    makeBullet();
    move();
  for(int i=0;i<bs.size();i++){
    BossBullet bossBullet=(BossBullet)bs.elementAt(i);
    bossBullet.DrawSelf(g);
  }
}
private void move(){//子弹的移动
   for(int i=0;i<bs.size();i++){
     BossBullet bossBullet=(BossBullet)bs.elementAt(i);
     bossBullet.Move();
   }
   for(int i=0;i<bs.size();i++){
     BossBullet bossBullet=(BossBullet)bs.elementAt(i);
     if(!bossBullet.getAlive()) remove(bossBullet);
   }
 }

private void remove(BossBullet bossBullet){//删除子弹
    bs.removeElement(bossBullet);
  }

private void add(BossBullet bossBullet){//加子弹
if(bs.size()>max)return;
bs.addElement(bossBullet);
Cntrl.midt.bossBullet0Mid_play=true;//******************
}

private void drawBoss(Graphics g,Bullets mybs){//画Boss
    if(x>=w-bossImg.getWidth()) {d=-d;}//边界检测
    if(x<=0) {d=-d;}
    x+=d;
    g.drawImage(bossImg,x,y,Graphics.LEFT|Graphics.TOP);//画boss
    this.hit(mybs);
    }


public int getLevinXpos(){//闪电的X坐标.
  return x+bwx+1;
  }
public int getLevinW(){//闪电的宽度.
  return bw/2;
  }
public boolean getAlive(){
    return bossalive;
}

//////////////////////////
  public void bossOver(){
    if(this.bossHp<=0){ bossDie();}
  }
private void bossDie(){
            Cntrl.gameBoard.myPlan.score+=ss;//加分。
            Cntrl.gameBoard.explodes.Add(new Explode(x,y));//加入爆炸效果
            this.bossalive=false;
            Cntrl.gameBoard.enemys.showBoss=false;
            bs.removeAllElements();//清空所有子弹。
            Cntrl.load.pass++;
           // this.isdrawLevin=false;//画闪电的状态
  }
////////////////////////////////////
             public void hit(Bullets mybs){//子弹和Boss的碰撞检测。
                 for(int i=0;i<mybs.bs.size();i++){
                     Bullet b=(Bullet)mybs.bs.elementAt(i);//取出每个子弹。
                     tw=x>b.getXPos()?b.imgW:this.imgw;
                     th=y>b.getYPos()?b.imgH:this.imgh;
                 if(tw>Math.abs(x-b.getXPos())       //检测是否相碰。
                    && th>Math.abs(y-b.getYPos()) )
                   {
                     if(this.bossHp<=0)
                        { bossDie();

                        }
                     else
                        {bossHp--;}//子弹打中bossHp次才消失。
                //     Cntrl.gameBoard.bexplode.Add(new BulletExplode(x,y));//加入子弹的爆炸效果.
                Cntrl.gameBoard.bexplode.Add(new BulletExplode(x+imgw/2,y+imgh/2));
                                                    //加入子弹的爆炸效果.传的是图片中心的坐标
                     mybs.bs.removeElement(b);     //子弹碰后就删除。
                   }
               }
             }
  public void setBossalive(boolean bossalive) {
    this.bossalive = bossalive;
  }
  public int getX() {
    return x;
  }
  public int getY() {
    return y;
  }
  public int getImgw() {
    return imgw;
  }
  public int getImgh() {
    return imgh;
  }
////////////////////////

}

//////////////////////////////////////////////////////////////////////////
/////////Boss子弹类.
class BossBullet{
    private int xSpeed=0;//速度
    private int ySpeed=0;
    private int xPos=0;//坐标
    private int yPos=0;
    public boolean alive=true;
    private int screenW=0;//屏幕的宽高
    private int screenH=0;
    private Image img;
    private int imgId;
    public int imgW;
    public int imgH;
    private int myPlanx;//玩家飞机的坐标
    private int myPlany;
    private int temp=0;
public BossBullet(int xp,int yp,int ys,int sw,int sh) {
    xPos=xp;//坐标
    yPos=yp;
    xSpeed=0;//速度
    ySpeed=ys;
    screenW=sw;////屏幕的宽高
    screenH=sh;
    imgId=Cntrl.load.bossBulletId;
    img=Cntrl.load.bossbulletImg[imgId];//图片
    imgW=img.getWidth();
    imgH=img.getHeight();
    myPlanx=Cntrl.gameBoard.myPlan.x;
    myPlany=Cntrl.gameBoard.myPlan.y;
}
public void DrawSelf(Graphics g){
    g.drawImage(img,xPos-imgW/2,yPos-imgH/2,Graphics.LEFT|Graphics.TOP);
}
public void Move(){

    xPos+=get_xSpeed();
    yPos+=ySpeed;
}
public boolean getAlive(){
  return alive&&xPos>-imgW&&xPos<screenW+(xPos)&&yPos>0&&yPos<screenH;
}
private int get_xSpeed(){//算出子弹的横向速度
  if(myPlany-yPos>20)//为了使分母不小于0
  {
          int n=1000*ySpeed*(xPos-myPlanx)/(yPos-myPlany);
            n=n>5000?5000:n;//为了不超过int 的最大值,和为了使速度不过大
            n=n<-5000?-5000:n;
            n=n+temp;
            if(Math.abs(n)>1000)
            {xSpeed=n/1000;
            temp=n-xSpeed*1000;
            }
            else{temp=temp+n;}
  }
          return xSpeed;
}

        public int getXPos() {
          return xPos;
        }
        public int getYPos() {
          return yPos;
        }



}



⌨️ 快捷键说明

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