pub struct SnakeBody<'a> {
pub(crate) body: Vec<GraphicBlock<'a>>,
case_size: u16,
position_ini: Position,
size_ini: u16,
}
Expand description
A struct representing the snake’s body in the game.
It is composed of multiple GraphicBlock
elements that make up the snake’s segments.
The body can move, grow, and check for overlaps with itself.
§Fields
body
: A vector ofGraphicBlock
elements representing the segments of the snake’s body.CASE_SIZE
: The size of each segment of the snake’s body in pixels.position_ini
: The initial position of the snake’s head.size_ini
: The initial size of the snake (the number of body segments).
Fields§
§body: Vec<GraphicBlock<'a>>
§case_size: u16
§position_ini: Position
§size_ini: u16
Implementations§
Source§impl<'a> SnakeBody<'a>
impl<'a> SnakeBody<'a>
Sourcepub fn new(
body_image: &'a str,
head_image: &'a str,
nb: u16,
position: Position,
case_size: u16,
) -> SnakeBody<'a>
pub fn new( body_image: &'a str, head_image: &'a str, nb: u16, position: Position, case_size: u16, ) -> SnakeBody<'a>
Creates a new SnakeBody
instance with the specified body image, head image, number of segments,
initial position, and case size.
§Parameters
body_image
: The image for the body segments of the snake.head_image
: The image for the snake’s head.nb
: The number of body segments.position
: The initial position of the snake’s head.CASE_SIZE
: The size of each body segment in pixels.
§Returns
A new SnakeBody
instance with the specified parameters.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the snake’s body to its initial position and size. The head is placed at the initial position, and the body segments are repositioned accordingly.
Sourcepub fn ramping_body(&mut self, previous_head: &Position)
pub fn ramping_body(&mut self, previous_head: &Position)
Updates the positions of the body segments to simulate the movement of the snake. The body segments “follow” the previous segment. Add one previous not shown elements by enabling it (to avoid a big increase in tail as +10)
§Parameters
previous_head
: The position of the previous head of the snake.
Sourcepub fn is_snake_eating_itself(&self) -> bool
pub fn is_snake_eating_itself(&self) -> bool
Checks if the snake’s head overlaps with any part of its body.
§Returns
false
if the head does not overlap with the body.true
if the head overlaps with any part of the body.
Sourcepub fn left(&mut self)
pub fn left(&mut self)
Moves the snake’s head left by one case and updates the body accordingly.
Sourcepub fn right(&mut self)
pub fn right(&mut self)
Moves the snake’s head right by one case and updates the body accordingly.
Sourcepub fn up(&mut self)
pub fn up(&mut self)
Moves the snake’s head up by one case/line and updates the body accordingly.
Sourcepub fn down(&mut self)
pub fn down(&mut self)
Moves the snake’s head down by one case/line and updates the body accordingly.
Sourcepub fn ramp(&mut self, direction: &Direction, carte: &Map<'_>) -> &Position
pub fn ramp(&mut self, direction: &Direction, carte: &Map<'_>) -> &Position
Moves the snake in the specified direction and checks if the snake’s head has moved outside the map or overlapped with its body. If the snake moves out of bounds, its position is reversed.
§Parameters
direction
: The direction in which to move the snake.carte
: The map used to check if the snake’s head is out of bounds.
§Returns
&Position
the new snake’s head position.
Sourcefn _get_widget(&self) -> impl Widget + 'a
fn _get_widget(&self) -> impl Widget + 'a
A backup plan in case the widget reference is unstable, by cloning the snake body.
Sourcepub fn relative_size_change(&mut self, nb: i16)
pub fn relative_size_change(&mut self, nb: i16)
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for SnakeBody<'a>
impl<'a> RefUnwindSafe for SnakeBody<'a>
impl<'a> Send for SnakeBody<'a>
impl<'a> Sync for SnakeBody<'a>
impl<'a> Unpin for SnakeBody<'a>
impl<'a> UnwindSafe for SnakeBody<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more