// move the snake in the direction it is facing
move the snake in the direction it is facing
move the snake in the direction it is facing
pub fn mv(&mut self, size: Size, direction: Direction) {
        self.wait = 0.0;
        if !self.is_valid(size) {
            self.is_alive = false;
            return;
        }
        match self.direction {
            Direction::Left => {
                if direction != Direction::Right {
                    self.direction = direction;
                }
            }
            Direction::Right => {
                if direction != Direction::Left {
                    self.direction = direction;
                }
            }
            Direction::Up => {
                if direction != Direction::Down {
                    self.direction = direction;
                }
            }
            Direction::Down => {
                if direction != Direction::Up {
                    self.direction = direction;
                }
            }
            Direction::Still => self.direction = direction,
        }
        // Note: I am using 2 match cases here for visibilty (I could have put this in the one up above).
        match self.direction {
            Direction::Left => {
                if self.overlap_tail(self.position.x - 1, self.position.y) {
                    self.is_alive = false;
                    return;
                }
                self.position.x -= 1;
                self.tail.pop_back();
                self.tail.push_front(self.position);
            }
            Direction::Right => {
                if self.overlap_tail(self.position.x + 1, self.position.y) {
                    self.is_alive = false;
                    return;
                }
                self.position.x += 1;
                self.tail.pop_back();
                self.tail.push_front(self.position);
            }
            Direction::Up => {
                if self.overlap_tail(self.position.x, self.position.y - 1) {
                    self.is_alive = false;
                    return;
                }
                self.position.y -= 1;
                self.tail.pop_back();
                self.tail.push_front(self.position);
            }
            Direction::Down => {
                if self.overlap_tail(self.position.x, self.position.y + 1) {
                    self.is_alive = false;
                    return;
                }
                self.position.y += 1;
                self.tail.pop_back();
                self.tail.push_front(self.position);
            }
            Direction::Still => {}
        }
    }
pub fn mv ( & mut self , size : Size , direction : Direction ) { self . wait = 0.0 ; if ! self . is_valid ( size ) { self . is_alive = false ; return ; } match self . direction { Direction :: Left => { if direction != Direction :: Right { self . direction = direction ; } } Direction :: Right => { if direction != Direction :: Left { self . direction = direction ; } } Direction :: Up => { if direction != Direction :: Down { self . direction = direction ; } } Direction :: Down => { if direction != Direction :: Up { self . direction = direction ; } } Direction :: Still => self . direction = direction , } // Note: I am using 2 match cases here for visibilty (I could have put this in the one up above). match self . direction { Direction :: Left => { if self . overlap_tail ( self . position . x - 1 , self . position . y ) { self . is_alive = false ; return ; } self . position . x -= 1 ; self . tail . pop_back ( ) ; self . tail . push_front ( self . position ) ; } Direction :: Right => { if self . overlap_tail ( self . position . x + 1 , self . position . y ) { self . is_alive = false ; return ; } self . position . x += 1 ; self . tail . pop_back ( ) ; self . tail . push_front ( self . position ) ; } Direction :: Up => { if self . overlap_tail ( self . position . x , self . position . y - 1 ) { self . is_alive = false ; return ; } self . position . y -= 1 ; self . tail . pop_back ( ) ; self . tail . push_front ( self . position ) ; } Direction :: Down => { if self . overlap_tail ( self . position . x , self . position . y + 1 ) { self . is_alive = false ; return ; } self . position . y += 1 ; self . tail . pop_back ( ) ; self . tail . push_front ( self . position ) ; } Direction :: Still => { } } }
move the snake in the direction it is facing
move the snake in the direction it is facing
&mut self
size
Size
direction
Direction