?? shellsprite.java~10~
字號(hào):
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 x,int y,
int launchDirection,
int speed, //炮彈速度
Applet GameApplet) {
super(0, 0); //調(diào)用父類的創(chuàng)建方法
this.shellImg = shellImg;
this.x=x;
this.y=y;
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);
//System.out.println("X,Y"+X+","+Y);
//移動(dòng)炮彈
switch (launchDirection) {
case 0: //左
if (x < (-shellWidth)){//設(shè)定炮彈的左邊界
setVisible(false); //設(shè)定炮彈Sprite為不可見
setMove(false); //設(shè)定炮彈Sprite為不可移動(dòng)
}
else{
x = this.getX() - speed;
}
break;
case 1: //右
if (x > (AppletWidth + shellWidth)){ //設(shè)定炮彈的右邊界
setVisible(false); //設(shè)定炮彈Sprite為不可見
setMove(false); //設(shè)定炮彈Sprite為不可移動(dòng)
}
else{
x = this.getX() + speed;
}
break;
case 2: //上
if (y < (-shellHeight)){//設(shè)定炮彈的上邊界
setVisible(false); //設(shè)定炮彈Sprite為不可見
setMove(false); //設(shè)定炮彈Sprite為不可移動(dòng)
}
else{
y = this.getY() - speed;
}
break;
case 3: //下
if (y > (AppletHeight + shellHeight)){//設(shè)定炮彈的下邊界
setVisible(false); //設(shè)定炮彈Sprite為不可見
setMove(false); //設(shè)定炮彈Sprite為不可移動(dòng)
}
else{
y = this.getY() + speed;
}
break;
}
//設(shè)定炮彈圖像的最新位置
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);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -