与Enemy AI发生问题 - Java

时间:2016-11-18 04:31:19

标签: java swing artificial-intelligence

我遇到敌人的AI问题。我现在使用的AI是一只狗。

狗应该做什么 -

如果玩家距离狗的200px,狗应该跟随玩家。它应该始终与角色保持100像素。这条狗后来会射出射弹,我可以很容易地做到这一点

我的主要问题是,狗似乎只是从它的起源,(0,0)或玩家图像的左上角攻击玩家。

我尝试在玩家周围添加8个点,以便狗不断检查他最接近的一个循环。他将朝着他最接近的那个方向前进,从而跟随玩家,但是远距离。

这是我尝试完成此操作的方式:

int p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y,p5x,p5y,p6x,p6y,p7x,p7y,p8x,p8y,p1final,p2final,p3final,p4final,p5final,p6final,p7final,p8final;
    p1x = Game.px - 350;
    p1y = Game.py - 350;
    p2x = Game.px;
    p2y = Game.py - 350;
    p3x = Game.px + 350;
    p3y = Game.py - 350;
    p4x = Game.px + 350;
    p4y = Game.py;
    p5x = Game.px + 350;
    p5y = Game.py + 350;
    p6x = Game.px;
    p6y = Game.py + 350;
    p7x = Game.px - 350;
    p7y = Game.py + 350;
    p8x = Game.px - 350;
    p8y = Game.py;
    p1final = 0;
    p2final = 0;
    p3final = 0;
    p4final = 0;
    p5final = 0;
    p6final = 0;
    p7final = 0;
    p8final = 0;
        if(x >= p1x){
            p1final = p1final + (x - p1x);
        }
        if(y < p1y){
            p1final = p1final + (p1y - y);
        }
        if(x < p1x){
            p1final = p1final + (p1x - x);
        }
        if(y >= p1y){
            p1final = p1final + (y - p1y);
        }
        if(x >= p2x){
            p2final = p2final + (x - p2x);
        }
        if(y < p2y){
            p2final = p2final + (p2y - y);
        }
        if(x < p2x){
            p2final = p2final + (p2x - x);
        }
        if(y >= p2y){
            p2final = p2final + (y - p2y);
        }
        if(x >= p3x){
            p3final = p3final + (x - p3x);
        }
        if(y < p3y){
            p3final = p3final + (p3y - y);
        }
        if(x < p3x){
            p3final = p3final + (p3x - x);
        }
        if(y >= p3y){
            p3final = p3final + (y - p3y);
        }
        if(x >= p4x){
            p4final = p4final + (x - p4x);
        }
        if(y < p4y){
            p4final = p4final + (p4y - y);
        }
        if(x < p4x){
            p4final = p4final + (p4x - x);
        }
        if(y >= p4y){
            p4final = p4final + (y - p4y);
        }
        if(x >= p5x){
            p5final = p5final + (x - p5x);
        }
        if(y < p5y){
            p5final = p5final + (p5y - y);
        }
        if(x < p5x){
            p5final = p5final + (p5x - x);
        }
        if(y >= p5y){
            p5final = p5final + (y - p5y);
        }
        if(x >= p6x){
            p6final = p6final + (x - p6x);
        }
        if(y < p6y){
            p6final = p6final + (p6y - y);
        }
        if(x < p6x){
            p6final = p6final + (p6x - x);
        }
        if(y >= p6y){
            p6final = p6final + (y - p6y);
        }
        if(x >= p7x){
            p7final = p7final + (x - p7x);
        }
        if(y < p7y){
            p7final = p7final + (p7y - y);
        }
        if(x < p7x){
            p7final = p7final + (p7x - x);
        }
        if(y >= p7y){
            p7final = p7final + (y - p7y);
        }
        if(x >= p8x){
            p8final = p8final + (x - p8x);
        }
        if(y < p8y){
            p8final = p8final + (p8y - y);
        }
        if(x < p8x){
            p8final = p8final + (p8x - x);
        }
        if(y >= p8y){
            p8final = p8final + (y - p8y);
        }
    // Now checks which is the highest
    // 1
    if(p1final > p2final && p1final > p3final && p1final > p4final && p1final > p5final && p1final > p6final && p1final > p7final && p1final > p8final){
        System.out.println("P1Final");
        if(x > p1x){
            x-=7;
            Game.dogrepaint = true;
        }
        if(x < p1x){
            x+=7;
            Game.dogrepaint = true;
        }
        if(y > p1y){
            y-=7;
            Game.dogrepaint = true;
        }
        if(y < p1y){
            y+=7;
            Game.dogrepaint = true;
        }
    }
    // 2
    if(p2final > p1final && p2final > p3final && p2final > p4final && p2final > p5final && p2final > p6final && p2final > p7final && p2final > p8final){
        System.out.println("P2Final");
        if(x > p2x){
            x-=7;
            Game.dogrepaint = true;
        }
        if(x < p2x){
            x+=7;
            Game.dogrepaint = true;
        }
        if(y > p2y){
            y-=7;
            Game.dogrepaint = true;
        }
        if(y < p2y){
            y+=7;
            Game.dogrepaint = true;
        }
    }
    // 3
    if(p3final > p1final && p3final > p2final && p3final > p4final && p3final > p5final && p3final > p6final && p3final > p7final && p3final > p8final){
        System.out.println("P3Final");
        if(x > p3x){
            x-=7;
            Game.dogrepaint = true;
        }
        if(x < p3x){
            x+=7;
            Game.dogrepaint = true;
        }
        if(y > p3y){
            y-=7;
            Game.dogrepaint = true;
        }
        if(y < p3y){
            y+=7;
            Game.dogrepaint = true;
        }
    }
    // 4
    if(p4final > p1final && p4final > p2final && p4final > p3final && p4final > p5final && p4final > p6final && p4final > p7final && p4final > p8final){
        System.out.println("P4Final");
        if(x > p4x){
            x-=7;
            Game.dogrepaint = true;
        }
        if(x < p4x){
            x+=7;
            Game.dogrepaint = true;
        }
        if(y > p4y){
            y-=7;
            Game.dogrepaint = true;
        }
        if(y < p4y){
            y+=7;
            Game.dogrepaint = true;
        }
    }
    // 5
    if(p5final > p1final && p5final > p2final && p5final > p3final && p5final > p4final && p5final > p6final && p5final > p7final && p5final > p8final){
        System.out.println("P5Final");
        if(x > p5x){
            x-=7;
            Game.dogrepaint = true;
        }
        if(x < p5x){
            x+=7;
            Game.dogrepaint = true;
        }
        if(y > p5y){
            y-=7;
            Game.dogrepaint = true;
        }
        if(y < p5y){
            y+=7;
            Game.dogrepaint = true;
        }
    }
    // 6
    if(p6final > p1final && p6final > p2final && p6final > p3final && p6final > p4final && p6final > p5final && p6final > p7final && p6final > p8final){
        System.out.println("P6Final");
        if(x > p6x){
            x-=7;
            Game.dogrepaint = true;
        }
        if(x < p6x){
            x+=7;
            Game.dogrepaint = true;
        }
        if(y > p6y){
            y-=7;
            Game.dogrepaint = true;
        }
        if(y < p6y){
            y+=7;
            Game.dogrepaint = true;
        }
    }
    // 7
    if(p7final > p1final && p7final > p2final && p7final > p3final && p7final > p4final && p7final > p5final && p7final > p6final && p7final > p8final){
        System.out.println("P7Final");
        if(x > p7x){
            x-=7;
            Game.dogrepaint = true;
        }
        if(x < p7x){
            x+=7;
            Game.dogrepaint = true;
        }
        if(y > p7y){
            y-=7;
            Game.dogrepaint = true;
        }
        if(y < p7y){
            y+=7;
            Game.dogrepaint = true;
        }
    }
    // 8
    if(p8final > p1final && p8final > p2final && p8final > p3final && p8final > p4final && p8final > p5final && p8final > p6final && p8final > p7final){
        System.out.println("P8Final");
        if(x > p8x){
            x-=7;
            Game.dogrepaint = true;
        }
        if(x < p8x){
            x+=7;
            Game.dogrepaint = true;
        }
        if(y > p8y){
            y-=7;
            Game.dogrepaint = true;
        }
        if(y < p8y){
            x+=7;
            Game.dogrepaint = true;
        }
    }
}

由于这基本上是胡言乱语,只是一长串的if语句,而且它仍然无效,我正试图找到一种更简单或实际的工作方式。

TLDR:狗不会一直跟随播放器的100像素范围。

0 个答案:

没有答案