2017-04-06 57 views
2

我正在尝试创建一个蛇游戏。我的问题是当蛇遇到食物时,没有匹配。请在我的代码中检查功能eat(),其中x需要相同food_position_xy需要等于food_position_y尝试比较两个位置时不匹配

(function() { 
 
    var canvas = document.getElementById('canvas'), 
 
     ctx = canvas.getContext('2d'), 
 
     x = 0, 
 
     y = 0, 
 
     speed = 1; 
 
     x_move = speed, 
 
     y_move = 0, 
 
     food_position_x = Math.floor(Math.random() * canvas.width); 
 
     food_position_y = Math.floor(Math.random() * canvas.height); 
 

 
    function eat() { 
 
    if (x == food_position_x && y == food_position_y) { 
 
     alert('Match!');  
 
    } 
 
    } 
 
    
 
    // Drawing 
 
    function draw() { 
 
    eat(); 
 
    requestAnimationFrame(function() {  
 
     draw();  
 
    });  
 
    // Draw the snake 
 
    ctx.beginPath(); 
 
    ctx.rect(Math.floor(x/10)*10, Math.floor(y/10)*10, 10, 10); 
 
    ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
    ctx.fillStyle = '#ffffff'; 
 
    ctx.fill(); 
 
    ctx.closePath(); 
 

 
    // Draw the food 
 
    ctx.beginPath(); 
 
    ctx.rect(Math.floor(food_position_x/10)*10, Math.floor(food_position_y/10)*10, 10, 10);  
 
    ctx.fillStyle = "blue"; 
 
    ctx.fill(); 
 
    ctx.closePath(); 
 

 
    // Increase the value of x and y in order to animate 
 
    x = x + x_move; 
 
    y = y + y_move;  
 
    } 
 
    draw(); 
 

 
    // Key Pressing 
 
    document.addEventListener('keydown', function(event) { 
 
    switch(event.keyCode) { 
 
     case 40: // Moving down 
 
     if (x_move != 0 && y_move != -1) { 
 
      x_move = 0; 
 
      y_move = speed; 
 
     } 
 
     break; 
 
     case 39: // Moving right 
 
     if (x_move != -1 && y_move != 0) { 
 
      x_move = speed; 
 
      y_move = 0; 
 
     } 
 
     break; 
 
     case 38: // Moving top 
 
     if (x_move != 0 && y_move != 1) { 
 
      x_move = 0; 
 
      y_move = -speed; 
 
     } 
 
     break; 
 
     case 37: // Moving left 
 
     if (x_move != 1 && y_move != 0) { 
 
      x_move = -speed; 
 
      y_move = 0; 
 
     } 
 
     break; 
 
    } 
 
    }); 
 
})();
canvas { background-color: red; }
<canvas width="500" height="300" id="canvas">

我在做什么错?

+1

它看起来像'food_position_x'和'food_position_y'可以在画布上任意整数坐标,但你的蛇的位置总是整除10.您正在绘制的食物放在同一像蛇一样的10像素网格,但它的实际位置是在两个单元之间的某处。作为第一步,当你设置它时,强迫食物的位置被10整除。 –

+0

您的蛇的位置也是一样,您正在以10像素的网格绘制它,但其实际位置可能是任何整数。因此,当(82,129)蛇和(85,124)食物时,你看起来像是在(80,120)所画的食物之上。 –

回答

1

下面的代码是一个工作解决方案。问题在于你用像素来衡量,所以确切的像素坐标不会总是匹配。如果将它扩大到一个范围(10像素,在这个答案中,但你可以调整),你会更好地捕捉到比赛。

我还添加了一个标志(matched),以便您不会继续得到“匹配!”对于蛇在可接受范围内移动的每个像素的消息。

如果需要,您可以调整范围,但该范围在标准桌面显示器上运行良好。

(function() { 
 
     var matched = false; 
 
     var canvas = document.getElementById('canvas'), 
 
      ctx = canvas.getContext('2d'), 
 
      x = 0, 
 
      y = 0, 
 
      speed = 1; 
 
      x_move = speed, 
 
      y_move = 0, 
 
      food_position_x = Math.floor(Math.random() * canvas.width); 
 
      food_position_y = Math.floor(Math.random() * canvas.height); 
 

 
     function eat() { 
 
     var xdiff = food_position_x - x; 
 
     var ydiff = food_position_y - y; 
 
     if ((xdiff > -6 && xdiff < 6) && (ydiff > -6 && ydiff < 6) && !matched) { 
 
      alert('Match!'); 
 
      matched = true; 
 
     } 
 
     else if (!(xdiff > -5 && xdiff < 5) && (ydiff > -5 && ydiff < 5)) { 
 
      matched = false; 
 
     } 
 
     } 
 
     
 
     // Drawing 
 
     function draw() { 
 
     eat(); 
 
     requestAnimationFrame(function() {  
 
      draw();  
 
     });  
 
     // Draw the snake 
 
     ctx.beginPath(); 
 
     ctx.rect(Math.floor(x/10)*10, Math.floor(y/10)*10, 10, 10); 
 
     ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
     ctx.fillStyle = '#ffffff'; 
 
     ctx.fill(); 
 
     ctx.closePath(); 
 

 
     // Draw the food 
 
     ctx.beginPath(); 
 
     ctx.rect(Math.floor(food_position_x/10)*10, Math.floor(food_position_y/10)*10, 10, 10);  
 
     ctx.fillStyle = "blue"; 
 
     ctx.fill(); 
 
     ctx.closePath(); 
 

 
     // Increase the value of x and y in order to animate 
 
     x = x + x_move; 
 
     y = y + y_move;  
 
     } 
 
     draw(); 
 

 
     // Key Pressing 
 
     document.addEventListener('keydown', function(event) { 
 
     switch(event.keyCode) { 
 
      case 40: // Moving down 
 
      if (x_move != 0 && y_move != -1) { 
 
       x_move = 0; 
 
       y_move = speed; 
 
      } 
 
      break; 
 
      case 39: // Moving right 
 
      if (x_move != -1 && y_move != 0) { 
 
       x_move = speed; 
 
       y_move = 0; 
 
      } 
 
      break; 
 
      case 38: // Moving top 
 
      if (x_move != 0 && y_move != 1) { 
 
       x_move = 0; 
 
       y_move = -speed; 
 
      } 
 
      break; 
 
      case 37: // Moving left 
 
      if (x_move != 1 && y_move != 0) { 
 
       x_move = -speed; 
 
       y_move = 0; 
 
      } 
 
      break; 
 
     } 
 
     }); 
 
    })();
canvas { background-color: red; }
<canvas width="500" height="300" id="canvas">

+0

谢谢你的回答。我测试了你的解决方案。由于某些原因,有时我不会得到一场比赛。 –

+0

@RezaSaadati你可能需要增加范围(而不是-5到5,也许尝试-7到7)。这可能会因屏幕尺寸/分辨率而异。另一个想法是做埃里克多布斯所建议的,只能使用可以被食物和蛇整除的位置。 – freginold

相关问题