麻烦迭代数组并排序成条件以检查每个数组项

时间:2018-04-21 01:43:44

标签: c arrays loops for-loop

我想遍历每个声明的数组all_sprites_x_coordinates[], all_sprites_width[], all_sprites_height[]并相应地将它们替换为if语句(我已在下面以示例的方式向您展示)。基本上这个功能是为了确保我游戏中的玩家安全地产生,所以它必须检查在汽车的左右两个像素距离内是否没有精灵。数组中的每个精灵都是游戏中存在的其他精灵。我一般都遇到语法和问题解决方面的问题,因为我对C非常陌生,所以简洁的解释真的有助于我理解。我也认为if语句应该在for循环中。

    bool determineSafeSpawn(sprite_id sprite1, sprite_id sprite2){
      int car_x = sprite_x(player_car);
      int all_sprites_x_coordinates[] = { sprite_x(rock), sprite_x(zombie), sprite_x(bat), sprite_x(tree), sprite_x(hill), sprite_x(bush), sprite_x(house_one), sprite_x(fuel_station)};
      int all_sprites_width[] = { ROCK_WIDTH, ZOMBIE_WIDTH, BAT_WIDTH, TREE_WIDTH, HILL_WIDTH, BUSH_WIDTH, HOUSE_ONE_WIDTH, FUEL_STATION_WIDTH};
      int all_sprites_height[] = { ROCK_HEIGHT, ZOMBIE_HEIGHT, BAT_HEIGHT, TREE_HEIGHT, HILL_HEIGHT, BUSH_HEIGHT, HOUSE_ONE_HEIGHT, FUEL_STATION_HEIGHT};

      bool flag = false;
      for (size_t i = 0; i < sizeof(all_sprites) / sizeof(sprite) && !flag; ++i) {
      //flag &&= overlaps(all_sprites[i], your_target_sprite);
        // check if any sprite within array is within 6 pixels of car
        if ((((car_x < all_sprites_x_coordinates[] + all_sprites_width[] + 6) && (car_x > all_sprites_x_coordinates[] + all_sprites_width[])) || ((car_x + CAR_WIDTH > all_sprites_x_coordinates[] - 6) && (car_x + CAR_WIDTH < all_sprites_x_coordinates[])) && speed == 0)) {
        // move left or right and search again
        } else {
          // it is safe to spawn
          return true;
        }
      }
    }

0 个答案:

没有答案