九、产生珍宝,用于补血或升级子弹(雷霆战机)

来源:互联网 发布:广电网络河源分公司 编辑:程序博客网 时间:2024/04/30 05:06

GitHub地址:https://github.com/WingedCat/AirWar

珍宝类的创建:

package cn.xpu.hcp.entity;import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.util.Random;import cn.xpu.hcp.game.GameFrame;import cn.xpu.hcp.tools.Constant;import cn.xpu.hcp.tools.GameImage;public class Treasure {    private static Random r = new Random();    int x, y, w, h;    GameFrame gf;     public int type;//珍宝类型    int step = 0;    private boolean live = true;    private static Image[] imgs = new Image[4];    private Image ensureImg;    private static int rx;    private static int ry;    static{        //补血        imgs[0] = GameImage.getImage("resources/t2.bmp");        //子弹升级        imgs[1] = GameImage.getImage("resources/t1.bmp");        imgs[2] = GameImage.getImage("resources/t3.bmp");        imgs[3] = GameImage.getImage("resources/t4.png");        rx = r.nextInt(Constant.GAME_WIDTH);        ry = r.nextInt(Constant.GAME_HEIGHT);    }    //指明血块运动的轨迹,由pos中各个点构成    private int[][] pos = {                      {rx, ry}, {rx+10, ry}, {rx+25, ry-25}, {rx+50, ry-100}, {rx+10, ry-30}, {rx+15, ry-10}, {rx-10, ry-20}                      };    public Treasure(GameFrame gf) {        x = pos[0][0];        y = pos[0][1];        int randIndex = r.nextInt(3)%(3) + 1;        ensureImg = imgs[randIndex];        type=2;        this.gf = gf;        w = h = ensureImg.getWidth(null);    }    public Treasure(int randIndex,GameFrame gf) {        x = pos[0][0];        y = pos[0][1];        randIndex = 0;        this.gf = gf;        ensureImg = imgs[randIndex];        type=1;        w = h = ensureImg.getWidth(null);    }    public void draw(Graphics g) {        if(!live) {            gf.ts.remove(this);            return;        }        g.drawImage(ensureImg, x, y, null);        move();    }    private void move() {        step ++;        if(step == pos.length){            step = 0;        }        x = pos[step][0];        y = pos[step][1];    }    public Rectangle getRect() {        return new Rectangle(x, y, w , h);    }    public boolean isLive() {        return live;    }    public void setLive(boolean live) {        this.live = live;    }}

飞机添加吃珍宝的方法:

public boolean eat(Treasure t) {        if(this.isAlive && t.isLive() && this.getRect().intersects(t.getRect())) {            if(t.type==1){                this.life = 100;            }else{                this.randIndex = r.nextInt(8)%(6) + 3;            }            t.setLive(false);            return true;        }        return false;    }

我将游戏设计成,吃一个补血珍宝补满血,吃一个升级子弹珍宝升级为中级或高级子弹,吃两个升级子弹珍宝后升级为超级炮弹(限时10秒),随后还原为升级前的子弹类型:
在plane类中添加:

int oldIndex;    public long eatStart;    public int count=0;//记录吃了多少个升级子弹的珍宝

修改eat()方法:

public boolean eat(Treasure t) {        if(this.isAlive && t.isLive() && this.getRect().intersects(t.getRect())) {            if(t.type==1){                this.life = 100;            }else{                count++;                if(count==2){                    this.randIndex = r.nextInt(12)%(4) + 9;                    eatStart = System.currentTimeMillis();                }else{                    this.randIndex = r.nextInt(8)%(6) + 3;                    oldIndex = randIndex;                }            }            t.setLive(false);            return true;        }        return false;    }

添加还原方法:

public void back(){        this.randIndex = oldIndex;}

创建珍宝集合:

public List<Treasure> ts = new LinkedList<Treasure>();

在检测线程中添加代码:

@Override    public void run() {        while(true){            if((System.currentTimeMillis()-gf.myplane.eatStart)/1000>=10){                gf.myplane.back();//超级炮弹使用10秒,还原                gf.myplane.count = 0;            }            if(gf.myplane.getLife()<=10&&r.nextInt(40)>=38){                Treasure t = new Treasure(0,gf);//生命值小于10时系统随机刷出补血的珍宝                gf.ts.add(t);            }            if((System.currentTimeMillis()-gf.start)/1000>=10){//一分半后,待打完所有普通敌人,boss开始出现                if((System.currentTimeMillis()-gf.start)/1000%30==0&&r.nextInt(40)>=38){                    Treasure t = new Treasure(gf);//30秒一个节点,随机刷出珍宝                    gf.ts.add(t);                }                if(gf.es.size()==0){                    。。。                    }else{                    。。。            }else{                。。。            }            try {                Thread.sleep(40);            } catch (InterruptedException e) {                e.printStackTrace();            }//使CPU歇会        }    }

绘制珍宝:

for(int i=0;i<ts.size();i++){            Treasure t = ts.get(i);            t.draw(g);            myplane.eat(t);        }