logic to keep snake within boundaries
This commit is contained in:
@@ -95,6 +95,20 @@ impl Snake {
|
|||||||
field.get_tile(pos.x as usize, pos.y as usize) == Tile::Food
|
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) {
|
pub fn do_move(&mut self) {
|
||||||
println!("moving?");
|
println!("moving?");
|
||||||
// probably a more optimal way of approaching this is just
|
// probably a more optimal way of approaching this is just
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ impl SnakeState for MovingState {
|
|||||||
self.dt_accumulator -= interval;
|
self.dt_accumulator -= interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
snake.keep_within_boundaries(field.width as isize, field.height as isize);
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user