简体   繁体   中英

How can I unshift an array into each property of an object?

I'm trying to make a simple snake game but instead of adding one block to the snake when it eats a piece of food it adds multiple blocks. I am working off of some sample code. The original code uses the unshift() method to place the tail block of the snake at the head of the snake.

tail = {x: head_x , y: head_y};

snake_array.unshift(tail);

The object tail is being shifted into the snake_array to add the new x and y coordinates to the front of the snake. I'd like to unshift two x values and two y values into the snake_array such that the snake grows by two units each time it eats the food. I thought I could use an array for each of the "tail" object properties but every time I do this the snake just kind of disappears into oblivion.

if(head_x == food.x && head_y == food.y){
   if (direction == "right") var tail = {x: [head_x+1,head_x] , y: [head_y,head_y]};
snake_array.unshift(tail);

I don't understand why this happens, am I not able to unshift arrays within object parameters?

Here is my full code, I changed head_x to nx and head_y to ny for easier reading. I'm only focused on when the snake moves in the left direction at the moment, the other directions I can figure out once I understand how the unshift method works.

if(nx == food.x && ny == food.y){
     var tail;
      if (d == "right") tail = {x: [nx+1,nx] , y: [ny,ny]}; //incremented to get new head position
    else if(d == "left") tail = {x: nx-1,y: ny};
    else if(d == "up") tail = {x: nx,y: ny-1};
    else if(d == "down") tail = {x: nx,y: ny+1};
      //Create new food
      score++;
      create_food();
    }     
    else{
      var tail = snake_array.pop();//pops out last cell
      tail = {x: nx,y: ny};
    }

    snake_array.unshift(tail); //Puts back the tail as the first cell

So as I described in the comments the code in the question does not work because the array does not properly translate into the snake_array. Instead I created a 1x2 array called "tail", each index contained the individual properties of the object and their values. Then I used the concat() method recommended by MinusFour. I also moved the unshift() method into the else part of the if statement. I plan to clean up the code so that if I want to add 5 blocks per food or 10 blocks I can easily do that using a for loop. Thank you trincot for pointing out how to print using the console.

if (nx == food.x && ny == food.y) {

  if (d == "right") tail = [{x: nx+1,y: ny},{x: nx,y: ny}];
  else if (d == "left") tail = [{x: nx - 1,y: ny},{x: nx,y: ny}];
  else if (d == "up") tail = [{x: nx,y: ny - 1},{x: nx,y: ny}];
  else if (d == "down") tail = [{x: nx,y: ny + 1},{x: nx,y: ny}];
  //Create new food
  create_food();
  snake_array = tail.concat(snake_array);
} else {
  var tail = snake_array.pop(); //pops out last cell
  tail = {x: nx,y: ny};
  console.log(JSON.stringify(tail));
  snake_array.unshift(tail); //Puts back the tail as the first cell
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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