Skip to main content

Collision Detection

Your game is progressing nicely. Only two more things and you're done: Growing the snake and "Game Over". For the first, you need to check if the snake collides with the fruit. For the second, you need to check if the snake collides with itself.

Collision Detection with the Fruit#

Collision detection can be one of the harder to understand concepts of game development. Lucky for you, this is not the case this time. This game is using a 20x20 grid. Each cell can either be occupied or free. To check this, you can compare the X and Y values of the 2 entities that are checked.

A simple

if snake.body[1] == fruit then  -- Snake's head hits the fruitend

is enough already to check if the snake eats the fruit. And to make the snake "grow", simply increase the length of the snake using the push function of the sequence. Now it remains the question what values should this new piece have. The easiest would be to add the current last piece:

local tail = snake.body[#snake.body]snake.body:push({ x = tail.x, y = tail.y })

Once this done, simply relocate the fruit:

fruit = { x = math.random(0, 19), y = math.random(0,19) }

Just this however will reallocate the fruit in the same places for every run because it uses always the same seed, to generate a random seed we need to call math.randomseed function with at least one argument, which can be frame_count:

math.randomseed(frame_count)

In its final form, it could look like this:

  if frame_count % 15 == 0 then    snake:update()
    if snake.body[1] == fruit then      local tail = snake.body[#snake.body]      snake.body:push({ x = tail.x, y = tail.y })      math.randomseed(frame_count)      fruit = { x = math.random(0, 19), y = math.random(0,19) }    end  end

Now you're almost done. Only "Game Over" is left to finish this game.

Collision Detection with Itself#

For the player to have any sense of "danger", the game needs a possibility for the player to lose. Usually the snake can't touch itself or it dies. For this, just loop through the body and check if the piece and the head have the same coordinates. Just like with the fruit. But it might be a good idea to move this to its own function:

function Snake:is_dead(): boolean  local head = self.body[1]
  for i = 2, #self.body do    if self.body[i] == head then      return true    end  end
  return falseend

Now you can call this function to check if the snake died in this frame:

  if frame_count % 15 == 0 then    snake:update()
    if snake:is_dead() then      -- Do something    end
    if snake.body[1] == fruit then      local tail = snake.body[#snake.body]      snake.body:push({ x = tail.x, y = tail.y })      math.randomseed(frame_count)      fruit = { x = math.random(0, 19), y = math.random(0,19) }    end  end

What you do, is up to you. You could stop the game and show the score. Or you could simply reset the game. Up to you.