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

📄 shellsprite.java

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

import java.applet.Applet;
import java.awt.Image;
import java.awt.*;

public class ShellSprite extends ImageSprite{
  int AppletWidth, AppletHeight;
  int shellWidth, shellHeight;
  int launchDirection;
  Image shellImg;
  Applet GameApplet;
  int speed;

  public ShellSprite(Image shellImg,
                     int launchDirection,
                     int speed,  //炮弹速度
                     Applet GameApplet) {
    super(0, 0); //调用父类的创建方法
    this.shellImg = shellImg;
    this.launchDirection = launchDirection;
    this.GameApplet = GameApplet;
    this.speed = speed;
    AppletWidth = GameApplet.getWidth();
    AppletHeight = GameApplet.getHeight();
    setVisible(false);
    setMove(false);
  }

  public void updateState(){
    shellWidth = shellImg.getWidth(GameApplet);
    shellHeight = shellImg.getHeight(GameApplet);
    //移动炮弹
    switch (launchDirection) {
      case 0: //左
        if (X < (-shellWidth)){//设定炮弹的左边界
          setVisible(false); //设定炮弹Sprite为不可见
          setMove(false); //设定炮弹Sprite为不可移动
        }
        else{
          X = this.getX() - speed;
        }
        break;
      case 1: //右
        if (X > (AppletWidth + shellWidth)){ //设定炮弹的右边界
          setVisible(false); //设定炮弹Sprite为不可见
          setMove(false); //设定炮弹Sprite为不可移动
        }
        else{
          X = this.getX() + speed;
        }
        break;
      case 2: //上
        if (Y < (-shellHeight)){//设定炮弹的上边界
          setVisible(false); //设定炮弹Sprite为不可见
          setMove(false); //设定炮弹Sprite为不可移动
        }
        else{
          Y = this.getY() - speed;
        }
        break;
      case 3: //下
        if (Y > (AppletHeight + shellHeight)){//设定炮弹的下边界
          setVisible(false); //设定炮弹Sprite为不可见
          setMove(false); //设定炮弹Sprite为不可移动
        }
        else{
          Y = this.getY() + speed;
        }
        break;
    }
    //设定炮弹图像的最新位置
    this.setLocation(X, Y);
  }

  public int getShellDirection() {
    return launchDirection;
  }

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

  //覆盖父类方法
  public void paintSprite(Graphics g) { //绘制Sprite
    if (visible == true) {
      g.drawImage(shellImg, X, Y, Game);
    }
  }


}

⌨️ 快捷键说明

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