?? enemy.java
字號:
import java.awt.*;public class Enemy { // Constants to represent the direction of the Enemy private final int LEFT = 0; private final int RIGHT = 1; private final int UP = 2; private final int DOWN = 3; // The size of the Enemy. private final int SIZE = 8; private boolean isVillain; //The colour of the enemy; private final int GREEN = 1; private final int RED = 2; private int color; private int xPos, yPos; private int direction; private int destX, destY; // The random destination position private int goldX, goldY; //The speed of the Enemy private final int SPEED = 5; public Enemy(){ xPos = 20; yPos = 20; isVillain = false; setDestinationPosition(); } public Enemy (int x, int y, boolean isRedVillain) { xPos = x; yPos = y; direction = UP; isVillain = isRedVillain; setDestinationPosition(); if(isVillain){ color = RED; }else{ color = GREEN; } } private void setDestinationPosition(){ if(isVillain){ destX = goldX; destY = goldY; }else{ destX = (int)(Math.random() * 440 + 10); destY = (int)(Math.random() * 440 + 10); } } public void setGoldPosition(int x, int y){ goldX = x; goldY = y; } private void setDirection(){ if(isVillain){ if(goldX < xPos - SIZE){ direction = LEFT; }else if(goldX > xPos + SIZE){ direction = RIGHT; }else{ if(goldY < yPos - SIZE){ direction = UP; }else if(goldY > yPos + SIZE){ direction = DOWN; }else{ setDestinationPosition(); return; } } }else{ if(destX < xPos - SIZE){ direction = LEFT; }else if(destX > xPos + SIZE){ direction = RIGHT; }else{ if(destY < yPos - SIZE){ direction = UP; }else if(destY > yPos + SIZE){ direction = DOWN; }else{ setDestinationPosition(); return; } } } } //Pass the position of the enemy. public int getX(){ return xPos - SIZE; } public int getY(){ return yPos - SIZE; } public int getSize(){ return SIZE; } private boolean isGotDestinationPoint(){ if(isVillain){ Point destinationPoint; Rectangle villainBox; destinationPoint = new Point(goldX,goldY); villainBox = new Rectangle(xPos - SIZE, yPos - SIZE, SIZE * 2, SIZE * 2); return villainBox.contains(destinationPoint); } if(isVillain == false){ Point destinationPoint; Rectangle enemyBox; destinationPoint = new Point(destX, destY); enemyBox = new Rectangle(xPos - SIZE, yPos - SIZE, SIZE * 2, SIZE * 2); return enemyBox.contains(destinationPoint); } return false; } //To change the facing direction of the Enemy. //To move the Enemy public void move(Rectangle boundary){ setDirection(); if(isGotDestinationPoint()){ setDestinationPosition(); setDirection(); } if(direction == UP && yPos - SIZE > boundary.y){ yPos -= SPEED; }else if(direction == DOWN && yPos + SIZE < boundary.y + boundary.height){ yPos += SPEED; }else if(direction == LEFT && xPos - SIZE > boundary.x){ xPos -= SPEED; }else if(direction == RIGHT && xPos + SIZE < boundary.x + boundary.width){ xPos += SPEED; } } // Display the Enemy. The direction the Enemy is facing // is indicated by a narrow rectangle. public void draw(Graphics g) { // Draw the main body of the Enemy if(color == GREEN){ g.setColor(Color.GREEN); }else if(color == RED){ g.setColor(Color.RED); } g.fillRect(xPos-SIZE, yPos-SIZE, 2*SIZE, 2*SIZE); g.setColor(Color.black); g.drawRect(xPos-SIZE, yPos-SIZE, 2*SIZE, 2*SIZE); // Calculate the position of the direction indicator Rectangle directionIndicator; if (direction == UP) { directionIndicator = new Rectangle(xPos-SIZE+2, yPos-SIZE+2, 2*SIZE-4, 3); } else if (direction == DOWN) { directionIndicator = new Rectangle(xPos-SIZE+2, yPos+SIZE-5, 2*SIZE-4, 3); } else if (direction == LEFT) { directionIndicator = new Rectangle(xPos-SIZE+2, yPos-SIZE+2, 3, 2*SIZE-4); } else { directionIndicator = new Rectangle(xPos+SIZE-5, yPos-SIZE+2, 3, 2*SIZE-4); } // Draw the direction indicator g.setColor(Color.white); g.fillRect(directionIndicator.x, directionIndicator.y, directionIndicator.width, directionIndicator.height); g.setColor(Color.black); g.drawRect(directionIndicator.x, directionIndicator.y, directionIndicator.width, directionIndicator.height); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -