gpt4 book ai didi

java - 将具有相同逻辑的图像添加到游戏中的不同位置?

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:24 24 4
gpt4 key购买 nike

我正在创建一款自上而下的射击游戏,僵尸会追赶你。我已经添加了所有逻辑,因此僵尸会跟随玩家在中心,如果僵尸与玩家相交,玩家就会失败。目前游戏中只有一只僵尸。但我希望能够添加更多僵尸而不需要太多代码。我希望有一个部分可以用来在随机位置添加一个新部分。

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;
}

最佳答案

假设您希望在给定时间内最多有 10 个僵尸。因此,在开始时创建一个包含 10 个僵尸的池(10 个僵尸的数组)。从阵列/池中的随机位置生成僵尸。当您想生成另一个僵尸时,请从池中获取另一个未激活或在屏幕上的僵尸并生成它。更新活着的僵尸(你的僵尸类中应该有一个 boolean 标志,也许你可以知道僵尸是否在屏幕上,这样你就不会更新/渲染不可见的僵尸)。

关于java - 将具有相同逻辑的图像添加到游戏中的不同位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17812779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com