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 self.snake.body[0] == fruit {    // Snake's head hits the fruit}

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 method. Now it remains the question what values should this new piece have. The easiest would be to add the current last piece:

// src/game.rslet dropped_pos = self.snake.update();
if self.snake.body[0] == self.fruit {    if let Some(last_pos) = dropped_pos {        self.snake.body.push(last_pos);    }}

Once this done, simply relocate the fruit:

// src/game.rsself.fruit.x = self.rng.i32(0..20);self.fruit.y = self.rng.i32(0..20);

In its final form, it could look like this:

// src/game.rs inside impl Game {} block    pub fn update(&mut self) {        self.frame_count += 1;
        self.input();
        if self.frame_count % 15 == 0 {            let dropped_pos = self.snake.update();
            if self.snake.body[0] == self.fruit {                if let Some(last_pos) = dropped_pos {                    self.snake.body.push(last_pos);                }
                self.fruit.x = self.rng.i32(0..20);                self.fruit.y = self.rng.i32(0..20);            }        }    }

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:

// src/snake.rs inside impl Snake {} block    pub fn is_dead(&self) -> bool {        self.body            .iter()            .skip(1)            .any(|&body_section| body_section == self.body[0])    }

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

// src/game.rs inside impl Game {} block    pub fn update(&mut self) {        self.frame_count += 1;
        self.input();
        if (self.snake.is_dead()) {            // Do something        }
        if self.frame_count % 15 == 0 {            let dropped_pos = self.snake.update();
            if self.snake.body[0] == self.fruit {                if let Some(last_pos) = dropped_pos {                    self.snake.body.push(last_pos);                }
                self.fruit.x = self.rng.i32(0..20);                self.fruit.y = self.rng.i32(0..20);            }        }    }

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.