gpt4 book ai didi

java:用于 java 游戏的敌方玩家跟踪 AI

转载 作者:太空狗 更新时间:2023-10-29 13:12:30 25 4
gpt4 key购买 nike

我是第一次用 Java 创建游戏,目前我正在尝试让我的僵尸跟踪并跟随玩家。下面是我的代码。我创建了一个 Controller 类,并使用链表创建了多个僵尸。然后在我的僵尸更新方法中,我使用简单的 if 僵尸语句来跟踪玩家。问题是僵尸会追踪到玩家的初始起始位置,但不会追踪到玩家移动的位置。

在僵尸 update() 方法中,我使用了 System.println(player.getX());它表明玩家的位置没有在僵尸类中更新,所以这就是为什么他们只跟踪它的起始位置。我不知道如何解决这个问题。任何帮助将不胜感激。

僵尸类:

public class Zombie extends Entity{

Animation dwn,up, left, right;
BufferedImage north, south, east, west;

Sprites sprites = new Sprites();
Player player = new Player(100,100);


Rectangle boundsZombie;

String direction = "";


public Zombie(int x, int y){

super(x,y);
sprites.create();
boundsZombie = new Rectangle(0,0, 32, 32);

}

public void update(){

if(player.getX() > x){
x = x + 1;
}

if(player.getX() < x){
x = x - 1;
}

if(player.getY() > y){
y = y + 1;
}

if(player.getY() < y){
y = y - 1;
}

System.out.println(player.getX());
}



public Rectangle getBounds(){
return new Rectangle((int) x, (int) y, 32, 32);
}

public void render(Graphics g){

g.drawImage(sprites.zombieNorth, x, y, null);
//g.setColor(Color.red);
//g.fillRect((int) x + boundsZombie.x, (int) y + boundsZombie.y, boundsZombie.width, boundsZombie.height);

}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

}

玩家类

公共(public)类播放器扩展实体{

Animation dwn,up, left, right;
BufferedImage north, south, east, west;
Sprites sprites = new Sprites();
KeyManager keyManager = new KeyManager();
Rectangle boundsPlayer;
//Controller c;


boolean movUp, movDwn, movRight, movLeft;
String direction = "";



public Player(int x, int y){
super(x,y);
sprites.create();
dwn = new Animation(100, sprites.player_down);
up = new Animation(100, sprites.player_up);
left = new Animation(100, sprites.player_left);
right = new Animation(100, sprites.player_right);
north = sprites.playerNorth;
south = sprites.playerSouth;
east = sprites.playerEast;
west = sprites.playerWest;

boundsPlayer = new Rectangle(0,0, 32, 32);
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public Rectangle getBounds(){
return new Rectangle((int) x, (int) y, 32, 32);
}

/**
* player tick method, used to move the player
*/
public void update(){

if(Game.getKeyManager().up){
y -= 3;
direction = "north";


}
if(Game.getKeyManager().down){
y += 3;
direction = "south";

}
if(Game.getKeyManager().left){
x -= 3;
direction = "west";

}

if(Game.getKeyManager().right){
x += 3;
direction = "east";
//System.out.println(x);
}

}

public int getX() {
return (int)x;
}

public int getY() {
return (int)y;
}

public String getDirection(){
return direction;
}

/**
* method used to return players facing direction sprite
* @return
*/
public BufferedImage getPlayerDirection(){
if(direction == "north"){
return north;
}

else if(direction == "south"){
return south;
}

else if(direction == "east"){
return east;
}

else{
return west;
}

}


public void render(Graphics g){
g.drawImage(getCurrentAnimationFrame(), (int) x, (int) y, null);

//g.setColor(Color.red);
//g.fillRect((int) x + boundsPlayer.x, (int) y + boundsPlayer.y, boundsPlayer.width, boundsPlayer.height);
}


/**
* method used to return the bufferedImage of current frame
* @return
*/
public BufferedImage getCurrentAnimationFrame(){
if(Game.getKeyManager().right == true){
return right.getCurrentFrame();
}

else if(Game.getKeyManager().left == true){
return left.getCurrentFrame();
}

else if(Game.getKeyManager().up == true){
return up.getCurrentFrame();
}

else if(Game.getKeyManager().down == true){
return dwn.getCurrentFrame();
}

else {
return getPlayerDirection();
}


}

最佳答案

发生这种情况是因为您的敌人类有一个玩家对象,而您的游戏类有一个完全不同的玩家对象。所以你的玩家位置在敌人类别中总是相同的。因此,在游戏类中进行敌人 AI 移动会更合乎逻辑。您的玩家或敌人类应该只保留角色的位置、方向或加载图像,无论它是敌人还是玩家。

我建议为敌人和玩家类创建一个父类,因为它们都有相似的特征。

关于java:用于 java 游戏的敌方玩家跟踪 AI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38406857/

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