简体   繁体   English

蛇游戏移动问题[javascript]

[英]snake game move issue [javascript]

I'm kind of new in javascript and I'm trying to make a snake game, I would like to make the snake move automatically with snake.push and snake.shift(tail) , I tried a few things and always failed, like a foor loop and add dy++ to the hY (but only the head was moving). 我是javascript的新手,正在尝试制作蛇游戏,我想使用snake.pushsnake.shift(tail)使蛇自动移动,我尝试了一些尝试,但始终失败,例如一个前foor loop并将dy ++添加到hY(但只有头部在移动)。

With this code, all I get is head to move and the tail shifted, but it just move once. 有了这段代码,我得到的只是头移动而尾巴移动,但是它只移动了一次。 How would it be a smart way to make it keep moving the desired direction by by adding and removing the item from the multidimensional array ? 通过从多维数组中添加和删除项目来使其保持期望的方向,这将是一种明智的方法吗?

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var snakeSize=10;
var snakeLength = 5;
var dist =2+snakeSize;
var dy=1;    
var direction = "up";

function game(){
 ctx.clearRect(0,0,canvas.width,canvas.height); 
drawSnake()

}


function snakeBody(x,y){
  ctx.beginPath();
  ctx.fillRect(x*(dist),y*(dist),snakeSize,snakeSize);
  ctx.fillStyle="red";
  ctx.fill();
  ctx.closePath();}

function drawSnake(){
 var snake = []; 
  for (var i=0;i<=snakeLength;i++){
  snake.push({x:i, y:canvas.height/dist/2});
  } 

  var hX = snake[snake.length-1].x
  var hY = snake[snake.length-1].y

  switch(direction){
  case "down":
  hY++;
      break;
  case "up":{
    hY--;}
    break;
  case "right":
    hX++
    break;
  case "left":
    hX--;
    break;
}

var tail = snake.shift();
  tail.x=hX;
  tail.y=hY;

  snake.push(tail);


   for (var i=0;i<=snake.length;i++){
 snakeBody(snake[i].x,snake[i].y); 
  }

} 


setInterval(game,1000)

It turns out that your snake initialization code is included at the beginning of the drawSnake() function. 事实证明,您的蛇初始化代码包含在drawSnake()函数的开头。

But it should be processed only once, when a new game is launched. 但是,在启动新游戏时,仅应处理一次。 Another problem is that snake[] must exist in the global scope of the game so that it is persistent between 2 calls to game() . 另一个问题是snake[]必须存在于游戏的全局范围内,以便在两次调用game()之间保持不变。

You can easily have your existing code working by moving this block: 您可以通过移动以下代码块轻松地使现有代码正常工作:

var snake = [];
for (var i=0;i<=snakeLength;i++){
  snake.push({x:i, y:canvas.height/dist/2});
} 

just before: 之前:

setInterval(game,1000)

There are also 3 other minor fixes in this JSFiddle : 此JSFiddle中还有3个其他小的修复程序:

  • replaced i<=snake.length with i<snake.length (off by one iteration) i<=snake.length替换为i<snake.length (关闭一次迭代)
  • replaced i<=snakeLength with i<snakeLength (off by one iteration) i<=snakeLength替换为i<snakeLength (关闭一次迭代)
  • moved ctx.fillStyle="red" before ctx.fillRect() (otherwise, the 1st rectangle is drawn in black) ctx.fillStyle="red"移动ctx.fillStyle="red" ctx.fillRect()之前(否则,第一个矩形以黑色绘制)

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM