我正在创建一个自上而下的射击游戏,僵尸会在其中追逐你。我已经添加了所有的逻辑,所以僵尸在中心跟随玩家,如果僵尸与玩家相交,玩家就输了。目前游戏中只有一只僵尸。但我希望能够在没有更多代码的情况下添加更多僵尸。我希望有一个部分可以用来例如在随机位置添加一个新部分。
public void addZombie(){}
我正在使用绘画方法。
这是僵尸类。它被添加到面板中。提前致谢。
package zombieGame;
import javax.swing.ImageIcon;
public class Zombie extends Sprite implements Commons {
String zombie = "/images/zombieUp.png";
int width1 = (int) screenSize.getWidth();
int height1 = (int) screenSize.getHeight();
//Move Left/Right
int dx;
int dy;
//Directions
boolean upLeft;
boolean upRight;
boolean downLeft;
boolean downRight;
boolean left;
boolean right;
boolean up;
boolean down;
public Zombie() {
if (zombie != null) {
ImageIcon ii = new ImageIcon(this.getClass().getResource(zombie));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
resetState();
}
}
public void move() {
if (zombie != null) {
x += dx;
y += dy;
animate();
}
}
public void animate() {
if (zombie != null) {
//If inbetween X
if (x + Board.amountX > width1 / 2 && x + Board.amountX < width1 / 2 + 3) {
dx = 0;
}
//Move right
if (x + Board.amountX < width1 / 2) {
dx = 1;
right = true;
left = false;
} else {
right = false;
}
//move left
if (x + Board.amountX > width1 / 2 + 3) {
dx = -1;
left = true;
right = false;
} else {
left = false;
}
//If inbetween Y
if (y + Board.amountY > height1 / 2 - 2 && y + Board.amountY < height1 / 2 + 3) {
dy = 0;
}
//move down
if (y + Board.amountY < height1 / 2 - 2) {
dy = 1;
down = true;
up = false;
} else {
down = false;
}
//Move up
if (y + Board.amountY > height1 / 2 + 3) {
dy = -1;
up = true;
down = false;
} else {
up = false;
}
if (left) {
zombie = "/images/zombieLeft.png";
}
if (right) {
zombie = "/images/zombieRight.png";
}
if (up) {
zombie = "/images/zombieUp.png";
}
if (down) {
zombie = "/images/zombieDown.png";
}
if (up && left) {
zombie = "/images/zombieUpLeft.png";
}
if (up && right) {
zombie = "/images/zombieUpRight.png";
}
if (down && left) {
zombie = "/images/zombieDownLeft.png";
}
if (down && right) {
zombie = "/images/zombieDownRight.png";
}
ImageIcon ii = new ImageIcon(this.getClass().getResource(zombie));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
}
}
public void resetState() {
x = 200;
y = 385;
}