Skip to main content
added 152 characters in body
Source Link
Bálint
  • 15.1k
  • 2
  • 38
  • 57

I'm not familiar with processing, only with java, but that shouldn't matter.

How I'd do it is to have an infinite array (in java an ArrayList) of vectors, each marking a position of a snake tile. When the snake wants to move, you first create a new vector in the heads new position, add it to the array, then check if it collides with any of the other snake tiles. If it does, then it's game over, otherwise check if it collides with the fruit.

As @wondra pointed out, a Deque is a better solution than an ArrayList, because it's easier to insert and remove elements from and to the ends of the list.

If it didn't collide with the fruit, remove the first piece (the last tail), this will make the snake win and lose a tile, thus it appears as it moved forward. You'll probably need to shift the other tiles back to make the snake occupy the beginning of the array.

If it collided with the fruit, then don't di anything, this will make the snake get a new tile.

I'm not familiar with processing, only with java, but that shouldn't matter.

How I'd do it is to have an infinite array (in java an ArrayList) of vectors, each marking a position of a snake tile. When the snake wants to move, you first create a new vector in the heads new position, add it to the array, then check if it collides with any of the other snake tiles. If it does, then it's game over, otherwise check if it collides with the fruit.

If it didn't collide with the fruit, remove the first piece (the last tail), this will make the snake win and lose a tile, thus it appears as it moved forward. You'll probably need to shift the other tiles back to make the snake occupy the beginning of the array.

If it collided with the fruit, then don't di anything, this will make the snake get a new tile.

I'm not familiar with processing, only with java, but that shouldn't matter.

How I'd do it is to have an infinite array (in java an ArrayList) of vectors, each marking a position of a snake tile. When the snake wants to move, you first create a new vector in the heads new position, add it to the array, then check if it collides with any of the other snake tiles. If it does, then it's game over, otherwise check if it collides with the fruit.

As @wondra pointed out, a Deque is a better solution than an ArrayList, because it's easier to insert and remove elements from and to the ends of the list.

If it didn't collide with the fruit, remove the first piece (the last tail), this will make the snake win and lose a tile, thus it appears as it moved forward. You'll probably need to shift the other tiles back to make the snake occupy the beginning of the array.

If it collided with the fruit, then don't di anything, this will make the snake get a new tile.

Source Link
Bálint
  • 15.1k
  • 2
  • 38
  • 57

I'm not familiar with processing, only with java, but that shouldn't matter.

How I'd do it is to have an infinite array (in java an ArrayList) of vectors, each marking a position of a snake tile. When the snake wants to move, you first create a new vector in the heads new position, add it to the array, then check if it collides with any of the other snake tiles. If it does, then it's game over, otherwise check if it collides with the fruit.

If it didn't collide with the fruit, remove the first piece (the last tail), this will make the snake win and lose a tile, thus it appears as it moved forward. You'll probably need to shift the other tiles back to make the snake occupy the beginning of the array.

If it collided with the fruit, then don't di anything, this will make the snake get a new tile.