AMBER677 2020-05-30 11:12 采纳率: 0%
浏览 178

JAVA 飞机大战 子弹每秒打出一个

想用timer实现,但是如下代码bu'tai'dui
Plane类代码如下:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.*;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.neusoft.planewar.client.PlaneWarClient;
import com.neusoft.planewar.constant.Constant;
import com.neusoft.planewar.util.ImageUtil;
import com.neusoft.planewar.util.MusicUtil;

public class Plane extends PlaneWarObject {
public double speed = 10;// 速度
public boolean left, up, right, down;
public int blood;// 血量
public int level;// 等级
public int type;// 等级
public int score = 0;// 积分
public boolean flagFire=true;//发出子弹判定
public static boolean flagPause=false;
public int topscore=getTopscore(); //历史最高分,通过读取文件获得

/**
 * 无参构造
 */
public Plane() {
    super();
}

public Plane(PlaneWarClient pwc, boolean good) {
    this.fire = false;
    this.x = (Constant.GAME_WIDTH - width) / 2;
    this.y = Constant.GAME_HEIGHT - height;
    this.img = ImageUtil.images.get("myPlane_01_01");
    this.width = img.getWidth(null);
    this.height = img.getHeight(null);
    this.pwc = pwc;
    this.good = good;
    this.blood = Constant.MYPLANE_MAX_BOOLD;
    this.level = 1;
    this.type = 1;
}

/**
 * 带参构造
 */

public Plane(int x, int y, Image img, int width, int height) {
    super();
    this.x = x;
    this.y = y;
    this.img = img;
    this.width = width;
    this.height = height;
}

public Plane(int x, int y, String imageName) {
    super();
    this.x = x;
    this.y = y;
    this.img = ImageUtil.images.get(imageName);
    this.width = img.getWidth(null);
    this.height = img.getHeight(null);
}

public Plane(int x, int y, Image img) {
    super();
    this.x = x;
    this.y = y;
    this.img = img;
    this.width = img.getWidth(null);
    this.height = img.getHeight(null);
}

/**
 * 判断我方飞机出界问题
 */
private void outOfBounds() {
    if (x <= 0-width/2)
        x = 0-width/2;
    if (x >= (Constant.GAME_WIDTH - width/2))
        x = Constant.GAME_WIDTH - width/2;
    if (y <= 0)
        y = 0;
    if (y >= (Constant.GAME_HEIGHT - height))
        y = Constant.GAME_HEIGHT - height;
}

/**
 * 是否开火
 */
public boolean fire;

/**
 * 我方飞机发子弹的方法
 */
public void fire() {
    // pwc.musics.add(mu);


    new MusicUtil("fire3").start();
    Missile missile = new Missile(pwc, this.x, this.y, "myPlane_missile_0" + type + "_0" + level, good);
    missile.x += (this.width - missile.width) / 2;
    missile.y -= height;
    pwc.missiles.add(missile);

}

boolean superFire;

/**
 * 超级子弹
 */
public void superFire() {
    if (superFireCount > 0) {
        int num = 24;
        for (int i = 1; i <= num; i++) {
            Missile missile = new Missile(pwc, this.x, this.y, "myPlane_missile_super", 6, good);
            int r = (int) (Math.sqrt(width * width + height * height) / 2);
            int theta = 360 * i / num;
            missile.setTheta(theta);
            missile.x = (int) (missile.x + (width / 2 + r * Math.sin(Math.toRadians(theta)) - missile.width / 2));
            missile.y = (int) (missile.y
                    - ((r * Math.cos(Math.toRadians(theta)) - height / 2 + missile.height / 2)));
            pwc.missiles.add(missile);
        }
        superFireCount--;
    }
    superFire = false;
}

/**
 * 判断是否存活
 */
public boolean live = true;

@Override
public void move() {
    if(!Plane.flagPause) {
    if (left) {
        x -= speed;
    }
    if (right) {
        x += speed;
    }
    if (up) {
        y -= speed;
    }
    if (down) {
        y += speed;
    }
    outOfBounds();
    if (fire)
        fire();
    if (superFire)
        superFire();
}
}

@Override
public void draw(Graphics g) {
    img = ImageUtil.images.get("myPlane_0" + type + "_0" + level);
    if (blood <= 0 && live) {
        live = false;
        Explode ex = new Explode(pwc, x, y);
        ex.x += (width - ex.width) / 2;
        ex.y += (height - ex.height) / 2;
        pwc.explodes.add(ex);
        pwc.enemyPlanes.clear();
        pwc.missiles.clear();
        pwc.items.clear();

    }

    if (live) {
        g.drawImage(img, x, y, null);
        move();
    }
    drawBloodAndScore(g);
}

/**
 * 画血条和积分
 *
 * @param g
 */
private void drawBloodAndScore(Graphics g) {
    Image bloodImg = ImageUtil.images.get("myBlood");
    Image blood_blank = ImageUtil.images.get("myBlood_blank");
    Image scoreImg = ImageUtil.images.get("score");
    Image topscoreImg = ImageUtil.images.get("topscore");
    int i = 0;
    g.drawImage(bloodImg, 10, 40, null);
    int num = (int) (((double) ((bloodImg.getWidth(null)) - 56) / (Constant.MYPLANE_MAX_BOOLD))
            * (Constant.MYPLANE_MAX_BOOLD - blood) / blood_blank.getWidth(null));
    for (int j = 0; j < num; j++) {
        g.drawImage(blood_blank, 10 + bloodImg.getWidth(null) - blood_blank.getWidth(null) * (j + 1), 40 + 14,
                null);
    }
    // 画积分
    g.drawImage(ImageUtil.images.get("score"), 10, 40 + bloodImg.getHeight(null) + 12, null);
    g.setFont(new Font("微软雅黑", Font.BOLD, 40));
    g.setColor(Color.WHITE);
    g.drawString(score + "", 10 + scoreImg.getWidth(null) + 10, 40 + bloodImg.getHeight(null) + 50);
    //画最高积分
    g.drawImage(ImageUtil.images.get("topscore"), bloodImg.getWidth(null)+30, topscoreImg.getHeight(null), null);
    g.setFont(new Font("微软雅黑", Font.BOLD, 30));
    g.setColor(Color.BLUE);
    g.drawString(getTopscore()+ "", scoreImg.getWidth(null) + 150, topscoreImg.getHeight(null)+75);
    //画暂停提示
    g.setFont(new Font("微软雅黑",Font.BOLD,20));
    g.setColor(Color.WHITE);
    g.drawString("按P键暂停", 400, 110);



    if(flagPause) {
        g.setFont(new Font("微软雅黑",Font.BOLD,80));
        g.setColor(Color.RED);
        g.drawString("游戏暂停", 100, 400);

    }//游戏暂停
}

/**
 * 大招剩余次数
 */
public int superFireCount = 0;

/**
 * 按下键盘的方法
 *
 * @param e
 * @throws InterruptedException 
 */
public void keyPressed(KeyEvent e) {
    Timer timer = new Timer();
    switch (e.getKeyCode()) {
    case KeyEvent.VK_A:
        left = true;
        break;
    case KeyEvent.VK_S:
        down = true;
        break;
    case KeyEvent.VK_D:
        right = true;
        break;
    case KeyEvent.VK_W:
        up = true;
        break;
    case KeyEvent.VK_J:// 发子弹

        superFire = false;

        fire = true;

        timer.schedule(new TimerTask() {
              public void run() {
                fire=false;

              }
            },1000);




        break;
    case KeyEvent.VK_SPACE:// 发超级子弹
        fire = false;
        superFire = true;
        break;
    case KeyEvent.VK_P:
        if(!flagPause) {

        flagPause =true;
        }else {

            flagPause=false;
        }
        break;
    }
}

/**
 * 松开键盘的方法
 *
 * @param e
 *            键盘事件
 */
public void keyReleased(KeyEvent e) {
    switch (e.getKeyCode()) {
    case KeyEvent.VK_A:
        left = false;
        break;
    case KeyEvent.VK_S:
        down = false;
        break;
    case KeyEvent.VK_D:
        right = false;
        break;
    case KeyEvent.VK_W:
        up = false;
        break;
    case KeyEvent.VK_J:// 发子弹
        fire = false;
        break;
    }
}
/**
 * 从文件中读取最高分
 * @return
 */
public int getTopscore(){
    int top=readFile();
    return top;
}

/**
 * 更改最高分
 */
public void writeTopscore(){
    if(score>topscore){
        writeFile();
    }
}

/**
 * 读取文件内容操作
 * @return
 */
public int readFile(){
    File file = new File("TopScore.txt");
    //如果文件不存在,文件输出流会自动创建文件
    if (!file.exists()) {
        try {
            file.createNewFile();
            String a = String.valueOf(0);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            bw.write(a);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //读取文件
    int topscorenow=0;
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String topscorenow1 =br.readLine();
        if(topscorenow1!=null) {
            topscorenow = Integer.parseInt(topscorenow1);
        }
        br.close();

    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return topscorenow;
}
public void writeFile(){
    File file = new File("TopScore.txt");
    //如果文件不存在,文件输出流会自动创建文件
    if (!file.exists()) {
        try {
            file.createNewFile();
            String a = String.valueOf(0);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            bw.write(a);//向文件写入初始最高分
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //写文件
    try {
        String a = String.valueOf(score);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(a);//向文件写入最高分
        bw.close();//关闭流

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-09-13 23:00
    关注
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)