?? enemytanksprite.java~20~
字號:
package tankgame2007;
import java.applet.Applet;
import java.awt.Image;
import java.util.Random;
public class EnemyTankSprite extends ImageSprite{
int enemyTankWidth, enemyTankHeight;
int direction;
int enemyTankX, enemyTankY;
Image enemyTankImg[];
Applet GameApplet;
Random rand;
int TimeCount = 0;
static int speed = 2;
ShellSprite shell;
int AppletWidth;
int AppletHeight;
public EnemyTankSprite(Image[] enemyTankImg,
int enemyTankX,
int enemyTankY,
ShellSprite shell,
Applet GameApplet) {
super(enemyTankImg, enemyTankX, enemyTankY, GameApplet); //調用父類的創建方法
this.enemyTankX = enemyTankX;
this.enemyTankY = enemyTankY;
this.enemyTankImg = enemyTankImg;
this.shell = shell;
this.GameApplet = GameApplet;
AppletWidth=GameApplet.getWidth();
AppletHeight=GameApplet.getHeight();
setVisible(true);
setMove(true);
rand = new Random();
}
//direction : 0=左; 1=右; 2=上; 3=下;
public void updateState(int SpiritDirection,int Tile_width,int Tile_height,int cols){
enemyTankWidth = enemyTankImg[0].getWidth(GameApplet);
enemyTankHeight = enemyTankImg[0].getHeight(GameApplet);
//移動坦克
switch (SpiritDirection) {
case 0: //左
if (this.X <= 0){//設定坦克的左邊界
this.X = 0;
this.direction = 1;
}
else
{
if (this.isCollide(Tile_width, Tile_height, cols, SpiritDirection)) {
this.X = this.getX();
this.direction=rand.nextInt(10)%4;
}
else
this.X = this.getX() - speed;
}
break;
case 1: //右
if (this.X >= AppletWidth - enemyTankWidth){ //設定坦克的右邊界
this.X = AppletWidth - enemyTankWidth;
this.direction = 0;
}
else
{
if (this.isCollide(Tile_width, Tile_height, cols, SpiritDirection)) {
this.X = this.getX();
this.direction=rand.nextInt(10)%4;
}
else
this.X= this.getX() + speed;
}
break;
case 2: //上
if (this.Y <= 0){//設定坦克的上邊界
this.Y = 0;
this.direction = 3;
}
else
{
if (this.isCollide(Tile_width, Tile_height, cols, SpiritDirection)) {
this.X = this.getX();
this.direction=rand.nextInt(10)%4;
}
else
this.Y = this.getY() - speed;
}
break;
case 3: //下
if (this.Y >= AppletHeight - enemyTankHeight){//設定坦克的下邊界
this.Y= AppletHeight - enemyTankHeight;
this.direction = 2;
}
else{
if (this.isCollide(Tile_width, Tile_height, cols, SpiritDirection)) {
this.X = this.getX();
this.direction=rand.nextInt(10)%4;
}
else
this.Y = this.getY() + speed;
}
break;
}
//設定坦克圖像的最新位置
if(shell.visible == false && this.visible == true){
shell.setShellDirection(SpiritDirection);
setShellPos(SpiritDirection);
shell.setVisible(true);
}
}
public int getTankDirection() {
return direction;
}
public void setTankDirection(int direction) {
this.direction = direction;
}
//調整炮彈的位置
public void setShellPos(int direction) {
switch (direction) {
case 0: //左
shell.setLocation(this.X - shell.getWidth(),
this.Y+ (this.getHeight() - shell.getHeight()) / 2);
break;
case 1:
shell.setLocation(this.X+ this.getWidth()+ shell.getWidth(),
this.Y+
(this.getHeight() - shell.getHeight()) / 2);
break;
case 2:
shell.setLocation(this.X +
(this.getHeight() - shell.getHeight()) / 2,
this.Y - this.getWidth() / 2);
break;
case 3:
shell.setLocation(this.X +
(this.getHeight() - shell.getHeight()) / 2,
this.Y + this.getWidth());
break;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -