snake3

Crates.iosnake3
lib.rssnake3
version0.1.3
created_at2025-06-24 15:26:04.595021+00
updated_at2025-07-06 19:22:04.432086+00
descriptionPlay the classical snake game on your terminal or use the crate to build your own.
homepagehttps://github.com/ciurana-life/snake3
repositoryhttps://github.com/ciurana-life/snake3
max_upload_size
id1724472
size92,435
Victor Ciurana (ciurana-life)

documentation

README

snake3 🐍

This crate gives you the building blocks to create the classical snake game and at the same time allows you to introduce new entities to the game and define how they interact with the snake.

Creating a new game

use snake3::SnakeGame;
let cols = 10;
let rows = 10;
let mut snake_game = SnakeGame::new(
    cols,
    rows,
    None, // snake_direction
    None  // starting_position
);

[SnakeGame::new] has 2 optional parameters that will get filled with some default values if not provided:

  • snake_direction will default to [snake::SnakeDirection::Right].
  • starting_position will default to the tuple (cols/2, rows/2).

Moving the snake

Using the method snake::Snake::set_direction we can change where the snake is headed:

use snake3::snake::SnakeDirection;
snake_game.snake.set_direction(SnakeDirection::Up);

And then on our game loop we can call snake::Snake::advance to move in the las set direction:

snake_game.snake.advance();

Dealing with collisions

After we have advanced we have to check if we are hitting a wall, ourselfs or any other entity:

use snake3::snake::Apple;
// Did we hit ourselfs or the wall?
if snake_game.check_collisions() {
    // End the game or custom logic
};
// Did we hit an entity?
if let Some(hit) = snake_game.check_entity_collision() {
    if let Some(_apple) = hit.downcast_ref::<Apple>() {
        // Make the snake bigger and add +1 to the score
    }
    // Or check for your custom entity
}

Adding entities and customization

You can randomly add entities to the game with:

new_game.generate_entity(named!(Apple));

The default game comes just with the [snake::Apple] entity, but you can add as many as you want, in the above examples you learned how to create and check for entities, here is how to add your own:

pub struct Bomb {
    pub x: i16,
    pub y: i16,
}
impl_entity!(Bomb);

let mut new_game = SnakeGame::new(10, 10, None, None);
new_game.generate_entity(named!(Bomb));

Working example

You can see an example implementation that runs in the terminal in the repo.

WASM support

It uses the macroquad random module.

cargo build --release --target wasm32-unknown-unknown
Commit count: 0

cargo fmt