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