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 SnakeGame;
let cols = 10;
let rows = 10;
let mut snake_game = new;
[SnakeGame::new] has 2 optional parameters that will get filled with some default values if not provided:
snake_directionwill default to [snake::SnakeDirection::Right].starting_positionwill 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 SnakeDirection;
snake_game.snake.set_direction;
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 Apple;
// Did we hit ourselfs or the wall?
if snake_game.check_collisions ;
// Did we hit an entity?
if let Some = snake_game.check_entity_collision
Adding entities and customization
You can randomly add entities to the game with:
new_game.generate_entity;
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:
impl_entity!;
let mut new_game = new;
new_game.generate_entity;
Working example
You can see an example implementation that runs in the terminal in the repo.
WASM support
It uses the macroquad random module.