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

📄 abstractsprite.java

📁 这个是由飞机游戏改编过来的坦克游戏。这只是个基础。做好的话。可以做的更像游戏
💻 JAVA
字号:
package tankgame2007;

import java.awt.Graphics;

public abstract class AbstractSprite { //这是Sprite父类
    int X, Y, width, height;
    boolean visible, active;

    abstract public void paintSprite(Graphics g, int SpiritDirection);
    abstract public void paintSprite(Graphics g);
    abstract public void updateState(int SpiritDirection);

    public int getX(){  //获得精灵X坐标值
        return X;
    }

    public int getY(){ //获得精灵Y坐标值
        return Y;
    }

    public void setLocation(int X, int Y) { //设置精灵X,Y坐标值
        this.X = X;
        this.Y = Y;
    }

    public int getWidth(){ //获得精灵的宽
        return width;
    }

    public int getHeight() { //获得精灵的高
        return height;
    }

    public void setSize(int width, int height) { //设置精灵宽高值
        this.width = width;
        this.height = height;
    }

    public boolean canVisible() { //获得精灵是否可见的属性
        return visible;
    }

    public void setVisible(boolean v) {//设置精灵是否可见
        visible = v;
    }

    public boolean canMove() {//获得精灵是否可移动的属性
        return active;
    }

    public void setMove(boolean m) {//设置精灵是否可移动的属性
        active = m;
    }

    //增加的方法:检测碰撞算法1====================================================
    /*
    public boolean isCollided(AbstractSprite target) {
      boolean collided = false;
      if(this.visible == true && target.visible == true){
        //头部碰撞
        if(this.getX() <= (target.getX()+ target.getWidth()) &&
           this.getX() >= target.getX()) {
          if((this.getY()+this.getHeight())>=target.getY()&&
             ((this.getY()+this.getHeight())<=
              (target.getY()+target.getHeight()))){  //左下角碰撞
            collided = true;
          }
          if((this.getY()<=(target.getY() + target.getHeight()))&&
             (this.getY()>=target.getY())){  //左上角碰撞
            collided = true;
          }
        }
        //尾部碰撞
        if(target.getX() <= (this.getX()+ this.getWidth()) &&
           target.getX() >= this.getX()) {
          if((this.getY()+this.getHeight())>=target.getY()&&
             ((this.getY()+this.getHeight())<=
              (target.getY()+target.getHeight()))){  //左下角碰撞
            collided = true;
          }
          if((this.getY()<=(target.getY() + target.getHeight()))&&
             (this.getY()>=target.getY())){  //左上角碰撞
            collided = true;
          }
        }
      }
      return collided;
    }
    */
    //检测碰撞算法2-------------------------------------------------------------
    public boolean isCollided(AbstractSprite target) {
      boolean collided = false;
      if(this.visible == true && target.visible == true){
        int cX=(this.getX()+this.getWidth()/2)-
               (target.getX()+target.getWidth()/2);
        int cW=(this.getWidth()+target.getWidth())/2;
        int cY=(this.getY()+this.getHeight()/2)-
               (target.getY()+target.getHeight()/2);
        int cH=(this.getHeight()+target.getHeight())/2;
        if((java.lang.Math.abs(cX) < java.lang.Math.abs(cW)) &&
           (java.lang.Math.abs(cY) < java.lang.Math.abs(cH))){
            collided = true;
        }
      }
      return collided;
    }
    //====================================================================
}

⌨️ 快捷键说明

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