Javascript 我怎样才能让蛇类游戏检测到它与尾巴的碰撞?

Javascript 我怎样才能让蛇类游戏检测到它与尾巴的碰撞?,javascript,Javascript,目前,我正在使用2d画布编写JavaScript蛇游戏时,正在努力进行碰撞检测。这对我来说是相当新鲜的。对边缘的碰撞检测工作正常。然而,我遇到了麻烦,当涉及到检测蛇是否击中自己 我一直在研究其他类似的主题,但没有发现它们直接适用于我的案例 下面是游戏循环、蛇形物体和碰撞检测功能。任何关于为什么蛇撞到自己时碰撞检测不会触发的提示都将非常感谢 在游戏循环结束时,我认为游戏应该检查snake.isOutOfBounds(map)和snake.istouching()。第二,未按预期触发 // Sn

目前,我正在使用2d画布编写JavaScript蛇游戏时,正在努力进行碰撞检测。这对我来说是相当新鲜的。对边缘的碰撞检测工作正常。然而,我遇到了麻烦,当涉及到检测蛇是否击中自己

我一直在研究其他类似的主题,但没有发现它们直接适用于我的案例

下面是游戏循环、蛇形物体和碰撞检测功能。任何关于为什么蛇撞到自己时碰撞检测不会触发的提示都将非常感谢

在游戏循环结束时,我认为游戏应该检查
snake.isOutOfBounds(map)
snake.istouching()。第二,未按预期触发

 // Snake object

  export class Snake {
  constructor(map) {
    this.positions = [{
      x: map.GetBlockSize() * 5,
      y: map.GetBlockSize() * 5
    },
    {
      x: map.GetBlockSize() * 4,
      y: map.GetBlockSize() * 5
    },
    {
      x: map.GetBlockSize() * 3,
      y: map.GetBlockSize() * 5
    }],
    this.colorBody = '#45b6fe',
    this.colorHead = '#0e2433',
    this.d,
    this.maxLength = 3,
    this.moves = 0

  }

  isOutOfBounds(map){
    if (   this.positions[0].x >= map.width
        || this.positions[0].x < 0
        || this.positions[0].y >= map.height
        || this.positions[0].y < 0
      ){
        console.log('Snake is attempting to flee.');
        return true;
      }
  }

  EatFood(food, map){
    if (  this.positions[0].x == food.x
       && this.positions[0].y == food.y){
         return true;
       } else {
         return false;
       }
   }

   IncrementTail(){
     var position = { x: this.positions[0].x,
                      y: this.positions[0].y };

     this.positions.unshift(position);
     if (this.positions.length > this.maxLength){
       this.positions.pop()
     }
   }

   IsTouchingItself(){
     for (var i = 2; i < this.positions.length; i++){
       if (this.positions[0].x === this.positions[i].x
        && this.positions[0].y === this.positions[i].y){
          console.log('Snake is touching itself!');
          return true;
        } else {
          console.log('Snake is well raised.');
          return false;
        }
     }
   }
}
//Snake对象
出口级蛇{
构造器(地图){
这一点。职位=[{
x:map.GetBlockSize()*5,
y:map.GetBlockSize()*5
},
{
x:map.GetBlockSize()*4,
y:map.GetBlockSize()*5
},
{
x:map.GetBlockSize()*3,
y:map.GetBlockSize()*5
}],
this.colorBody='#45b6fe',
this.colorHead='#0e2433',
这个,d,,
此.maxLength=3,
此值为0
}
等自动边界(地图){
if(this.positions[0].x>=map.width
||此.positions[0].x<0
||此.positions[0].y>=map.height
||此.positions[0].y<0
){
log('蛇正试图逃跑');
返回true;
}
}
吃食物(食物,地图){
if(this.positions[0].x==food.x
&&this.positions[0].y==food.y){
返回true;
}否则{
返回false;
}
}
递增尾(){
var position={x:this.positions[0].x,
y:this.positions[0].y};
此。位置。取消移动(位置);
if(this.positions.length>this.maxLength){
this.positions.pop()
}
}
我接触了自己{
对于(var i=2;i
尝试检查头部位置是否与身体部位的位置匹配

//I guess the snake's head is the first element in this.positions (index 0)
isSnakeDead() {
    //Creating the variables for simplicity
    let headX = this.positions[0].x;
    let headY = this.positions[0].y;

    //The for loop starts from 1 because the head is index 0
    //and you don't want to check if the head collides with itself
    for (let i = 1; i < this.positions.length; ++i) {
        //Another variables for simplicity
        let currentX = this.positions[i].x;
        let currentY = this.positions[i].y;

        if (headX === currentX && headY === currentY) {
            //The head collides with the current body part
            return true;
        }
    }
    //If the function reaches here then 
    //the head does not collide with any of the other parts
    return false;
}
//我猜蛇头是这个位置的第一个元素(索引0)
isSnakeDead(){
//为简单起见创建变量
设headX=this.positions[0].x;
设headY=this.positions[0].y;
//for循环从1开始,因为头是索引0
//你不想检查头部是否与自身碰撞
for(设i=1;i
如果您单击
编辑
,然后单击
[]
代码段编辑器,您可以创建一个对话框,您可以在其中更新每个位置的位置吗?我好像找不到它。如何呈现运动?头部移动,尾部跟随。请查看snake对象中的IncrementTail()。snake的渲染在Map对象中进行,使用以下函数:DrawSnake(snake){for(让i=0;i//I guess the snake's head is the first element in this.positions (index 0) isSnakeDead() { //Creating the variables for simplicity let headX = this.positions[0].x; let headY = this.positions[0].y; //The for loop starts from 1 because the head is index 0 //and you don't want to check if the head collides with itself for (let i = 1; i < this.positions.length; ++i) { //Another variables for simplicity let currentX = this.positions[i].x; let currentY = this.positions[i].y; if (headX === currentX && headY === currentY) { //The head collides with the current body part return true; } } //If the function reaches here then //the head does not collide with any of the other parts return false; }