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

📄 abstractsprite.java

📁 1) 在实践中理解游戏地图的含义 2) 地图的编辑制作 3) 地图数据的运用 4) 地图的碰撞检测
💻 JAVA
字号:
package tankgame611;

import java.awt.Graphics;

public abstract class AbstractSprite {
    int X,Y;
    int width,height;
    boolean visible,active;
    int collisionX,collisionY;
    abstract public void paintSprite(Graphics g);
    abstract public void paintSprite(Graphics g,int SpiritDirection);
    abstract public void updateState(int SpiritDirection);
    public int getX(){
      return X;
    }
    public int getY(){
      return Y;
    }
    public void setLocation(int X,int 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;
    }

    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;

    }
    public int getCollisionX(){
      return collisionX;
    }
    public int getCollisionY(){
      return collisionY;
    }
    public boolean isCollide(int TileWidth,int TileHeight,int MapCols,int moveDirection){
      int mapRow=(this.Y)/TileHeight;
      int mapCol=(this.X)/TileWidth;
      int mapIndexOfLeftTop=mapRow*MapCols+mapCol;
      mapRow=(this.Y)/TileHeight;
      mapCol=(this.X+this.width)/TileWidth;
      int mapIndexOfRightTop=mapRow*MapCols+mapCol;
      mapRow=(this.Y+this.height)/TileHeight;
      mapCol=(this.X)/TileWidth;
      int mapIndexOfLeftBottom=mapRow*MapCols+mapCol;
      mapRow=(this.Y+this.height)/TileHeight;
      mapCol=(this.X+this.width)/TileWidth;
      int mapIndexOfRightBottom=mapRow*MapCols+mapCol;
      boolean collided=false;
      switch(moveDirection){
        case 0:
          if((GameMap.walls[mapIndexOfLeftTop]>0)||((GameMap.walls[mapIndexOfLeftBottom]>0))){
            if(GameMap.walls[mapIndexOfLeftTop]>0){
              collisionX=(mapIndexOfLeftTop%MapCols)*TileWidth+TileWidth+1;
            }else{
              collisionX=(mapIndexOfLeftBottom%MapCols)*TileWidth+TileWidth+1;
            }
            collided=true;
          }
          break;
          case 1:
            if((GameMap.walls[mapIndexOfRightTop]>0)||((GameMap.walls[mapIndexOfRightBottom]>0))){
              if(GameMap.walls[mapIndexOfRightTop]>0){
                collisionX=(mapIndexOfRightTop%MapCols)*TileWidth-this.getWidth()-1;
              }else{
                collisionX=(mapIndexOfRightBottom%MapCols)*TileWidth-this.getWidth()-1;
              }
              collided=true;
            }
            break;
            case 2:
              if((GameMap.walls[mapIndexOfLeftTop]>0)||((GameMap.walls[mapIndexOfRightTop]>0))){
                if(GameMap.walls[mapIndexOfLeftTop]>0){
                  collisionY=(mapIndexOfLeftTop/MapCols+1)*TileHeight+1;
                }else{
                  collisionY=(mapIndexOfRightTop/MapCols+1)*TileHeight+1;
                }
                collided=true;
              }
              break;
              case 3:
                if((GameMap.walls[mapIndexOfLeftBottom]>0)||((GameMap.walls[mapIndexOfRightBottom]>0))){
                if(GameMap.walls[mapIndexOfLeftBottom]>0){
                  collisionY=(mapIndexOfLeftBottom/MapCols)*TileHeight-this.getHeight()-1;
                }else{
                  collisionY=(mapIndexOfRightBottom/MapCols)*TileHeight-this.getHeight()-1;
                }
                collided=true;
              }
      }
      return collided;
    }

}




⌨️ 快捷键说明

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