Struct SnakeBody

Source
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 of GraphicBlock 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>

Source

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.

Source

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.

Source

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.
Source

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.
Source

pub fn left(&mut self)

Moves the snake’s head left by one case and updates the body accordingly.

Source

pub fn right(&mut self)

Moves the snake’s head right by one case and updates the body accordingly.

Source

pub fn up(&mut self)

Moves the snake’s head up by one case/line and updates the body accordingly.

Source

pub fn down(&mut self)

Moves the snake’s head down by one case/line and updates the body accordingly.

Source

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.
Source

fn _get_widget(&self) -> impl Widget + 'a

A backup plan in case the widget reference is unstable, by cloning the snake body.

Source

pub fn relative_size_change(&mut self, nb: i16)

Change the snake size by adding/removing a specified number of segments to its body.

§Parameters
  • nb:The number of segments to add or to remove to the snake’s body.
§Panics

If no element in Snake, as we keep a minimum size size_ini, when resizing down should not happen

Trait Implementations§

Source§

impl<'a> Clone for SnakeBody<'a>

Source§

fn clone(&self) -> SnakeBody<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Widget for &SnakeBody<'_>

Source§

fn render(self, area: Rect, buf: &mut Buffer)

Draws the current state of the widget in the given buffer. That is the only method required to implement a custom widget.
Source§

impl Widget for SnakeBody<'_>

Only needed for backwards compatibility

Source§

fn render(self, area: Rect, buf: &mut Buffer)

Draws the current state of the widget in the given buffer. That is the only method required to implement a custom widget.
Source§

impl WidgetRef for SnakeBody<'_>

Source§

fn render_ref(&self, area: Rect, buf: &mut Buffer)

Draws the current state of the widget in the given buffer. That is the only method required to implement a custom widget.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V