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

📄 enemytanksprite.java

📁 全面运用所学的JAVA理论对所设计的类进一步完善
💻 JAVA
字号:
package tankgame2007;

import java.applet.Applet;
import java.awt.Image;
import java.util.Random;

public class EnemyTankSprite extends ImageSprite{
  int enemyTankWidth, enemyTankHeight;
  int direction;
  int enemyTankX, enemyTankY;
  Image enemyTankImg[];
  Applet GameApplet;
  Random rand;
  int TimeCount = 0;
  static int speed = 2;
  ShellSprite shell;
 int AppletWidth;
 int AppletHeight;
  public EnemyTankSprite(Image[] enemyTankImg,
                         int enemyTankX,
                         int enemyTankY,
                         ShellSprite shell,
                         Applet GameApplet) {
    super(enemyTankImg, enemyTankX, enemyTankY, GameApplet); //调用父类的创建方法
    this.enemyTankX = enemyTankX;
    this.enemyTankY = enemyTankY;
    this.enemyTankImg = enemyTankImg;
    this.shell = shell;
    this.GameApplet = GameApplet;
    AppletWidth=704;
    AppletHeight=GameApplet.getHeight();
    setVisible(true);
    setMove(true);
    rand = new Random();

  }

  //direction : 0=左; 1=右; 2=上; 3=下;
  public void updateState(int SpiritDirection,int Tile_width,int Tile_height,int cols){
    enemyTankWidth = enemyTankImg[0].getWidth(GameApplet);
    enemyTankHeight = enemyTankImg[0].getHeight(GameApplet);
    //移动坦克
    switch (SpiritDirection) {
      case 0: //左
        if (this.X <= 0){//设定坦克的左边界
          this.X = 0;
          this.direction = 1;
        }
        else
        {
          if (this.isCollide(Tile_width, Tile_height, cols, SpiritDirection)) {
            this.X = this.getX();
            this.direction=rand.nextInt(10)%4;
          }
          else
          this.X = this.getX() - speed;
        }
        break;
      case 1: //右
        if (this.X >= AppletWidth - enemyTankWidth){ //设定坦克的右边界
          this.X = AppletWidth - enemyTankWidth;
          this.direction = 0;

        }
        else
        {
          if (this.isCollide(Tile_width, Tile_height, cols, SpiritDirection)) {
            this.X = this.getCollisionX();
             this.direction=rand.nextInt(10)%4;
          }
          else
          this.X= this.getX() + speed;
        }
        break;
      case 2: //上
        if (this.Y <= 0){//设定坦克的上边界
          this.Y = 0;
          this.direction = 3;
        }
        else
        {
          if (this.isCollide(Tile_width, Tile_height, cols, SpiritDirection)) {
            this.Y = this.getCollisionY();
             this.direction=rand.nextInt(10)%4;
          }
          else
          this.Y = this.getY() - speed;
        }
        break;
      case 3: //下
        if (this.Y >= AppletHeight - enemyTankHeight){//设定坦克的下边界
          this.Y= AppletHeight - enemyTankHeight;
          this.direction = 2;
        }
        else{
          if (this.isCollide(Tile_width, Tile_height, cols, SpiritDirection)) {
            this.Y = this.getCollisionY();
             this.direction=rand.nextInt(10)%4;
          }
          else
          this.Y = this.getY() + speed;
        }
        break;
    }
    //设定坦克图像的最新位置
    if(shell.visible == false && this.visible == true){
      shell.setShellDirection(SpiritDirection);
      setShellPos(SpiritDirection);
      shell.setVisible(true);
    }
  }

  public int getTankDirection() {
    return direction;
  }

  public void setTankDirection(int direction) {
    this.direction = direction;
  }

  //调整炮弹的位置
  public void setShellPos(int direction) {
    switch (direction) {
      case 0: //左
        shell.setLocation(this.X - shell.getWidth(),
                          this.Y+ (this.getHeight() - shell.getHeight()) / 2);
        break;
      case 1:
        shell.setLocation(this.X+ this.getWidth()+ shell.getWidth(),
                          this.Y+
                          (this.getHeight() - shell.getHeight()) / 2);
        break;
      case 2:
        shell.setLocation(this.X +
                          (this.getHeight() - shell.getHeight()) / 2,
                          this.Y - this.getWidth() / 2);
        break;
      case 3:
        shell.setLocation(this.X +
                          (this.getHeight() - shell.getHeight()) / 2,
                          this.Y + this.getWidth());
        break;
    }
  }

}

⌨️ 快捷键说明

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