?? enemy.java
字號(hào):
import java.awt.*;
/**
* 定義敵人的類
*/
public class Enemy{
private int xPos;
private int yPos;
private int xVPos;
private int yVPos;
private int direction;
//敵人是否失去反抗能力
private boolean alarm;
//敵人失去防抗能力的時(shí)間
private int alarmTime;
private boolean ghostMode;
private Rectangle Enemy;
private int speed;
private int interval;
private int Colour;
private int AI_time1, AI_time2;
private int lefttime, uptime, righttime, downtime;
private int gameFlow;
private boolean ghost;
private boolean canMoveUp, canMoveDown, canMoveLeft,canMoveRight;
private boolean cantGoAnyWhere;
private int prisonTime;
private boolean stop;
/**
**根據(jù)x、y、顏色、是否已經(jīng)出牢籠、剩余多長(zhǎng)時(shí)間才可出牢籠
**構(gòu)造敵人對(duì)象
*/
public Enemy(int a, int b, int c, boolean cantGoAnyWhere, int prisonTime){
xPos = a;
yPos = b;
xVPos = xPos;
yVPos = yVPos;
direction = 3;
speed = 3;
interval = 63 + 21*((int)(Math.random()*10));
AI_time1 = 0;
Colour = c;
this.prisonTime = prisonTime;
this.cantGoAnyWhere = cantGoAnyWhere;
Enemy = new Rectangle(xPos -10, yPos - 10, 21, 21);
}
/**
**方法:敵人的移動(dòng)
*/
public void move(int a, int b, Wall[] wall){
AI_time1++;
AI_time2++;
uptime++;
downtime++;
lefttime++;
righttime++;
gameFlow++;
//如果敵人沒有反抗能力
if(alarm)
alarmTime++;
boolean changeMovement = false;
//如果敵人不能移動(dòng),就將囚禁得時(shí)間不停增加
if(cantGoAnyWhere)
prisonTime++;
//當(dāng)敵人失去反抗能力后,速度降低
if(alarm)
speed = 1;
else
speed = 2;
//失去反抗能力到指定時(shí)間后,恢復(fù)原態(tài)
if(alarmTime >= 330){
alarmTime = 0;
alarm = false;
}
canMoveUp = true;
canMoveDown = true;
canMoveLeft = true;
canMoveRight = true;
Rectangle R;
Rectangle UP = new Rectangle(xVPos - 10, yVPos - 10 - speed, 21, 21);
Rectangle DOWN = new Rectangle(xVPos - 10, yVPos - 10 + speed, 21, 21);
Rectangle LEFT = new Rectangle(xVPos - 10 - speed, yVPos - 10, 21, 21);
Rectangle RIGHT = new Rectangle(xVPos - 10 + speed, yVPos - 10, 21, 21);
for(int i = 0; i < wall.length; i++){
R = new Rectangle(wall[i].getxPos() - 10, wall[i].getyPos() - 10, 21, 21);
if(R.intersects(UP))
canMoveUp = false;
if(prisonTime < 231){
//確定此 Rectangle R是否與指定 Rectangle UP 相交。如果兩個(gè)矩形的交集為非空,則它們是相交的。
//如果R與UP相交,則返回 true;否則返回 false。
if(R.intersects(UP) && direction == 0){
yPos = yVPos;
changeMovement = true;
}
}
if(!(ghost && (yVPos >= 189 && yVPos < 231 && xVPos == 210))){
if(R.intersects(DOWN))
canMoveDown = false;
if(R.intersects(DOWN) && direction == 1){
yPos = yVPos;
changeMovement = true;
}
}
if(R.intersects(LEFT))
canMoveLeft = false;
if(R.intersects(LEFT) && direction == 2){
xPos = xVPos;
changeMovement = true;
}
if(R.intersects(RIGHT))
canMoveRight = false;
if(R.intersects(RIGHT) && direction == 3){
xPos = xVPos;
changeMovement = true;
}
}
interval-=speed;
if(interval <=0)
changeMovement = true;
int u = (int)(Math.random()*4);
if(canMoveUp && direction != 1 && AI_time1 > 10 && u == 0){
direction = 0;
xPos = xVPos;
AI_time1 = 0;
}
if(canMoveDown && direction != 0 && AI_time1 > 10 && u == 1){
direction = 1;
xPos = xVPos;
AI_time1 = 0;
}
if(canMoveLeft && direction != 3 && AI_time1 > 10 && u == 2){
direction = 2;
xPos = xVPos;
AI_time1 = 0;
}
if(canMoveRight && direction != 2 && AI_time1 > 10 && u == 3){
direction = 3;
xPos = xVPos;
AI_time1 = 0;
}
if(!alarm && !cantGoAnyWhere){
if(AI_time2 > 99){
if(canMoveUp && yPos > b && AI_time2 > 99){
direction = 0;
xPos = xVPos;
AI_time2 = 0;
}
if(canMoveDown && yPos < b && AI_time2 > 99){
direction = 1;
xPos = xVPos;
AI_time2 = 0;
}
if(canMoveLeft && xPos > a && AI_time2 > 99){
direction = 2;
yPos = yVPos;
AI_time2 = 0;
}
if(canMoveRight && xPos < a && AI_time2 > 99){
direction = 3;
yPos = yVPos;
AI_time2 = 0;
}
}
}
if(alarm){
if(AI_time2 > 99){
if(canMoveDown && yPos > b && AI_time2 > 99){
direction = 1;
xPos = xVPos;
AI_time2 = 0;
}
if(canMoveUp && yPos < b && AI_time2 > 99){
direction = 0;
xPos = xVPos;
AI_time2 = 0;
}
if(canMoveRight && xPos > a && AI_time2 > 99){
direction = 3;
yPos = yVPos;
AI_time2 = 0;
}
if(canMoveLeft && xPos < a && AI_time2 > 99){
direction = 2;
yPos = yVPos;
AI_time2 = 0;
}
}
}
if(ghost){
a = 210;
b = 189;
speed = 4;
alarm = false;
alarmTime = 0;
if(AI_time2 > 99){
if(canMoveUp && yPos > b && AI_time2 > 99){
direction = 0;
xPos = xVPos;
AI_time2 = 0;
}
if(canMoveDown && yPos < b && AI_time2 > 99){
direction = 1;
xPos = xVPos;
AI_time2 = 0;
}
if(canMoveLeft && xPos > a && AI_time2 > 99){
direction = 2;
yPos = yVPos;
AI_time2 = 0;
}
if(canMoveRight && xPos < a && AI_time2 > 99){
direction = 3;
yPos = yVPos;
AI_time2 = 0;
}
}
}
if(changeMovement){
for(;;){
u = (int)(Math.random()*2);
if(direction == 0 || direction == 1){
if(u == 0){
if(canMoveLeft)
direction = 2;
if(canMoveRight)
direction = 3;
}
if(u == 1){
if(canMoveRight)
direction = 3;
if(canMoveLeft)
direction = 2;
}
AI_time1 = 0;
break;
}
if(direction == 2 || direction == 3){
if(u == 0){
if(canMoveUp)
direction = 0;
if(canMoveDown)
direction = 1;
}
if(u == 1){
if(canMoveDown)
direction = 1;
if(canMoveUp)
direction = 0;
}
if(cantGoAnyWhere){
if(canMoveLeft)
direction = 2;
if(canMoveRight)
direction = 3;
}
AI_time1 = 0;
break;
}
}
if(!alarm && !ghost && !cantGoAnyWhere){
if(canMoveLeft && xPos > a && lefttime > 66){
direction = 2;
yPos = yVPos;
AI_time2 = 0;
lefttime = 0;
}
if(canMoveRight && xPos < a && righttime> 66){
direction = 3;
yPos = yVPos;
AI_time2 = 0;
righttime = 0;
}
if(canMoveUp && yPos > b && uptime > 66){
direction = 0;
xPos = xVPos;
AI_time2 = 0;
uptime = 0;
}
if(canMoveDown && yPos < b && downtime > 66){
direction = 1;
xPos = xVPos;
AI_time2 = 0;
downtime = 0;
}
}
if(alarm && !ghost){
if(canMoveRight && xPos > a && righttime > 66){
direction = 3;
yPos = yVPos;
AI_time2 = 0;
lefttime = 0;
}
if(canMoveLeft && xPos < a && lefttime> 66){
direction = 2;
yPos = yVPos;
AI_time2 = 0;
righttime = 0;
}
if(canMoveDown && yPos > b && downtime > 66){
direction = 1;
xPos = xVPos;
AI_time2 = 0;
uptime = 0;
}
if(canMoveUp && yPos < b && uptime > 66){
direction = 0;
xPos = xVPos;
AI_time2 = 0;
downtime = 0;
}
}
if(ghost){
a = 210;
b = 189;
if(canMoveLeft && xPos > a && lefttime > 66){
direction = 2;
yPos = yVPos;
AI_time2 = 0;
lefttime = 0;
}
if(canMoveRight && xPos < a && righttime> 66){
direction = 3;
yPos = yVPos;
AI_time2 = 0;
righttime = 0;
}
if(canMoveUp && yPos > b && uptime > 66){
direction = 0;
xPos = xVPos;
AI_time2 = 0;
uptime = 0;
}
if(canMoveDown && yPos < b && downtime > 66){
direction = 1;
xPos = xVPos;
AI_time2 = 0;
downtime = 0;
}
}
interval = 63 + 21*((int)(Math.random()*10));
}
if(ghost){
if(xVPos == 189 && yVPos == 441)
direction = 0;
if(xVPos == 231 && yVPos == 441)
direction = 0;
if(xVPos == 189 && yVPos == 399)
direction = 2;
if(xVPos == 231 && yVPos == 399)
direction = 3;
if(xVPos == 189 && yVPos == 357)
direction = 0;
if(xVPos == 231 && yVPos == 357)
direction = 0;
if(xVPos == 189 && yVPos == 315)
direction = 2;
if(xVPos == 231 && yVPos == 315)
direction = 3;
if(xVPos == 147 && yVPos == 315)
direction = 0;
if(xVPos == 273 && yVPos == 315)
direction = 0;
if(xVPos == 105 && yVPos == 231)
direction = 3;
if(xVPos == 315 && yVPos == 231)
direction = 2;
if(xVPos == 147 && yVPos == 105)
direction = 1;
if(xVPos == 273 && yVPos == 105)
direction = 1;
if(yVPos >= 189 && yVPos < 231 && xVPos == 210)
direction = 1;
if(xVPos == 210 && yVPos == 231){
ghost = false;
speed = 2;
cantGoAnyWhere = true;
prisonTime = 0;
}
}
if(prisonTime > 231){
if(yVPos > 189 && yVPos <= 231 && xVPos == 210){
direction = 0;
canMoveUp = true;
}
if(yVPos == 189){
prisonTime = 0;
cantGoAnyWhere = false;
}
}
if(stop)
speed = 0;
if(direction == 0 && canMoveUp)
yPos-=speed;
if(direction == 1 && canMoveDown)
yPos+=speed;
if(direction == 2 && canMoveLeft)
xPos-=speed;
if(direction == 3 && canMoveRight)
xPos+=speed;
if(direction == 2 && xPos < 0)
xPos = 420;
if(direction == 3 && xPos > 420)
xPos = 0;
Enemy = new Rectangle(xPos -10, yPos - 10, 21, 21);
a = (xPos - 10)/21;
b = (xPos - 10)%21;
if(b < 6)
b = 0;
if(b > 16)
b = 21;
if(b < 17 && b > 5)
b = 11;
xVPos = a*21 + b + 10;
int c = (yPos - 10)/21;
int d = (yPos - 10)%21;
if(d < 6)
d = 0;
if(d > 16)
d = 21;
if(d < 17 && d > 5)
d = 11;
yVPos = c*21 + d + 10;
}
public void draw(Graphics g){
if(Colour == 1)
g.setColor(Color.red);
if(Colour == 2)
g.setColor(Color.cyan);
if(Colour == 3)
g.setColor(Color.pink);
if(Colour == 4)
g.setColor(Color.orange);
//Polygon 類封裝了坐標(biāo)空間中封閉的二維區(qū)域的描述。此區(qū)域以任意條線段為邊界,每條線段都是多邊形的一條邊
Polygon Ghost = null;
if(gameFlow% 16 > 8){
int[] xPoints = {xPos - 5,
xPos - 9,
xPos - 9,
xPos - 6,
xPos - 3,
xPos,
xPos + 3,
xPos + 6,
xPos + 10,
xPos + 10,
xPos + 5,
};
int[] yPoints = new int[] {yPos - 9,
yPos - 4,
yPos + 9,
yPos + 4,
yPos + 9,
yPos + 4,
yPos + 9,
yPos + 4,
yPos + 9,
yPos - 4,
yPos - 9,
};
//初始化新的 Polygon。
//參數(shù):xPoints為x坐標(biāo)的數(shù)組;yPoints為y坐標(biāo)的數(shù)組;xPoints.length為此Polygon中點(diǎn)的總數(shù)
Ghost = new Polygon (xPoints, yPoints, xPoints.length);
}
if(gameFlow% 16 < 9){
int[] xPoints = {xPos - 5,
xPos - 9,
xPos - 9,
xPos - 6,
xPos,
xPos + 6,
xPos + 10,
xPos + 10,
xPos + 5,
};
int[] yPoints = new int[] {yPos - 9,
yPos - 4,
yPos + 9,
yPos + 4,
yPos + 9,
yPos + 4,
yPos + 9,
yPos - 4,
yPos - 9,
};
Ghost = new Polygon (xPoints, yPoints, xPoints.length);
}
if(!alarm && !ghost){
//用圖形上下文的當(dāng)前顏色填充由指定的Polygon對(duì)象定義的多邊形Ghost。
g.fillPolygon(Ghost);
}
//如果敵人沒有失去反抗能力,則用藍(lán)色進(jìn)行填充敵人的整體,用白色填充敵人的眼睛。
if(!alarm || ghost){
//填充敵人眼睛的白色部分
g.setColor(Color.white);
g.fillOval(xPos - 8, yPos - 5, 7, 8);
g.fillOval(xPos + 2, yPos - 5, 7, 8);
//填充敵人眼睛中的藍(lán)色十字部分
g.setColor(Color.blue);
if(direction == 0){
//使用當(dāng)前顏色填充外接指定矩形框的橢圓。
g.fillOval(xPos - 6, yPos - 4, 3, 4);
g.fillOval(xPos + 4, yPos - 4, 3, 4);
}
if(direction == 1){
g.fillOval(xPos - 6, yPos - 2, 3, 4);
g.fillOval(xPos + 4, yPos - 2, 3, 4);
}
if(direction == 2){
g.fillOval(xPos - 7, yPos - 3, 3, 4);
g.fillOval(xPos + 3, yPos - 3, 3, 4);
}
if(direction == 3){
g.fillOval(xPos - 5, yPos - 3, 3, 4);
g.fillOval(xPos + 5, yPos - 3, 3, 4);
}
}
//如果敵人失去反抗能力
if(alarm){
g.setColor(Color.blue);
//當(dāng)敵人即將恢復(fù)攻擊能力時(shí),將其顏色顯示為灰色
if(alarmTime >= 231 && alarmTime%6 > 2)
g.setColor(Color.lightGray);
g.fillPolygon(Ghost);
g.setColor(Color.white);
g.fillOval(xPos - 6, yPos - 6, 5, 6);
g.fillOval(xPos + 2, yPos - 6, 5, 6);
//在此圖形上下文的坐標(biāo)系統(tǒng)中,使用當(dāng)前顏色(白色),
//在點(diǎn)(xPos - 6, yPos + 3)和(xPos - 4, yPos + 1)之間畫一條線。
g.drawLine(xPos - 6, yPos + 3, xPos - 4, yPos + 1);
g.drawLine(xPos - 4, yPos + 1, xPos - 2, yPos + 3);
g.drawLine(xPos - 2, yPos + 3, xPos, yPos + 1);
g.drawLine(xPos, yPos + 1, xPos + 2, yPos + 3);
g.drawLine(xPos + 2, yPos + 3, xPos + 4, yPos + 1);
g.drawLine(xPos + 4, yPos + 1, xPos + 6, yPos + 3);
}
}
/**
**方法:設(shè)置敵人是否處于失去反抗能力的狀態(tài)
*/
public void Alarm(int a){
if(a == 1)
alarm = true;
if(a == 2)
alarm = false;
alarmTime = 0;
}
public void Ghost(){
ghost = true;
alarmTime = 0;
}
public Rectangle getBorder(){
return Enemy;
}
public int status(){
int a = 0;
if(alarm)
a = 1;
if(ghost)
a = 2;
return a;
}
public void stop(){
stop = true;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -