敌人追逐玩家的 Java 二维数组

huangapple go评论58阅读模式
英文:

Enemy chasing player 2D array in Java

问题

大家好,我正在制作一个游戏,在特定阶段中,我有一个敌人和一个玩家,在一个10x10的数组上随机生成,然后敌人必须朝着玩家移动,直到它们处于相邻的单元格上。玩家由用户控制,敌人必须朝着玩家的方向移动以捕捉他。

我已经开始做了数组边界检查以及敌人可以移动的位置,但是我不知道怎么做追逐的部分。你能帮帮我吗?

public void 敌人移动()
{
    if (敌人存活) {
        if (敌人.当前行() == 0) {
            if (敌人.当前列() == 0) {
                // 只能向右和向下移动
            }
            else if (敌人.当前列() == 9) {
                // 只能向左和向下移动
            }
            else {
                // 可以向两侧或向下移动
            }
        }
        else if (敌人.当前行() == 9) {
            if (敌人.当前列() == 0) {
                // 只能向上或向右移动
            }
            else if (敌人.当前列() == 9) {
                // 只能向上或向左移动
            }
            else {
                // 只能向两侧或向上移动
            }
        }
        else if (敌人.当前列() == 0) {
            if (敌人.当前行() != 0 && 敌人.当前行() != 9) {
                // 除了向左外,可以朝任何方向移动
            }
        }
        else if (敌人.当前列() == 9) {
            if (敌人.当前行() != 0 && 敌人.当前行() != 9) {
                // 除了向右外,可以朝任何方向移动
            }
        }
        else {
            // 可以朝任何方向移动
        }
    }
}
英文:

So guys I'm creating a game and in a specific fase I've got an enemy and a player who spawns randomly on a 10x10 array and then the enemy must move towards the player until they are on adjacent cells. The player is controled by the user and the enemy must move in his direction to catch him.
I already started and did the array margin checks and where the enemy can move but I don't have any idea how to do the chase thing. Can you help me?

public void enemyMovement()
{
    if(enemyAlive) {
        if( enemy.getCurrentLine() == 0 ) {
            if( enemy.getCurrentColumn() == 0 ) {
                // only moves to right and down
            }
            else if( enemy.getCurrentColumn() == 9 ) {
                // only moves to left and down
            }
            else {
                // moves to the sides or down
            }
        }
        else if( enemy.getCurrentLine() == 9 ) {
            if( enemy.getCurrentColumn() == 0 ) {
                // only moves up or right
            }
            else if( enemy.getCurrentColumn() == 9 ) {
                // only moves up or left
            }
            else {
                // only moves to the sides and up
            }
        }
        else if ( enemy.getCurrentColumn() == 0 ) {
            if( enemy.getCurrentLine()  != 0 &&  enemy.getCurrentLine()  != 9) {
                // can go anywhere excepts left
            }
        }
        else if( enemy.getCurrentColumn() == 9 ) {
            if( enemy.getCurrentLine()  != 0 &&  enemy.getCurrentLine()  != 9) {
                // can go anywhere excepts right
            }
        }
        else {
            // can goes anywhere
        }
    }
}

答案1

得分: 1

检查敌人和玩家之间的距离,并沿着距离较长的轴移动。例如,如果敌人的坐标为(1,2),玩家在(9,5)处,那么它们之间的距离是(8,3),所以敌人应沿着x轴移动至(2,2)。如果它们之间的距离类似于(5,5),那么您可以选择x/y轴优先,或者随机选择一个轴。

英文:

Check the distance between the enemy and the player, and move along with the axis which the distance is longer. For example, if the coordinate of the enemy is (1,2) and the player is at (9,5), then the distance between them is (8,3), so the enemy should move along x axis to (2,2). If the distance between them is something like (5,5), then you can choose x/y axis priority or just randomly choose one axis.

huangapple
  • 本文由 发表于 2020年5月5日 08:12:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61603729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定