使用循环创建对象的 ArrayList,检查重叠对象

发布于 2024-10-06 23:40:32 字数 1630 浏览 1 评论 0原文

我正在为一个类制作一个游戏,游戏的一个元素是显示一些卷心菜,这些卷心菜存储在 ArrayList 中。这个ArrayList必须是固定数量20,其中10个是好白菜,10个是坏白菜。

创建卷心菜时,我想确保它们在显示时不会重叠。我遇到的麻烦是,当我发现重叠的卷心菜时,我不知道如何返回并在其位置创建新的卷心菜。到目前为止,当代码发现重叠时,它只会停止循环。我想我无法正确打破循环并在未填充的索引处重新启动。

这是我到目前为止所掌握的内容。任何建议将不胜感激。

        // Initialize the elements of the ArrayList = cabbages
    // (they should not overlap and be in the garden) ....
    int minX = 170 ;
    int maxX = 480;
    int minY = 15;
    int maxY = 480;
    boolean r = false;
    Cabbage cabbage;
    for (int i = 0; i < N_GOOD_CABBAGES + N_BAD_CABBAGES; i++){
        if (i % 2 == 0){
        cabbage = new GoodCabbage((int)(Math.random()* (maxX-minX + 1))+ minX,
                (int)(Math.random()*(maxY-minY + 1))+ minY,window);
        } 
        else {
            cabbage = new BadCabbage((int)(Math.random()* (maxX-minX + 1))+ minX,
                    (int)(Math.random()*(maxY-minY + 1))+ minY,window);
            }
        if (i >= cabbages.size()){
        // compares the distance between two cabbages
            for (int j = 0; j < cabbages.size(); j++){
                Point c1 = cabbage.getLocation();
                Cabbage y = (Cabbage) cabbages.get(j);
                Point c2 = y.getLocation();
                int distance = (int) Math.sqrt((Math.pow((c1.x - c2.x), 2) + Math.pow((c1.y - c2.y),2)));
                if (distance <= (CABBAGE_RADIUS*2) && !(i == j)){
                    r = true;
                }
            }
        if (r){
            break;
            }
        cabbage.draw();
        cabbages.add(i, cabbage);
        }       
    }

I'm making a game for a class and one element of the game is displaying a number of cabbages, which are stored in an ArrayList. This ArrayList must be a fixed number of 20, 10 of Good Cabbage and 10 of Bad Cabbage.

As the cabbages are created, I want to make sure they don't overlap when they are displayed. Where I'm running into trouble with this is that when I find a cabbage that overlaps, I'm not sure how to go back and create a new cabbage in its place. So far when the code finds an overlap, it just stops the loop. I guess I'm having trouble properly breaking out of a loop and restarting at the index that goes unfilled.

Here's what I have so far for this. Any suggestions would be much appreciated.

        // Initialize the elements of the ArrayList = cabbages
    // (they should not overlap and be in the garden) ....
    int minX = 170 ;
    int maxX = 480;
    int minY = 15;
    int maxY = 480;
    boolean r = false;
    Cabbage cabbage;
    for (int i = 0; i < N_GOOD_CABBAGES + N_BAD_CABBAGES; i++){
        if (i % 2 == 0){
        cabbage = new GoodCabbage((int)(Math.random()* (maxX-minX + 1))+ minX,
                (int)(Math.random()*(maxY-minY + 1))+ minY,window);
        } 
        else {
            cabbage = new BadCabbage((int)(Math.random()* (maxX-minX + 1))+ minX,
                    (int)(Math.random()*(maxY-minY + 1))+ minY,window);
            }
        if (i >= cabbages.size()){
        // compares the distance between two cabbages
            for (int j = 0; j < cabbages.size(); j++){
                Point c1 = cabbage.getLocation();
                Cabbage y = (Cabbage) cabbages.get(j);
                Point c2 = y.getLocation();
                int distance = (int) Math.sqrt((Math.pow((c1.x - c2.x), 2) + Math.pow((c1.y - c2.y),2)));
                if (distance <= (CABBAGE_RADIUS*2) && !(i == j)){
                    r = true;
                }
            }
        if (r){
            break;
            }
        cabbage.draw();
        cabbages.add(i, cabbage);
        }       
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望喜 2024-10-13 23:40:32

最简单的方法可能是添加另一个循环。

do...while 循环适用于始终需要至少一次迭代的情况。像这样的东西:

  boolean overlapped;
  do {
      // create your new cabbage here

      overlapped = /* check whether it overlaps another cabbage here */;
  } while (overlapped);

  cabbage.draw();
  cabbages.add(i, cabbage);

The easiest way to do this is probably to add another loop.

A do...while loop is suited to cases where you always need at least one iteration. Something like:

  boolean overlapped;
  do {
      // create your new cabbage here

      overlapped = /* check whether it overlaps another cabbage here */;
  } while (overlapped);

  cabbage.draw();
  cabbages.add(i, cabbage);
半仙 2024-10-13 23:40:32

看起来你正在制作卷心菜物品,然后将它们扔掉,这是一种(微不足道的)浪费。

为什么不随机选择 X 和 Y,检查该位置是否有空间,然后在有合适位置时制作卷心菜?您只需搅动数字,而不是创建和丢弃整个对象。另外,您不必为好和坏的卷心菜重复随机位置代码。

整数 x, y
做 {
// 选择 x 和 y
} while (cabbageOverlaps(x,y,list)

// 在 x,y 处创建一棵卷心菜,并将其添加到列表

boolean cabbageOverlaps(int x, int y, ArrayList existingCabbages)

It looks like you are making cabbage objects and then throwing them away, which is a (trivial) waste.

Why not pick the random X and Y, check if there is room at that spot, then make the cabbage when you have a good spot? You'll just churn through numbers, rather than making and discarding entire Objects. Plus you won't have to repeat the random location code for good and bad cabbages.

int x, y
do {
// pick x and y
} while (cabbageOverlaps(x,y,list)

// create a cabbage at that x,y, and add it to list

boolean cabbageOverlaps(int x, int y, ArrayList existingCabbages)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文