简单的突围游戏中的碰撞检测

问题描述 投票:0回答:1

我正在用java制作一个简单的突破游戏作为大学作业,我需要迫切的帮助来使上面的方块具有命中检测功能。分配的详细信息是需要至少 3 行,其中每行具有不同的命中数。例如,第一行需要 1 次点击才能消失,然后第二行需要 2 次点击,第三行需要 3 次。我想你明白了,但这大致就是我所需要的。

我创建了一个名为 SquareCollection 的类,它不扩展任何内容。在 SquareCollection 中,我制作了三个数组,我将它们与其余数组一起粘贴在下面。这三个我已经在主类中画好了,现在我需要的是与球的碰撞检测。

package Game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class SquareCollection {

    private int hits;
    private ArrayList<ColoredBox> row1; 
    private ArrayList<ColoredBox> row2;
    private ArrayList<ColoredBox> row3; 

    public SquareCollection() {
        //super(x, y, Constants.SquareWidth, Constants.SquareHeight);
        hits = 0;

        row1=new ArrayList<ColoredBox>();
        for (int i = 0; i < 8; i++) {
            row1.add(new ColoredBox(i*100 +20, 20,Constants.SquareWidth , Constants.SquareHeight, Color.black));
        }

        row2=new ArrayList<ColoredBox>();
        for (int j = 0; j < 8; j++) {
            row2.add(new ColoredBox(j*100 +20, 80,Constants.SquareWidth , Constants.SquareHeight, Color.black));
        }

        row3=new ArrayList<ColoredBox>();
        for (int k = 0; k < 8; k++) {
            row3.add(new ColoredBox(k*100 +20, 140,Constants.SquareWidth , Constants.SquareHeight, Color.black));
        }
    }

    public void update(Keyboard keyboard) {

    }
    public void draw(Graphics2D graphics) {
        for(int i = 0;i < row1.size();i++) {
            row1.get(i).draw(graphics);
        }
        for(int j = 0; j < row1.size(); j++) {
            row2.get(j).draw(graphics);
        }
        for(int k = 0; k < row1.size(); k++) {
            row3.get(k).draw(graphics);
        }
    }   
}

我知道这是一个相当微不足道的问题,但我对编码和 Java 总体来说非常陌生,所以如果这“太简单”请原谅我,但我的大脑对此感到困难。另外,如果有“代码转储”以及是否难以阅读,请通知我。如果您需要详细信息,我很乐意提供帮助。

java game-physics breakout
1个回答
0
投票

如果游戏不必是完美的 - 似乎不必如此,因为这是学校里的一些有趣的小项目 - 你可以将球“包裹”到看不见的方块中。然后很容易检测到该正方形与任何其他盒子的碰撞。

例如,在这里您可以看到如何进行矩形碰撞:java中两个矩形之间的碰撞检测有一个简单的答案建议这个解决方案:

if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
    RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1) 
© www.soinside.com 2019 - 2024. All rights reserved.