2020-11-13

贪吃蛇VS坦克大战:

package com.tank;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;

/**

  • 坦克类

  • @author LEON
    /
    public class Tank extends Frame {
    private static final long serialVersionUID = 1L;
    private int width = 50;
    private int height = 50;
    private int x;
    private int y;
    /
    *

    • 方位,当方位为true时表示移动方向,都为false时原地不动
      /
      private static boolean TOP = false;
      private static boolean DOWN = false;
      private static boolean LEFT = false;
      private static boolean RIGHT = false;
      /
      *
    • 方向值,由方位方法赋值,用于给moveTank()传参
      /
      private int direction = 0;
      /
      *
    • 原始方向值
      /
      private int original = 4;
      /
      *
    • 是否是我方坦克
      /
      private boolean isFriend = true;
      private boolean isLive = true;
      private TankClient tankClient;
      /
      *
    • 老的X
      /
      private int old_x;
      /
      *
    • 老的Y
      */
      private int old_y;
      private int step;
      private Random random = new Random();
      private CheatCode cheatCode = new CheatCode();

    /**

    • 获取系统工具,读取系统文件
      */
      private static Toolkit toolkit = Toolkit.getDefaultToolkit();
      private static Image[] tankImages = {
      toolkit.getImage(Boom.class.getClassLoader().getResource(“img/tank1.gif”)),
      toolkit.getImage(Boom.class.getClassLoader().getResource(“img/tank2.gif”)),
      toolkit.getImage(Boom.class.getClassLoader().getResource(“img/tank3.gif”)),
      toolkit.getImage(Boom.class.getClassLoader().getResource(“img/tank4.gif”))
      };

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

    /**

    • 画坦克
      */
      public void drowTank(Graphics g) {
      //如果坦克没有存活,则不再画它
      if (isLive == false) {
      return;
      }
      switch (original) {
      case 1:
      g.drawImage(tankImages[0], x, y, null);
      break;
      case 2:
      g.drawImage(tankImages[1], x, y, null);
      break;
      case 3:
      g.drawImage(tankImages[2], x, y, null);
      break;
      case 4:
      g.drawImage(tankImages[3], x, y, null);
      break;
      default:
      break;
      }
      if (!isFriend) {
      if (step == 0) {
      direction = random.nextInt(4) + 1;
      original = direction;
      step = random.nextInt(50) + 1;
      }
      if (random.nextInt(100) > 95) {
      this.fire(false);
      }
      }
      moveTank();
      step–;
      }

    /**

    • 移动坦克
      */
      public void moveTank() {
      old_x = x;
      old_y = y;
      switch (direction) {
      case 1:
      y -= 5;
      break;
      case 2:
      y += 5;
      break;
      case 3:
      x -= 5;
      break;
      case 4:
      x += 5;
      break;
      default:
      break;
      }
      if (x < 0) {
      x = 0;
      }
      if (x > 950) {
      x = 950;
      }
      if (y > 550) {
      y = 550;
      }
      if (y < 25) {
      y = 25;
      }
      }

    /**

    • 当键盘按下,改变方位值
    • @param keyCode
      */
      public void keyPressed(int keyCode) {
      switch (keyCode) {
      case KeyEvent.VK_J:
      cheatCode.fire(this.x, this.y, this.tankClient, true);
      break;
      case KeyEvent.VK_B:
      cheatCode.bigMissile(x, y, original, tankClient, true);
      break;
      case KeyEvent.VK_K:
      fire(true);
      break;
      case KeyEvent.VK_W:
      TOP = true;
      original = 1;
      break;
      case KeyEvent.VK_A:
      LEFT = true;
      original = 3;
      break;
      case KeyEvent.VK_S:
      DOWN = true;
      original = 2;
      break;
      case KeyEvent.VK_D:
      RIGHT = true;
      original = 4;
      break;
      default:
      break;
      }
      judgeDirection();
      }

    public void keyReleased(int keyCode) {
    switch (keyCode) {
    case KeyEvent.VK_W:
    TOP = false;
    break;
    case KeyEvent.VK_A:
    LEFT = false;
    break;
    case KeyEvent.VK_S:
    DOWN = false;
    break;
    case KeyEvent.VK_D:
    RIGHT = false;
    break;
    default:
    break;
    }
    judgeDirection();
    }

    /**

    • 根据方位值改变direction值
      */
      private void judgeDirection() {
      if (TOP && !DOWN && !LEFT && !RIGHT) {
      direction = 1;
      } else if (!TOP && DOWN && !LEFT && !RIGHT) {
      direction = 2;
      } else if (!TOP && !DOWN && LEFT && !RIGHT) {
      direction = 3;
      } else if (!TOP && !DOWN && !LEFT && RIGHT) {
      direction = 4;
      } else {
      direction = 0;
      }
      }

    /**

    • 开炮!!!
    • @return
      */
      private void fire(boolean isFriend) {
      Missile mis = new Missile(x + 21, y + 21, original, tankClient, isFriend);
      tankClient.missiles.add(mis);
      }

    /**

    • 坦克是否与其他敌方坦克相碰撞
    • 判断:当坦克不等于自身 并且其他坦克方框没有和自身坦克方框重叠 并且 双方都是存活状态,照面退一步
    • @param tanks
      */
      public void hitTanks(List tanks) {
      for (int i = 0; i < tanks.size(); i++) {
      Tank tank = tanks.get(i);
      if (tank != this && tank.getRect().intersects(getRect()) && this.isLive && tank.isLive()) {
      this.x = this.old_x;
      this.y = this.old_y;
      }
      }
      }

    /**

    • 坦克是否与我方坦克相撞
    • @param tank
      */
      public void hitTank(Tank tank) {
      if (tank != this && tank.getRect().intersects(getRect()) && this.isLive && tank.isLive) {
      this.x = this.old_x;
      this.y = this.old_y;
      }
      }

    /**

    • 坦克撞墙
    • @param wall
      */
      public void hitWall(Wall wall) {
      if (isLive && getRect().intersects(wall.getRect())) {
      this.x = this.old_x;
      this.y = this.old_y;
      }
      }

    /**

    • 获取坦克方框
    • @return
      */
      public Rectangle getRect() {
      return new Rectangle(x, y, width, height);
      }

    /**

    • isLive的getter()和setter()
    • @return
      */
      public boolean isLive() {
      return isLive;
      }

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

    public boolean isFriend() {
    return isFriend;
    }

    public void setFriend(boolean isFriend) {
    this.isFriend = isFriend;
    }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值