logic to keep snake within boundaries

This commit is contained in:
2026-03-10 17:09:48 -05:00
parent f75ab1ac47
commit b4166d9e63
2 changed files with 16 additions and 0 deletions

View File

@@ -95,6 +95,20 @@ impl Snake {
field.get_tile(pos.x as usize, pos.y as usize) == Tile::Food
}
pub fn keep_within_boundaries(&mut self, width: isize, height: isize) {
if self.body.position.x >= width {
self.body.position.x = 0;
} else if self.body.position.x < 0 {
self.body.position.x = width - 1;
}
if self.body.position.y >= height {
self.body.position.y = 0;
} else if self.body.position.y < 0 {
self.body.position.y = height - 1;
}
}
pub fn do_move(&mut self) {
println!("moving?");
// probably a more optimal way of approaching this is just

View File

@@ -55,6 +55,8 @@ impl SnakeState for MovingState {
self.dt_accumulator -= interval;
}
snake.keep_within_boundaries(field.width as isize, field.height as isize);
None
}
}