0

特定の領域にとどまるはずのヘビを作成する複数の SDL_Rects があります。「蛇」が境界に到達すると、一部が消えることがあります。

void Snake::update(SDL_Surface *screen, int level)
{
  old_pos.first = snake_rect[0];
  if(x_axis)
    snake_rect[0].x += snake_speed * level * direction_multiplier;
  else if(!x_axis)
    snake_rect[0].y += snake_speed * level * direction_multiplier;

  for(unsigned int i = 1; i < snake_rect.size(); ++i)
  {
    old_pos.second = snake_rect[i];
    snake_rect[i] = old_pos.first;
    old_pos.first = old_pos.second;
  }
  boundariesCheck(screen);
  /// Making the enemy move randomly
  if(rand() % 100 < 10)
  {
    if(x_axis)
    {
      x_axis = false;
      direction.second = rand() % 2;
      if(direction.second)
        direction_multiplier = 1;
      else if(!direction.second)
        direction_multiplier = -1;
    }
    else if(!x_axis)
    {
      x_axis = true;
      direction.first = rand() % 2;
      if(direction.first)
        direction_multiplier = 1;
      else if(!direction.first)
        direction_multiplier = -1;
    }
  }
}


void Snake::draw(SDL_Surface *screen)
{
  for(unsigned int i = 1; i < snake_rect.size(); ++i)
  {
    SDL_FillRect(screen, &snake_rect[i], 0xFF0000);
  }
  SDL_FillRect(screen, &snake_rect[0], 0xFF5500);
}
4

0 に答える 0