?? enemytanksprite.java
字號:
package tankgame611;
import java.applet.Applet;
import java.awt.Image;
import java.util.Random;
import java.awt.Graphics;
public class EnemyTankSprite extends SpriteDrawing{
int AppletWidth, AppletHeight;
int enemyTankWidth, enemyTankHeight;
int direction;
Image enemyTankImg[];
Applet GameApplet;
Random rand;
int TimeCount = 0;
static int speed = 2;
ShellSprite shell;
public EnemyTankSprite(Image[] enemyTankImg,
int enemyTankStartX,
int enemyTankStartY,
ShellSprite shell,
Applet GameApplet) {
super(enemyTankImg, enemyTankStartX,
enemyTankStartY, GameApplet); //調用父類的創建方法
this.X = enemyTankStartX;
this.Y = enemyTankStartY;
this.enemyTankImg = enemyTankImg;
this.shell = shell;
this.GameApplet = GameApplet;
AppletWidth = GameApplet.getWidth();
AppletHeight = GameApplet.getHeight();
setVisible(true);
setMove(true);
rand = new Random();
}
public void updateState(int SpiritDirection){}
//direction : 0=左; 1=右; 2=上; 3=下;
public void updatePos(int SpiritDirection){
enemyTankWidth = enemyTankImg[0].getWidth(GameApplet);
enemyTankHeight = enemyTankImg[0].getHeight(GameApplet);
//移動坦克
switch (SpiritDirection) {
case 0: //左
this.X = this.getX() - speed;
if (this.X <= 1){//設定坦克的左邊界
this.X = 1;
this.direction = 1;
}
if(this.isCollide(TankGame611.TILE_WIDTH,
TankGame611.TILE_HEIGHT,
TankGame611.MAP_COLS,
SpiritDirection)) {
this.X = this.getCollisionX();
this.direction = rand.nextInt(20)%4;
}
break;
case 1: //右
this.X = this.getX() + speed;
if (this.X >= AppletWidth - DrawGameState.StatusBarWidth -
enemyTankWidth-1){ //設定坦克的右邊界
this.X = AppletWidth - DrawGameState.StatusBarWidth -
enemyTankWidth - 1;
this.direction = 0;
}
if(this.isCollide(TankGame611.TILE_WIDTH,
TankGame611.TILE_HEIGHT,
TankGame611.MAP_COLS,
SpiritDirection)) {
this.X = this.getCollisionX();
this.direction = rand.nextInt(20)%4;
}
break;
case 2: //上
this.Y = this.getY() - speed;
if (this.Y <= 1){//設定坦克的上邊界
this.Y = 1;
this.direction = 3;
}
if(this.isCollide(TankGame611.TILE_WIDTH,
TankGame611.TILE_HEIGHT,
TankGame611.MAP_COLS,
SpiritDirection)) {
this.Y = this.getCollisionY();
this.direction = rand.nextInt(20)%4;
}
break;
case 3: //下
this.Y = this.getY() + speed;
if (this.Y >= AppletHeight - enemyTankHeight-1){//設定坦克的下邊界
this.Y = AppletHeight - enemyTankHeight-1;
this.direction = 2;
}
if(this.isCollide(TankGame611.TILE_WIDTH,
TankGame611.TILE_HEIGHT,
TankGame611.MAP_COLS,
SpiritDirection)) {
this.Y = this.getCollisionY();
this.direction = rand.nextInt(20)%4;
}
break;
}
//設定坦克圖像的最新位置
this.setLocation(this.X, this.Y);
if(shell.visible == false && this.visible == true
&& shell.getLaunchState()){
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 + (enemyTankHeight - shell.getHeight()) / 2);
break;
case 1:
shell.setLocation(this.X + enemyTankWidth + shell.getWidth(),
this.Y + (enemyTankHeight - shell.getHeight()) / 2);
break;
case 2:
shell.setLocation(this.X + (enemyTankHeight - shell.getHeight()) / 2,
this.Y - enemyTankWidth / 2);
break;
case 3:
shell.setLocation(this.X + (enemyTankHeight - shell.getHeight()) / 2,
this.Y + enemyTankWidth);
break;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -