检测图像碰撞的最简单方法是什么?

时间:2013-12-16 17:46:38

标签: java image swing awt collision

我正在制作一个迷宫游戏,仍然需要用墙壁添加碰撞事件。我知道如何处理碰撞,但我不知道如何检测它。如有必要,我的代码如下。

public class Move extends Applet implements KeyListener, MouseListener {
    private Rectangle rect;
    private ArrayList<Integer> keysDown;
    Random randomGenerator = new Random();
    int speed = 4;
    int level = 1;     // change to 0 once start menu works
    int xpos;
    int ypos;
    public static final int START_X_POS = 160;
    public static final int START_Y_POS = 160;
    public static final int START_WIDTH = 256;
    public static final int START_HEIGHT = 64;
    boolean startClicked;
    Image start;
    Image block;
    URL base;
    MediaTracker mt;

    //int randomx = randomGenerator.nextInt(560);

    public void init() {
        addKeyListener(this);
        addMouseListener(this);
        keysDown = new ArrayList<Integer>();
        rect = new Rectangle(32, 32, 50, 50);
        mt = new MediaTracker(this);
        try {
            base = getDocumentBase();
        } catch (Exception e) {            }
        start = getImage(base, "start_button.png");
        block = getImage(base, "block.gif");
        try {
            mt.waitForAll();
        } catch (InterruptedException e) {            }
    }

    public void paint(Graphics g) {
        setSize(600, 400);
        Graphics2D g2 = (Graphics2D) g;
        if (level != 0) {
            g2.fill(rect);
            Font font = new Font("Arial", Font.BOLD, 18);
            g.setFont(font);
            //String text = "Speed: " + speed;
            String text = "Level: " + level;
            FontMetrics fm = g.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = (getHeight() - fm.getHeight()) + fm.getAscent();
            g.drawString(text, x, y);
        }

        if (level == 1) {
            // horizontal blocks
            g.drawImage(block, 0, 0, this);
            g.drawImage(block, 32, 0, this);
            g.drawImage(block, 64, 0, this);
            g.drawImage(block, 96, 0, this);

            // vertical blocks
            g.drawImage(block, 0, 0, this);
            g.drawImage(block, 0, 32, this);
            g.drawImage(block, 0, 64, this);
            g.drawImage(block, 0, 96, this);
        }
        else { // start menu
            g.drawImage(start, 160, 160, this);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (!keysDown.contains(e.getKeyCode()))
            keysDown.add(new Integer(e.getKeyCode()));
        moveRect();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        keysDown.remove(new Integer(e.getKeyCode()));
    }

    public void moveRect() {
        if (level != 0) {
            int x = rect.x;
            int y = rect.y;
            if (keysDown.contains(KeyEvent.VK_UP)) {
                y -= speed;
            }
            if (keysDown.contains(KeyEvent.VK_DOWN)) {
                y += speed;
            }
            if (keysDown.contains(KeyEvent.VK_LEFT)) {
                x -= speed;
            }
            if (keysDown.contains(KeyEvent.VK_RIGHT)) {
                x += speed;
            }
            rect.setLocation(x, y);
            repaint();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {        }

    @Override
    public void mouseClicked(MouseEvent me) {
        if (level == 0) {
            xpos = me.getX();
            ypos = me.getY();
            if (xpos >= START_X_POS 
                    && ypos >= START_Y_POS 
                    && xpos <= START_X_POS + START_WIDTH
                    && ypos <= START_X_POS + START_HEIGHT) {
                level = 1;
            }
        }
    }

    @Override
    public void mouseEntered(MouseEvent me) {        }

    @Override
    public void mouseExited(MouseEvent me) {        }

    @Override
    public void mouseReleased(MouseEvent me) {        }

    @Override
    public void mousePressed(MouseEvent me) {        }
}

感谢您的帮助!

对于这种奇怪的缩进感到抱歉......

1 个答案:

答案 0 :(得分:0)

有趣的是,缩进是关于少数事情之一,并不会让我觉得奇怪; - )

无论如何,我建议你创建一个知道它的大小和位置的Block对象。给它一个简单的#collidesWith(Rectangle)方法,并在内存中保存它们的列表,检查每一步,你就会被设置。