Projekt/snake_main.c
void move_snake(){
  //moves the snakes head forward if no command is given or in another direction if the user presses one of the buttons
  //if the user tries to move 180 degrees from current direction or presses multiple buttons the command is ignored
  //called each frame
  //updates position of the snakes head block and snake.facing_direction
  void move_head(){
    /*helper that calculates how much x should be updated depending on the direction*/
    int x_offset_from_dir(Direction dir){
      if(dir == 'L' || dir == 'R'){
        return (dir == 'L') ? -SPEED : SPEED; // L = - and R = +
      }else{
        return 0;
      }
    }
    /*helper that calculates how much y should be updated depending on the direction*/
    int y_offset_from_dir(Direction dir){
      if(dir == 'U' || dir == 'D'){
        return (dir == 'U') ? -SPEED : SPEED; // U = - and D = +
      }else{
        return 0;
      }
    }
    /*helper that calculates if the user is trying to turn left when going right or up when going down etc. (180 degrees)*/
    bool user_turning_180_degrees(Direction dir){
      switch (snake.facing_direction){
      case 'U':
        return dir == 'D';
        
      case 'D':
        return dir == 'U';

      case 'L':
        return dir == 'R';
        
      case 'R':
        return dir == 'L';
    
      default:
        return false;
      }
    }
    Direction user_move_dir = user_move_direction();
    int i;
    if(user_move_dir == 'N' || user_turning_180_degrees(user_move_dir)){
      /*no command or invalid command - just move the snake forward*/
      int x_add = x_offset_from_dir(snake.facing_direction);
      int y_add = y_offset_from_dir(snake.facing_direction);
      
      //update the head coordinates
      snake.blocks_array[0].x0 += x_add;
      snake.blocks_array[0].y0 += y_add;

    }else{
      /*a valid command is sent - move the snake in the direction the user wants*/

      int x_add = x_offset_from_dir(user_move_dir);
      int y_add = y_offset_from_dir(user_move_dir);
      
      //update the head coordinates
      snake.blocks_array[0].x0 += x_add;
      snake.blocks_array[0].y0 += y_add;

      //update the direction the snake is facing
      snake.facing_direction = user_move_dir;
    }
    

  }
  //copy snake blocks at t-1 - we're not interested in the entire snake just the blocks so copy those
  Block old_blocks[snake.num_blocks];// = snake.blocks_array;

  int j;
  for (j = 0; j < snake.num_blocks; j++)
  {
   old_blocks[j] = snake.blocks_array[j];
  }

  //update prev_tail
  snake.prev_tail = old_blocks[snake.num_blocks-1];
 
  
  //old_blocks_array = snake.blocks_array;
  
  
  //move the head
  move_head();

  //put all other blocks at position i-1 from the snake at t-1
  int i;
  for (i = 1; i < snake.num_blocks; i++){ //the head is at index 0
    snake.blocks_array[i].x0 = old_blocks[i-1].x0; //block 1 goes to the prev. head pos, block 2 goes to block 1 prev. etc.
    snake.blocks_array[i].y0 = old_blocks[i-1].y0;
  }
  

}
/*makes the snake "slither" by first moving the head then moving each block into the position the following block was in before
also assigns snake_tail*/
makes the snake "slither" by first moving the head then moving each block into the position the following block was in before
also assigns snake_tail
makes the snake " slither " by first moving the head then moving each block into the position the following block was in before also assigns snake_tail
void move_snake(){
  void move_head(){
    int x_offset_from_dir(Direction dir){
      if(dir == 'L' || dir == 'R'){
        return (dir == 'L') ? -SPEED : SPEED; 
      }else{
        return 0;
      }
    }
    int y_offset_from_dir(Direction dir){
      if(dir == 'U' || dir == 'D'){
        return (dir == 'U') ? -SPEED : SPEED; 
      }else{
        return 0;
      }
    }
    bool user_turning_180_degrees(Direction dir){
      switch (snake.facing_direction){
      case 'U':
        return dir == 'D';
      case 'D':
        return dir == 'U';
      case 'L':
        return dir == 'R';
      case 'R':
        return dir == 'L';
      default:
        return false;
      }
    }
    Direction user_move_dir = user_move_direction();
    int i;
    if(user_move_dir == 'N' || user_turning_180_degrees(user_move_dir)){
      int x_add = x_offset_from_dir(snake.facing_direction);
      int y_add = y_offset_from_dir(snake.facing_direction);
      snake.blocks_array[0].x0 += x_add;
      snake.blocks_array[0].y0 += y_add;
    }else{
      int x_add = x_offset_from_dir(user_move_dir);
      int y_add = y_offset_from_dir(user_move_dir);
      snake.blocks_array[0].x0 += x_add;
      snake.blocks_array[0].y0 += y_add;
      snake.facing_direction = user_move_dir;
    }
  }
  Block old_blocks[snake.num_blocks];
  int j;
  for (j = 0; j < snake.num_blocks; j++)
  {
   old_blocks[j] = snake.blocks_array[j];
  }
  snake.prev_tail = old_blocks[snake.num_blocks-1];
  move_head();
  int i;
  for (i = 1; i < snake.num_blocks; i++){ 
    snake.blocks_array[i].x0 = old_blocks[i-1].x0; 
    snake.blocks_array[i].y0 = old_blocks[i-1].y0;
  }
}
void move_snake ( ) { void move_head ( ) { int x_offset_from_dir ( Direction dir ) { if ( dir == ' ' || dir == ' ' ) { return ( dir == ' ' ) ? - SPEED : SPEED ; } else { return 0 ; } } int y_offset_from_dir ( Direction dir ) { if ( dir == ' ' || dir == ' ' ) { return ( dir == ' ' ) ? - SPEED : SPEED ; } else { return 0 ; } } bool user_turning_180_degrees ( Direction dir ) { switch ( snake . facing_direction ) { case ' ' : return dir == ' ' ; case ' ' : return dir == ' ' ; case ' ' : return dir == ' ' ; case ' ' : return dir == ' ' ; default : return false ; } } Direction user_move_dir = user_move_direction ( ) ; int i ; if ( user_move_dir == ' ' || user_turning_180_degrees ( user_move_dir ) ) { int x_add = x_offset_from_dir ( snake . facing_direction ) ; int y_add = y_offset_from_dir ( snake . facing_direction ) ; snake . blocks_array [ 0 ] . x0 += x_add ; snake . blocks_array [ 0 ] . y0 += y_add ; } else { int x_add = x_offset_from_dir ( user_move_dir ) ; int y_add = y_offset_from_dir ( user_move_dir ) ; snake . blocks_array [ 0 ] . x0 += x_add ; snake . blocks_array [ 0 ] . y0 += y_add ; snake . facing_direction = user_move_dir ; } } Block old_blocks [ snake . num_blocks ] ; int j ; for ( j = 0 ; j < snake . num_blocks ; j ++ ) { old_blocks [ j ] = snake . blocks_array [ j ] ; } snake . prev_tail = old_blocks [ snake . num_blocks - 1 ] ; move_head ( ) ; int i ; for ( i = 1 ; i < snake . num_blocks ; i ++ ) { snake . blocks_array [ i ] . x0 = old_blocks [ i - 1 ] . x0 ; snake . blocks_array [ i ] . y0 = old_blocks [ i - 1 ] . y0 ; } }
makes the snake "slither" by first moving the head then moving each block into the position the following block was in before
also assigns snake_tail
makes the snake " slither " by first moving the head then moving each block into the position the following block was in before also assigns snake_tail
//moves the snakes head forward if no command is given or in another direction if the user presses one of the buttons //if the user tries to move 180 degrees from current direction or presses multiple buttons the command is ignored //called each frame //updates position of the snakes head block and snake.facing_direction /*helper that calculates how much x should be updated depending on the direction*/ // L = - and R = + /*helper that calculates how much y should be updated depending on the direction*/ // U = - and D = + /*helper that calculates if the user is trying to turn left when going right or up when going down etc. (180 degrees)*/ /*no command or invalid command - just move the snake forward*/ //update the head coordinates /*a valid command is sent - move the snake in the direction the user wants*/ //update the head coordinates //update the direction the snake is facing //copy snake blocks at t-1 - we're not interested in the entire snake just the blocks so copy those // = snake.blocks_array; //update prev_tail //old_blocks_array = snake.blocks_array; //move the head //put all other blocks at position i-1 from the snake at t-1 //the head is at index 0 //block 1 goes to the prev. head pos, block 2 goes to block 1 prev. etc.