交叉后如何使矩形消失

时间:2014-05-29 08:06:01

标签: java intersection rectangles

我正在构建一个小程序,我希望当主要方块为它们加热时,称为检查点的矩形会消失。

到目前为止,我使用的代码是:

if (antalCheckpoints == 5) {
        lblGameOver.setText("Du vann!");
        btnUpp.setEnabled(false); // Upp = up
        btnNer.setEnabled(false); //Ner = down
        btnHöger.setEnabled(false); // Höger = right
        btnVänster.setEnabled(false); //Vänster = left
    }
    if (spelare.intersects(checkpoint1)) {
        checkpoint1.resize(0, 0);
        antalCheckpoints++;
        repaint();
    }
    if (spelare.intersects(checkpoint2)) {
        checkpoint1.setSize(0, 0);
        antalCheckpoints++;
        repaint();
    }
    if (spelare.intersects(checkpoint3)) {
        checkpoint1.setSize(0, 0);
        antalCheckpoints++;
        repaint();
    }
    if (spelare.intersects(checkpoint4)) {
        checkpoint1.resize(0,0);
        antalCheckpoints++;
        repaint();
    }
    if (spelare.intersects(checkpoint5)) {
        checkpoint1.setSize(0, 0);
        antalCheckpoints++;
        repaint();
    }

spelare是主要的玩家方格,检查点是检查点方格。 antalCheckpoints中找到的检查点数量。我希望检查点在触摸时消失,我希望在到达所有检查点时antalCheckpoints都会消失。

就像现在一样,当我触摸第一个检查点时,if(antalCheckpoints==5){熄灭,游戏说它已经完成,而检查点仍在那里。

我尝试了.resize.setSize广场,但他们仍然不会消失。

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:1)

  • 创建代表检查点的List Rectangle
  • 在每次更新时,请浏览此List并检查哪些内容发生冲突,从List
  • 中删除
  • 绘画时,只需重复List并绘制
  • 中包含的内容。
  • Rectangle中不再有List s时,您就赢了:)

更新了示例

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CheckPointCharlie {

    public static void main(String[] args) {
        new CheckPointCharlie();
    }

    public CheckPointCharlie() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new CheckPointsPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class CheckPointsPane extends JPanel {

        private List<Rectangle> checkpoints;
        private Rectangle player;

        public CheckPointsPane() {
            checkpoints = new ArrayList<>(20);
            int count = (int)(Math.random() * 20);
            while (checkpoints.size() < count) {
                int x = (int)(Math.random() * 195);
                int y = (int)(Math.random() * 195);
                Rectangle rect = new Rectangle(x, y, 5, 5);
                boolean keep = true;
                for (Rectangle check : checkpoints) {
                    if (check.intersects(rect)) {
                        keep = false;
                        break;
                    }
                }
                if (keep) {
                    checkpoints.add(rect);
                }
            }
            player = new Rectangle(95, 95, 10, 10);
            checkCollisions();

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Up");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Down");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Left");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Right");

            ActionMap am = getActionMap();
            am.put("Up", new MovementAction(this, player, 0, -4));
            am.put("Down", new MovementAction(this, player, 0, 4));
            am.put("Left", new MovementAction(this, player, -4, 0));
            am.put("Right", new MovementAction(this, player, 4, 0));
        }

        protected void checkCollisions() {
            Iterator<Rectangle> it = checkpoints.iterator();
            while (it.hasNext()) {
                Rectangle check = it.next();
                if (player.intersects(check)) {
                    it.remove();
                }
            }
            repaint();
            if (checkpoints.isEmpty()) {
                JOptionPane.showMessageDialog(this, "You rock!");
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            for (Rectangle rect : checkpoints) {
                g2d.fill(rect);
            }
            g2d.setColor(Color.BLUE);
            g2d.fill(player);
            g2d.dispose();
        }
    }

    public class MovementAction extends AbstractAction {

        private CheckPointsPane parent;
        private Rectangle player;
        private int xDelta;
        private int yDelta;

        private MovementAction(CheckPointsPane parent, Rectangle player, int xDelta, int yDelta) {
            this.parent = parent;
            this.player = player;
            this.xDelta = xDelta;
            this.yDelta = yDelta;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            player.x += xDelta;
            player.y += yDelta;
            if (player.x < 0) {
                player.x = 0;
            } else if (player.x + player.width > parent.getWidth()) {
                player.x = parent.getWidth() - player.width;
            }
            if (player.y < 0) {
                player.y = 0;
            } else if (player.y + player.height > parent.getHeight()) {
                player.y = parent.getHeight() - player.height;
            }
            parent.checkCollisions();
        }

    }

}