📄 shellsprite.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;
private int x,y;
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 = 704;
AppletHeight = 576;
setVisible(false);
setMove(false);
}
public void updateState(int launchDirection,
int TileWidth,
int TileHeight,
int mapCols){
shellWidth = shellImg.getWidth(GameApplet);
shellHeight = shellImg.getHeight(GameApplet);
//System.out.println("X,Y"+X+","+Y);
//移动炮弹
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 + -