The first module we will build is the game logic. You are probably familiar with snake games, but
if not, the basic idea is that the player guides a snake around a 2D grid. At any given time, there
is some "food" at a random location on the grid and the goal of the game is to get the snake to
"eat" as much food as possible. Each time the snake eats food it grows in length. The player loses
if the snake crashes into its own tail.
In some variants of the game, the player also loses if the snake crashes into the edge of the grid,
but given the small size of our grid we are going to implement a "wraparound" rule: if the snake
goes off one edge of the grid, it will continue from the opposite edge.
We start by defining a coordinate system for our game (src/game/coords.rs).
#![allow(unused)]fnmain() {
use super::Prng;
use heapless::FnvIndexSet;
/// A single point on the grid.#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]pubstructCoords {
// Signed ints to allow negative values (handy when checking if we have gone// off the top or left of the grid)pub row: i8,
pub col: i8,
}
impl Coords {
/// Get random coordinates within a grid. `exclude` is an optional set of/// coordinates which should be excluded from the output.pubfnrandom(rng: &mut Prng, exclude: Option<&FnvIndexSet<Coords, 32>>) -> Self {
letmut coords = Coords {
row: ((rng.random_u32() asusize) % 5) asi8,
col: ((rng.random_u32() asusize) % 5) asi8,
};
while exclude.is_some_and(|exc| exc.contains(&coords)) {
coords = Coords {
row: ((rng.random_u32() asusize) % 5) asi8,
col: ((rng.random_u32() asusize) % 5) asi8,
}
}
coords
}
/// Whether the point is outside the bounds of the grid.pubfnis_out_of_bounds(&self) -> bool {
self.row < 0 || self.row >= 5 || self.col < 0 || self.col >= 5
}
}
}
We use a Coords struct to refer to a position on the grid. Because Coords only contains two
integers, we tell the compiler to derive an implementation of the Copy trait for it, so we can
pass around Coords structs without having to worry about ownership.
We define an associated function, Coords::random, which will give us a random position on the
grid. We will use this later to determine where to place the snake's food.
To generate random coordinates, we need a source of random numbers. The nRF52833 has a hardware
random number generator (HWRNG) peripheral, documented at section 6.19 of the nRF52833 spec. The
HAL gives us a simple interface to the HWRNG via the microbit::hal::rng::Rng struct. The HWRNG may
not be fast enough for a game; it is also convenient for testing to be able to replicate the
sequence of random numbers produced by the generator between runs, which is impossible for the HWRNG
by design. We thus also define a pseudo-random number generator (PRNG). The PRNG uses an
xorshift algorithm to generate pseudo-random u32 values. The algorithm is basic and not
cryptographically secure, but it is efficient, easy to implement and good enough for our humble
snake game. Our Prng struct requires an initial seed value, which we do get from the RNG
peripheral.
We also need to define a few enums that help us manage the game's state: direction of movement,
direction to turn, the current game status and the outcome of a particular "step" in the game (ie, a
single movement of the snake). src/game/movement.rs contains these.
#![allow(unused)]fnmain() {
use super::Coords;
/// Define the directions the snake can move.pubenumDirection {
Up,
Down,
Left,
Right,
}
/// What direction the snake should turn.#[derive(Debug, Copy, Clone)]pubenumTurn {
Left,
Right,
None,
}
/// The current status of the game.pubenumGameStatus {
Won,
Lost,
Ongoing,
}
/// The outcome of a single move/step.pubenumStepOutcome {
/// Grid full (player wins)
Full,
/// Snake has collided with itself (player loses)
Collision,
/// Snake has eaten some food
Eat(Coords),
/// Snake has moved (and nothing else has happened)
Move(Coords),
}
}
Next up we define a Snake struct, which keeps track of the coordinates occupied by the snake and
its direction of travel. We use a queue (heapless::spsc::Queue) to keep track of the order of
coordinates and a hash set (heapless::FnvIndexSet) to allow for quick collision detection. The
Snake has methods to allow it to move. src/game/snake.rs gets this.
#![allow(unused)]fnmain() {
use super::{Coords, Direction, FnvIndexSet, Turn};
use heapless::spsc::Queue;
pubstructSnake {
/// Coordinates of the snake's head.pub head: Coords,
/// Queue of coordinates of the rest of the snake's body. The end of the tail is/// at the front.pub tail: Queue<Coords, 32>,
/// A set containing all coordinates currently occupied by the snake (for fast/// collision checking).pub coord_set: FnvIndexSet<Coords, 32>,
/// The direction the snake is currently moving in.pub direction: Direction,
}
impl Snake {
pubfnmake_snake() -> Self {
let head = Coords { row: 2, col: 2 };
let initial_tail = Coords { row: 2, col: 1 };
letmut tail = Queue::new();
tail.enqueue(initial_tail).unwrap();
letmut coord_set: FnvIndexSet<Coords, 32> = FnvIndexSet::new();
coord_set.insert(head).unwrap();
coord_set.insert(initial_tail).unwrap();
Self {
head,
tail,
coord_set,
direction: Direction::Right,
}
}
/// Move the snake onto the tile at the given coordinates. If `extend` is false,/// the snake's tail vacates the rearmost tile.pubfnmove_snake(&mutself, coords: Coords, extend: bool) {
// Location of head becomes front of tailself.tail.enqueue(self.head).unwrap();
// Head moves to new coordsself.head = coords;
self.coord_set.insert(coords).unwrap();
if !extend {
let back = self.tail.dequeue().unwrap();
self.coord_set.remove(&back);
}
}
fnturn_right(&mutself) {
self.direction = matchself.direction {
Direction::Up => Direction::Right,
Direction::Down => Direction::Left,
Direction::Left => Direction::Up,
Direction::Right => Direction::Down,
}
}
fnturn_left(&mutself) {
self.direction = matchself.direction {
Direction::Up => Direction::Left,
Direction::Down => Direction::Right,
Direction::Left => Direction::Down,
Direction::Right => Direction::Up,
}
}
pubfnturn(&mutself, direction: Turn) {
match direction {
Turn::Left => self.turn_left(),
Turn::Right => self.turn_right(),
Turn::None => (),
}
}
}
}
The Game struct keeps track of the game state. It holds a Snake object, the current coordinates
of the food, the speed of the game (which is used to determine the time that elapses between each
movement of the snake), the status of the game (whether the game is ongoing or the player has won or
lost) and the player's score.
This struct contains methods to handle each step of the game, determining the snake's next move and
updating the game state accordingly. It also contains two methods--game_matrix and
score_matrix--that output 2D arrays of values which can be used to display the game state or the
player score on the LED matrix (as we will see later).
We put the Game struct at the top of the game module, in src/game.rs.
#![allow(unused)]fnmain() {
mod coords;
mod movement;
mod rng;
mod snake;
use crate::Rng;
pubuse coords::Coords;
pubuse movement::{Direction, GameStatus, StepOutcome, Turn};
pubuse rng::Prng;
pubuse snake::Snake;
use heapless::FnvIndexSet;
/// Struct to hold game state and associated behaviourpubstructGame {
pub status: GameStatus,
rng: Prng,
snake: Snake,
food_coords: Coords,
speed: u8,
score: u8,
}
impl Game {
pubfnnew(rng: &mut Rng) -> Self {
letmut rng = Prng::seeded(rng);
let snake = Snake::make_snake();
let food_coords = Coords::random(&mut rng, Some(&snake.coord_set));
Self {
rng,
snake,
food_coords,
speed: 1,
status: GameStatus::Ongoing,
score: 0,
}
}
/// Reset the game state to start a new game.pubfnreset(&mutself) {
self.snake = Snake::make_snake();
self.place_food();
self.speed = 1;
self.status = GameStatus::Ongoing;
self.score = 0;
}
/// Randomly place food on the grid.fnplace_food(&mutself) -> Coords {
let coords = Coords::random(&mutself.rng, Some(&self.snake.coord_set));
self.food_coords = coords;
coords
}
/// "Wrap around" out of bounds coordinates (eg, coordinates that are off to the/// left of the grid will appear in the rightmost column). Assumes that/// coordinates are out of bounds in one dimension only.fnwraparound(&self, coords: Coords) -> Coords {
if coords.row < 0 {
Coords { row: 4, ..coords }
} elseif coords.row >= 5 {
Coords { row: 0, ..coords }
} elseif coords.col < 0 {
Coords { col: 4, ..coords }
} else {
Coords { col: 0, ..coords }
}
}
/// Determine the next tile that the snake will move on to (without actually/// moving the snake).fnget_next_move(&self) -> Coords {
let head = &self.snake.head;
let next_move = matchself.snake.direction {
Direction::Up => Coords {
row: head.row - 1,
col: head.col,
},
Direction::Down => Coords {
row: head.row + 1,
col: head.col,
},
Direction::Left => Coords {
row: head.row,
col: head.col - 1,
},
Direction::Right => Coords {
row: head.row,
col: head.col + 1,
},
};
if next_move.is_out_of_bounds() {
self.wraparound(next_move)
} else {
next_move
}
}
/// Assess the snake's next move and return the outcome. Doesn't actually update/// the game state.fnget_step_outcome(&self) -> StepOutcome {
let next_move = self.get_next_move();
ifself.snake.coord_set.contains(&next_move) {
// We haven't moved the snake yet, so if the next move is at the end of// the tail, there won't actually be any collision (as the tail will have// moved by the time the head moves onto the tile)if next_move != *self.snake.tail.peek().unwrap() {
StepOutcome::Collision
} else {
StepOutcome::Move(next_move)
}
} elseif next_move == self.food_coords {
ifself.snake.tail.len() == 23 {
StepOutcome::Full
} else {
StepOutcome::Eat(next_move)
}
} else {
StepOutcome::Move(next_move)
}
}
/// Handle the outcome of a step, updating the game's internal state.fnhandle_step_outcome(&mutself, outcome: StepOutcome) {
self.status = match outcome {
StepOutcome::Collision => GameStatus::Lost,
StepOutcome::Full => GameStatus::Won,
StepOutcome::Eat(c) => {
self.snake.move_snake(c, true);
self.place_food();
self.score += 1;
ifself.score % 5 == 0 {
self.speed += 1
}
GameStatus::Ongoing
}
StepOutcome::Move(c) => {
self.snake.move_snake(c, false);
GameStatus::Ongoing
}
}
}
pubfnstep(&mutself, turn: Turn) {
self.snake.turn(turn);
let outcome = self.get_step_outcome();
self.handle_step_outcome(outcome);
}
/// Calculate the length of time to wait between game steps, in milliseconds./// Generally this will get lower as the player's score increases, but need to/// be careful it cannot result in a value below zero.pubfnstep_len_ms(&self) -> u32 {
let result = 1000 - (200 * ((self.speed asi32) - 1));
if result < 200 {
200u32
} else {
result asu32
}
}
/// Return an array representing the game state, which can be used to display the/// state on the microbit's LED matrix. Each `_brightness` parameter should be a/// value between 0 and 9.pubfngame_matrix(
&self,
head_brightness: u8,
tail_brightness: u8,
food_brightness: u8,
) -> [[u8; 5]; 5] {
letmut values = [[0u8; 5]; 5];
values[self.snake.head.row asusize][self.snake.head.col asusize] = head_brightness;
for t in &self.snake.tail {
values[t.row asusize][t.col asusize] = tail_brightness
}
values[self.food_coords.row asusize][self.food_coords.col asusize] = food_brightness;
values
}
/// Return an array representing the game score, which can be used to display the/// score on the microbit's LED matrix (by illuminating the equivalent number of/// LEDs, going left->right and top->bottom).pubfnscore_matrix(&self) -> [[u8; 5]; 5] {
letmut values = [[0u8; 5]; 5];
let full_rows = (self.score asusize) / 5;
#[allow(clippy::needless_range_loop)]for r in0..full_rows {
values[r] = [1; 5];
}
for c in0..(self.score asusize) % 5 {
values[full_rows][c] = 1;
}
values
}
}
}
Next we will add the ability to control the snake's movements.