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

📄 shellsprite.java

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

import java.applet.Applet;
import java.awt.Image;
import java.awt.*;
import java.applet.*;
public class ShellSprite extends SpriteDrawing{
  private final int StateBarWidth=100;
  private int AppletWidth,AppletHeight;
  private int shellWidth,shellHeight;
  private int launchDirection;
  private Image shellImg;
  private Applet GameApplet;
  private int speed;
  private Explosion explosion;
  private boolean launchNewShell=true;
  private AudioClip shootAudio;
  public ShellSprite(Image shellImg,int launchDirection,int speed,Explosion explosion,Applet GameApplet){
    super(0,0);
    this.shellImg=shellImg;
    this.launchDirection=launchDirection;
    this.GameApplet=GameApplet;
    this.speed=speed;
    AppletWidth=GameApplet.getWidth();
    AppletHeight=GameApplet.getHeight();
    this.explosion=explosion;
    setVisible(false);
    setMove(false);
    shootAudio=GameApplet.getAudioClip(GameApplet.getDocumentBase(),"audio/shoot.wav");
  }
  public void updateState(){
    shellWidth=shellImg.getWidth(GameApplet);
    shellHeight=shellImg.getHeight(GameApplet);
    switch(launchDirection){
      case 0:
        X=this.getX()-speed;
        launchNewShell=false;
        if(X<=1){
          setVisible(false);
          setMove(false);
          X=1;
          launchNewShell=true;
        }
        if(this.isCollide(TankGame611.TILE_WIDTH,TankGame611.TILE_HEIGHT,TankGame611.MAP_COLS,this.getShellDirection())){
          explosion.setLocation(this.getCollisionX(),this.Y);
          launchNewShell=true;
          this.setVisible(false);
          this.setMove(false);
          shootAudio.play();
          explosion.setVisible(true);
        }
        break;
        case 1:
          X=this.getX()+speed;
          launchNewShell=false;
          if(X>(AppletWidth-StateBarWidth-shellWidth-1)){
            setVisible(false);
            setMove(false);
            X=((AppletWidth-StateBarWidth-this.getWidth()-1));
            launchNewShell=true;
          }
          if(this.isCollide(TankGame611.TILE_WIDTH,TankGame611.TILE_HEIGHT,TankGame611.MAP_COLS,this.getShellDirection())){
            explosion.setLocation(this.getCollisionX(), this.Y);
            launchNewShell = true;
            this.setVisible(false);
            this.setMove(false);
            shootAudio.play();
            explosion.setVisible(true);
          }
          break;
          case 2:
            Y=this.getY()-speed;
            launchNewShell=false;
            if(Y<=1){
              setVisible(false);
              setMove(false);
              Y=1;
              launchNewShell=true;
            }
            if(this.isCollide(TankGame611.TILE_WIDTH,TankGame611.TILE_HEIGHT,TankGame611.MAP_COLS,this.getShellDirection())){
              explosion.setLocation(this.X,this.getCollisionY());
              launchNewShell=true;
              this.setVisible(false);
              this.setMove(false);
              shootAudio.play();
              explosion.setVisible(true);
            }
            break;
            case 3:
              Y=this.getY()+speed;
              launchNewShell=false;
              if(Y>(AppletHeight-shellHeight-1)){
              setVisible(false);
              setMove(false);
              Y=(AppletHeight-shellHeight-1);
              launchNewShell=true;
          }
          if(this.isCollide(TankGame611.TILE_WIDTH,TankGame611.TILE_HEIGHT,TankGame611.MAP_COLS,this.getShellDirection())){
            explosion.setLocation(this.X,this.getCollisionY());
            launchNewShell = true;
            this.setVisible(false);
            this.setMove(false);
            shootAudio.play();
            explosion.setVisible(true);
          }

              break;
    }
  }
  public boolean getLaunchState(){
    return launchNewShell;
  }
  public int getShellDirection(){
    return launchDirection;
  }
  public void setShellDirection(int direction){
    this.launchDirection=direction;
  }
  public void paintSprite(Graphics g){
    if(visible==true){
      g.drawImage(shellImg,X,Y,GameApplet);
    }
  }
}

⌨️ 快捷键说明

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