pub fn draw(&mut self, con: Context, g: &mut G2d) {
        if Game::get_time() > self.next_move {
            if self.game_over {
                self.restart();
            }

            if !self.has_food {
                let food = self.make_food();
                self.food_x = food.0;
                self.food_y = food.1;
                self.has_food = true;
            }

            if self.ate_food() {
                self.snake.grow_snake();
                self.has_food = false;
            }

            self.next_move = Game::get_new_time(MOVE_RATE);

            if !self.detect_collision() {
                self.snake.move_snake();
            }
        }

        if self.game_over {
            // games over, draw red screen tinting
            draw_rectangle(GAME_OVER_COLOR, 0, 0, self.width, self.height, con, g);
        }

        draw_rectangle(BORDER_COLOR, 0, 0, self.height, 1, con, g);
        draw_rectangle(BORDER_COLOR, 0, 0, 1, self.width, con, g);
        draw_rectangle(BORDER_COLOR, self.width - 1, 1, 1, self.height - 1, con, g);
        draw_rectangle(BORDER_COLOR, 1, self.height - 1, self.width - 2, 1, con, g);

        self.draw_food(con, g);
        self.snake.draw(con, g);
    }
/// Update the UI and manage game state
/// Detect collisions to end game
/// Determine if Head and Apple match
/// Draw borders, game over, snake, and food
Update the UI and manage game state
Detect collisions to end game
Determine if Head and Apple match
Draw borders, game over, snake, and food
Update the UI and manage game state Detect collisions to end game Determine if Head and Apple match Draw borders game over snake and food
pub fn draw(&mut self, con: Context, g: &mut G2d) {
        if Game::get_time() > self.next_move {
            if self.game_over {
                self.restart();
            }
            if !self.has_food {
                let food = self.make_food();
                self.food_x = food.0;
                self.food_y = food.1;
                self.has_food = true;
            }
            if self.ate_food() {
                self.snake.grow_snake();
                self.has_food = false;
            }
            self.next_move = Game::get_new_time(MOVE_RATE);
            if !self.detect_collision() {
                self.snake.move_snake();
            }
        }
        if self.game_over {
            // games over, draw red screen tinting
            draw_rectangle(GAME_OVER_COLOR, 0, 0, self.width, self.height, con, g);
        }
        draw_rectangle(BORDER_COLOR, 0, 0, self.height, 1, con, g);
        draw_rectangle(BORDER_COLOR, 0, 0, 1, self.width, con, g);
        draw_rectangle(BORDER_COLOR, self.width - 1, 1, 1, self.height - 1, con, g);
        draw_rectangle(BORDER_COLOR, 1, self.height - 1, self.width - 2, 1, con, g);
        self.draw_food(con, g);
        self.snake.draw(con, g);
    }
pub fn draw ( & mut self , con : Context , g : & mut G2d ) { if Game :: get_time ( ) > self . next_move { if self . game_over { self . restart ( ) ; } if ! self . has_food { let food = self . make_food ( ) ; self . food_x = food . 0 ; self . food_y = food . 1 ; self . has_food = true ; } if self . ate_food ( ) { self . snake . grow_snake ( ) ; self . has_food = false ; } self . next_move = Game :: get_new_time ( MOVE_RATE ) ; if ! self . detect_collision ( ) { self . snake . move_snake ( ) ; } } if self . game_over { // games over, draw red screen tinting draw_rectangle ( GAME_OVER_COLOR , 0 , 0 , self . width , self . height , con , g ) ; } draw_rectangle ( BORDER_COLOR , 0 , 0 , self . height , 1 , con , g ) ; draw_rectangle ( BORDER_COLOR , 0 , 0 , 1 , self . width , con , g ) ; draw_rectangle ( BORDER_COLOR , self . width - 1 , 1 , 1 , self . height - 1 , con , g ) ; draw_rectangle ( BORDER_COLOR , 1 , self . height - 1 , self . width - 2 , 1 , con , g ) ; self . draw_food ( con , g ) ; self . snake . draw ( con , g ) ; }
Update the UI and manage game state
Detect collisions to end game
Determine if Head and Apple match
Draw borders, game over, snake, and food
Update the UI and manage game state Detect collisions to end game Determine if Head and Apple match Draw borders game over snake and food
&mut self
con
Context
g
&mut G2d
&mut self
con
Context
g
&mut G2d