Java中的Pacman碰撞

如何解决Java中的Pacman碰撞

我正在用Java开发一个pacman街机游戏,但是我的碰撞有问题。 如果看到我的形象 here我的吃豆精灵在走到拐角处或试图转回其出现的方向时(即向左走但不再向右走)静止不动(无x或y运动)。我了解这是因为,如果检测到冲突,我会将xMovement设置为0,将yMovement设置为0(请参见collide()下的第二个代码块)。

如果我的crash()不允许这样做,我该如何允许pacman精灵以另一种方式移动(即它来自左侧,我希望它向右移动)或越过一个角落?

下面是App.java类中的draw方法,该方法根据用户的输入绘制播放器。按键分别对应于上,左,下,右。仅使用this.direction,以便在发生碰撞时玩家不会浪费任何举动,也不会穿过墙。 *请注意,我放(this.player.x_ord + 2) % 16 == 0是因为我的吃豆人图像不是16x16图像,这与我的墙壁不同。

public void draw() { // This is an infinite loop
    mapDraw(); // this draws my map constantly so I do not have multiple pacman sprites drawn 
    this.player.tick(); // see second code below for details
    if (keypressed){ // Used wasd rather than arrow keys
        if (key == 's' && this.direction != 's' && (this.player.x_ord + 2) % 16 == 0 && 
        (this.player.y_ord + 4) % 16 == 0){ // We dont want Player to turn ever on a non 16 divisible area
            this.player.p = this.loadImage("src/main/resources/playerDown.png");
            this.player.yMovement = this.player.speed;
            this.player.xMovement = 0;
            this.direction = 's';
        }
        else if (key == 'w' && this.direction != 'w' && (this.player.x_ord + 2) % 16 == 0 && 
        (this.player.y_ord + 4) % 16 == 0){
            this.player.p = this.loadImage("src/main/resources/playerUp.png");
            this.player.yMovement = -this.player.speed;
            this.player.xMovement = 0; // I do not want my pacman to move diagonally so thats why
            this.direction = 'w';

        }
        else if (key == 'd' && this.direction != 'd' && (this.player.x_ord + 2) % 16 == 0 && 
        (this.player.y_ord + 4) % 16 == 0){
            this.player.p = this.loadImage("src/main/resources/playerRight.png");
            this.player.xMovement = this.player.speed;
            this.player.yMovement = 0;
            this.direction = 'd';
        }
        else if (key == 'a' && this.direction != 'a' && (this.player.x_ord + 2) % 16 == 0 && 
        (this.player.y_ord + 4) % 16 == 0){
            this.player.p = this.loadImage("src/main/resources/playerLeft.png");
            this.player.xMovement = -this.player.speed;
            this.player.yMovement = 0;
            this.direction = 'a';
        }
    }
    this.player.draw(this); //see second code below for details
}

下面是我的player.java类 *请再次注意,this.x_ord + 2 == w.x_pos + 16 && this.y_ord + 4 == w.y_pos因为我的吃豆精灵精灵大于我的16x16墙壁而偏移了。

   public void tick(){
    // //logic   
    this.detectCollision();
    if (this.isLiving){
        this.y_ord += this.yMovement;
        this.x_ord += this.xMovement;
    }
}

public void draw(papplet a){ //just draw the sprite
    if (this.isLiving){
        a.image(this.p,this.x_ord,this.y_ord);
    }
    
}

public void collide(boolean isX){ // Is it an x or y collision
    if (isX == true){ // If it moves left or right into a wall
        this.xMovement = 0;
    }
    else if (isX == false){ // If it moves up or down into a wall
        this.xMovement = 0;
    }
}

public void detectCollision(){
    for (Walls w: this.wallLocations){ // A list of wall locations from a .txt file
        if (this.x_ord + 2 == w.x_pos + 16 && this.y_ord + 4 == w.y_pos){ // Detect left movement into right wall piece
            collide(true);
        }

        if (this.x_ord + 2 + 16 == w.x_pos && this.y_ord + 4 == w.y_pos){ // Detect right movement into left wall piece
            collide(true);
        }

        if (this.y_ord + 4 == w.y_pos + 16 && this.x_ord + 2 == w.x_pos){ // Detect up movement into bottom wall piece
            collide(false);
        }

        if (this.y_ord + 4 + 16 == w.y_pos && this.x_ord + 2 == w.x_pos){ // Detect down movement into top wall piece
            collide(false);
        }
    }

对于我的问题的任何帮助,我们深表感谢。

解决方法

我没有深入分析您的代码,但是在我看来,您的主要问题是collide方法仅考虑2种情况,即垂直移动或水平移动。如果要在4个不同的方向上移动,则该方法需要具有4个不同的状态。

为此,您可以创建一个代表方向的枚举。然后在detectCollision中将适当的Direction传递到collide中。最后,在collide中考虑4个不同的方向。例如,如果右侧有障碍,则xMovement必须为非正数。 collide方法看起来像这样:

public void collide(Direction direction){ 
    if (direction == Direction.RIGHT){ 
        this.xMovement = Math.min(0,this.xMovement);
    }
    if (direction == Direction.LEFT){ 
        this.xMovement = Math.max(0,this.xMovement);
    }
    if (direction == Direction.UP){ 
        this.yMovement = Math.min(0,this.yMovement);
    }
    if (direction == Direction.DOWN){ 
        this.yMovement = Math.max(0,this.yMovement);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?