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

📄 enemytanksprite.java

📁 1) 在实践中理解游戏地图的含义 2) 地图的编辑制作 3) 地图数据的运用 4) 地图的碰撞检测
💻 JAVA
字号:
package tankgame611;
import java.applet.Applet;
import java.awt.Image;
import java.util.Random;
import java.awt.Graphics;

public class EnemyTankSprite extends SpriteDrawing{
  int AppletWidth, AppletHeight;
  int enemyTankWidth, enemyTankHeight;
  int direction;
  Image enemyTankImg[];
  Applet GameApplet;
  Random rand;
  int TimeCount = 0;
  static int speed = 2;
  ShellSprite shell;

  public EnemyTankSprite(Image[] enemyTankImg,
                         int enemyTankStartX,
                         int enemyTankStartY,
                         ShellSprite shell,
                         Applet GameApplet) {
    super(enemyTankImg, enemyTankStartX,
          enemyTankStartY, GameApplet); //调用父类的创建方法
    this.X = enemyTankStartX;
    this.Y = enemyTankStartY;
    this.enemyTankImg = enemyTankImg;
    this.shell = shell;
    this.GameApplet = GameApplet;
    AppletWidth = GameApplet.getWidth();
    AppletHeight = GameApplet.getHeight();
    setVisible(true);
    setMove(true);
    rand = new Random();
  }

  public void updateState(int SpiritDirection){}

  //direction : 0=左; 1=右; 2=上; 3=下;
  public void updatePos(int SpiritDirection){
    enemyTankWidth = enemyTankImg[0].getWidth(GameApplet);
    enemyTankHeight = enemyTankImg[0].getHeight(GameApplet);
    //移动坦克
    switch (SpiritDirection) {
      case 0: //左
        this.X = this.getX() - speed;
        if (this.X <= 1){//设定坦克的左边界
          this.X = 1;
          this.direction = 1;
        }
        if(this.isCollide(TankGame611.TILE_WIDTH,
                          TankGame611.TILE_HEIGHT,
                          TankGame611.MAP_COLS,
                          SpiritDirection)) {
          this.X = this.getCollisionX();
          this.direction = rand.nextInt(20)%4;
        }
        break;
      case 1: //右
        this.X = this.getX() + speed;
        if (this.X >= AppletWidth - DrawGameState.StatusBarWidth -
            enemyTankWidth-1){ //设定坦克的右边界
          this.X = AppletWidth - DrawGameState.StatusBarWidth -
                   enemyTankWidth - 1;
          this.direction = 0;
        }
        if(this.isCollide(TankGame611.TILE_WIDTH,
                          TankGame611.TILE_HEIGHT,
                          TankGame611.MAP_COLS,
                          SpiritDirection)) {
          this.X = this.getCollisionX();
          this.direction = rand.nextInt(20)%4;
        }
        break;
      case 2: //上
        this.Y = this.getY() - speed;
        if (this.Y <= 1){//设定坦克的上边界
          this.Y = 1;
          this.direction = 3;
        }
        if(this.isCollide(TankGame611.TILE_WIDTH,
                          TankGame611.TILE_HEIGHT,
                          TankGame611.MAP_COLS,
                          SpiritDirection)) {
          this.Y = this.getCollisionY();
          this.direction = rand.nextInt(20)%4;
        }
        break;
      case 3: //下
        this.Y = this.getY() + speed;
        if (this.Y >= AppletHeight - enemyTankHeight-1){//设定坦克的下边界
          this.Y = AppletHeight - enemyTankHeight-1;
          this.direction = 2;
        }
        if(this.isCollide(TankGame611.TILE_WIDTH,
                          TankGame611.TILE_HEIGHT,
                          TankGame611.MAP_COLS,
                          SpiritDirection)) {
          this.Y = this.getCollisionY();
          this.direction = rand.nextInt(20)%4;
        }
        break;
    }
    //设定坦克图像的最新位置
    this.setLocation(this.X, this.Y);
    if(shell.visible == false && this.visible == true
       && shell.getLaunchState()){
      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 + (enemyTankHeight - shell.getHeight()) / 2);
        break;
      case 1:
        shell.setLocation(this.X + enemyTankWidth + shell.getWidth(),
                     this.Y + (enemyTankHeight - shell.getHeight()) / 2);
        break;
      case 2:
        shell.setLocation(this.X + (enemyTankHeight - shell.getHeight()) / 2,
                     this.Y - enemyTankWidth / 2);
        break;
      case 3:
        shell.setLocation(this.X + (enemyTankHeight - shell.getHeight()) / 2,
                     this.Y + enemyTankWidth);
        break;
    }
  }

}

⌨️ 快捷键说明

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