2020-11-13

贪吃蛇VS坦克大战:

package com.tank;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.List;

/**

  • 炮弹类

  • @author LEON
    */
    public class Missile {
    // 炮弹位置
    private int x;
    private int y;
    // 炮弹速度
    private static final int speed = 10;
    // 炮弹长宽
    private int width = 12;
    private int height = 12;
    //炮弹是否存活
    private boolean isLive = true;
    //炮弹是好是坏
    private boolean isFriend;
    // 坦克实体
    private TankClient tankClient;
    private int direction = 4;
    private boolean isCheat = false;
    //获取系统工具,读取系统文件
    private static Toolkit toolkit = Toolkit.getDefaultToolkit();
    private static Image[] missileImages = {
    toolkit.getImage(Boom.class.getClassLoader().getResource(“img/missile1.gif”)),
    toolkit.getImage(Boom.class.getClassLoader().getResource(“img/missile2.gif”)),
    toolkit.getImage(Boom.class.getClassLoader().getResource(“img/missile3.gif”)),
    toolkit.getImage(Boom.class.getClassLoader().getResource(“img/missile4.gif”))
    };

    /**

    • 画炮弹
    • @param g
      */
      public void drowMissile(Graphics g) {
      if (isLive == false) {
      return;
      }
      if (isCheat) {
      // 设置炮弹颜色
      g.setColor(Color.BLACK);
      // 设置位置和大小
      g.fillOval(x, y, width, height);
      } else {
      switch (direction) {
      case 1:
      g.drawImage(missileImages[0], x, y, null);
      break;
      case 2:
      g.drawImage(missileImages[1], x, y, null);
      break;
      case 3:
      g.drawImage(missileImages[2], x, y, null);
      break;
      case 4:
      g.drawImage(missileImages[3], x, y, null);
      break;
      default:
      break;
      }
      }
      moveMissile();
      }

    /**

    • 移动炮弹,当超过窗口的时候消除子弹
      */
      private void moveMissile() {
      switch (direction) {
      case 1:
      y -= speed;
      break;
      case 2:
      y += speed;
      break;
      case 3:
      x -= speed;
      break;
      case 4:
      x += speed;
      break;
      default:
      break;
      }
      if (x < 0 || x > 1000 || y < 0 || y > 600) {
      this.isLive = false;
      this.tankClient.missiles.remove(this);
      }
      }

    /**

    • 当攻击到坦克时,子弹和坦克都消失
    • @param tanks
      */
      public void hitTank(List tanks) {
      for (int i = 0; i < tanks.size(); i++) {
      Tank tank = tanks.get(i);
      if (getRect().intersects(tank.getRect()) && tank.isLive() && this.isFriend != tank.isFriend()) {
      tank.setLive(false);
      tanks.set(i, tank);
      Boom boom = new Boom(x, y, this.tankClient);
      this.tankClient.booms.add(boom);
      this.tankClient.missiles.remove(this);
      }
      }
      }

    /**

    • 当攻击到墙时
    • @param wall 撞击到墙时的动作
      */
      public void hitWall(Wall wall) {
      if (this.isLive && getRect().intersects(wall.getRect()) && !this.isFriend) {
      this.tankClient.missiles.remove(this);
      }
      }

    /**

    • 敌方坦克攻击到我方时
    • @param tank
      */
      public void hitTank(Tank tank) {
      if (getRect().intersects(tank.getRect()) && tank.isLive() && this.isFriend != tank.isFriend()) {
      tank.setLive(false);
      Boom boom = new Boom(x, y, this.tankClient);
      this.tankClient.booms.add(boom);
      this.tankClient.missiles.remove(this);
      }
      }

    /**

    • 得到子弹的方框
    • @return
      */
      private Rectangle getRect() {
      return new Rectangle(x, y, width, height);
      }

    /**

    • 炮弹是否存活的getter和setter
    • @return
      */
      public boolean isLive() {
      return isLive;
      }

    public void setLive(boolean isLive) {
    this.isLive = isLive;
    }

    public Missile() {
    }

    public Missile(int x, int y, int direction, TankClient tankClient, boolean isFriend) {
    this.x = x;
    this.y = y;
    this.direction = direction;
    this.tankClient = tankClient;
    this.isFriend = isFriend;
    }

    public Missile(int x, int y, int dir, TankClient tankClient, boolean isFriend, int width, int height, boolean isCheat) {
    this.x = x;
    this.y = y;
    this.direction = dir;
    this.tankClient = tankClient;
    this.isFriend = isFriend;
    this.width = width;
    this.height = height;
    this.isCheat = isCheat;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值