无法获取对象的 x 和 y 值

Not able to get x and y values of an object

我们需要创建一个简单的吃豆人游戏,请耐心等待,我是 java 的新手。所以我在这个作业的这一部分做了几乎所有的事情,但是,我被困在我们必须传递点坐标以收集玩家 class 方法的地方。 这是给我们的指示:-

向 Player 添加一个“collect()”方法 class 以收集指定为参数的点(如果可能的话)。只有当玩家与点处于同一位置时,玩家才能收集点。 当玩家收集点时,玩家的“collectedDots”计数应增加 1。 当玩家收集点时,点应该消失。 为了实现这一点,作为 class Player 中的“collect()”方法,应该调用“disappear()”方法 class点。

下面是游戏class.

public class Game {
    // instance variables - replace the example below with your own

    private Player player;
    private Dot dot1;
    private Dot dot2;
    private Dot dot3;

    //Do not touch anything above this mofo.
    /**
     * Constructor for objects of class Game
     */
    public Game(int xPos, int yPos) {
        // initialise instance variables
        player = new Player(xPos, yPos);

        dot1 = new Dot(1, 1);

        dot2 = new Dot(2, 2);

        dot3 = new Dot(3, 3);

    }
    //Do not touch anything above this mofo. 

    public void move(int dx, int dy) {

        player.move(dx, dy);
        /*everything working above this*/
        player.collect(dot1);
        player.collect(dot2);
        player.collect(dot3);
    }

    public String toString() {
        return player + " " + dot1 + " " + dot2 + " " + dot3;
    }
}

玩家class:

public class Player {

    private int x;
    private int y;
    private int collectedDots;

    /**
     * Constructor for objects of class Player
     */
    public Player(int xCoordinate, int yCoordinate) { // initialise instance variables
        x = xCoordinate;
        y = yCoordinate;
        collectedDots = 0;
    }
    // do not touch anything above this line\

    public void move(int dx, int dy) {
        x = x + dx;
        y = y + dy;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void collect(Dot dot1) {
        Dot disappearDot = new Dot(x, y);
        if (x1 == x && y1 == y) {
            collectedDots = 1;
            disappearDot.disappear();
        } else if (x1 == x && y1 == y) {
            collectedDots = 2;
            disappearDot.disappear();
        } else if (x1 == x && y1 == y) {
            collectedDots = 3;
            disappearDot.disappear();
        }
    }

    public String toString() {
        return "Player[" + collectedDots + "]" + Util.objectStr(x, y, true);
    }

}

在我看来,我们可以在计划中分阶段进行。让我们去解决你的问题,例如你的代码。

class Player {

    int id;
    String name;

    int xPos;
    int yPos;

    public Player(int x, int y){
        this.xPos = x;
        this.yPos = y;
    }

    public void collect(Dot d){

    }
    public void move(int dx, int dy){

    }
}

此外,我们需要构建:

class Dot {
    int xPos;
    int yPos;

    public Dot(int x, int y){
        this.xPos = x;
        this.yPos = y;
    }

}

现在,您的代码将传递 Player 的坐标。

根据您更新的评论,您只需在传递给您的方法的点对象上使用点符号,即可从点对象获取 x 和 y 值。更新了您的 "collect" 方法并向根据您的要求传递的方法添加了注释:

//Inside of player class
    /**
 * Add a “collect()” method to the Player class to collect the dot specified as the parameter 
 * if that is possible. The player can collect a dot only if the player is at the same position as the dot. When 
 * the player collects a dot, the player’s “collectedDots” count should be increased by 1. When the player 
 * collects a dot, the dot should disappear. To implement that, as part of the “collect()” method in the class 
 * Player, there should be a call to a “disappear()” method in the class Dot.
 */
public void collect(Dot dot){
    if(this.xCoordinate == dot.getX() && this.yCoordinate == dot.getY()){
        //increment the players count
        setCollectedDots(getCollectedDots() + 1);

        //remove the dot
        dot.disappear();
    }
}

添加了消失圆点的方法(假设从游戏中删除圆点)

public class Dot {
    private int x;
    private int y;

    public Dot(int x, int y){
        this.x = x;
        this.y = y;
    }

    /**
     * Returns the full coordinates for the Dot[x,y]
     * @return
     */
    public int[] getCoordinates(){
        int[] coordinates = new int[2];
        //place our coordinates into the array
        coordinates[0] = x;
        coordinates[1] = y;

        return coordinates;
    }

    public void disappear(){
        //remove from the dot grid by setting to a negative position
        setX( -(this.x));
        setY(-(this.y));
    }

    /**
     * @return the x
     */
    public int getX() {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * @return the y
     */
    public int getY() {
        return y;
    }

    /**
     * @param y the y to set
     */
    public void setY(int y) {
        this.y = y;
    }
}