title: Collision detection in maze 2D java game
body: <p>I'm trying to make my first Pacman game but i have met a wall that i can't seem to smash on my own :(</p>
  
  <p>It's about how to detect collision in my game, so the pacman can't go through barriers / walls. I have made it so it can't go outside of the screen with this code:</p>
  
  <pre><code>    if (pacman_x &lt; 27) {
          pacman_velX = 0;
          pacman_x = 27;
      }
      if (pacman_y &lt; 27) {
          pacman_velY = 0;
          pacman_y = 27;
      }
      if (pacman_x &gt; 621) {
          pacman_velX = 0;
          pacman_x = 621;
      }
      if (pacman_y &gt; 513) {
          pacman_velY = 0;
          pacman_y = 513;
      }
  </code></pre>
  
  <p>, but if i have a rectangle on the board in middle of the screen, i don't know how to program it so it will stop just before a wall.</p>
  
  <p>I have uploaded a foto of the map: 
  <img src="https://i.stack.imgur.com/uFnb4.png" alt="MY game"></p>
  
  <p><strong>I need to stop my pacman to move onto the wall inside the arena as you see (the rectangle on top to the left)</strong></p>
  
  <p>My code of the Board class:</p>
  
  <pre><code>public class Board extends JPanel implements ActionListener, KeyListener {
  
  private Timer timer;
  private Map m;
  private Pacman p;
  int pacman_x = 27, pacman_y = 27, pacman_velX = 0, pacman_velY = 0; 
  
  public Board() 
  {
      m = new Map();
      p = new Pacman();
      timer = new Timer(10, this);
      timer.start();
      addKeyListener(this);
      setFocusable(true);
      setFocusTraversalKeysEnabled(false); // So we can't use shift 
  }
  
  public void actionPerformed(ActionEvent e) {
      pacman_x = pacman_x + pacman_velX;
      pacman_y = pacman_y + pacman_velY;
      repaint();          
  
      if (pacman_x &lt; 27) {
          pacman_velX = 0;
          pacman_x = 27;
      }
      if (pacman_y &lt; 27) {
          pacman_velY = 0;
          pacman_y = 27;
      }
      if (pacman_x &gt; 621) {
          pacman_velX = 0;
          pacman_x = 621;
      }
      if (pacman_y &gt; 513) {
          pacman_velY = 0;
          pacman_y = 513;
      }
  
      if (pacman_x &gt;= 150 &amp;&amp; pacman_y &gt;= 27) {
          pacman_velX = 0;
          pacman_x = 27;
      }
  
  }
  
  public void paint(Graphics g) {
      super.paint(g);
  
      Graphics2D g2d = (Graphics2D) g;
  
      for(int y = 0; y &lt; 21; y++) {
          for (int x = 0; x &lt; 25; x++) {
              if (m.getMap(x, y).equals("b")) {
                  g.drawImage(m.getBarrier(), x * 27, y * 27, null);
              }
              if (m.getMap(x, y).equals("s")) {
                  g.drawImage(m.getSpace(), x * 27, y * 27, null);
              }
          }
  
      }
  
      // Place pacman on board        
      g.drawImage(p.getPacman(), pacman_x , pacman_y, null);  
  
      // Create Rectangles barriers
      Rectangle r1 = new Rectangle(150, 27, 27, 27);
  
      g2d.setColor(new Color(63, 72, 204, 250));      
      g.fillRect(r1.x, r1.y, 27, 27);
  
      // Sets Color on lives
      int lives = 3;
      g2d.setColor(new Color(255, 0, 0, 250));
      g2d.drawString("Lives left: " + lives, 20, 20);
  
      // Sets Color on Scoreboard text
      int point = 0;      
      g2d.setColor(new Color(255, 0, 0, 250));
      g2d.drawString("Score: " + point, 20, 550);
  }
  
  @Override
  public void keyPressed(KeyEvent e) {
  
      int pacman_direction = e.getKeyCode();
  
      if (pacman_direction == KeyEvent.VK_LEFT) {
  
          pacman_velX = -3;
          pacman_velY = 0;
  
      }       
  
      if (pacman_direction == KeyEvent.VK_UP) {
  
          pacman_velX = 0;
          pacman_velY = -3;           
  
      }
  
      if (pacman_direction == KeyEvent.VK_RIGHT) {
  
          pacman_velX = 3;
          pacman_velY = 0;
  
      }
  
      if (pacman_direction == KeyEvent.VK_DOWN) {
  
          pacman_velX = 0;
          pacman_velY = 3;    
  
      }
  
  }
  </code></pre>
  
  <p>Hope someone can tell me what to do.. can't seem to find an easy example tutorial on the internet that explain the thing that i want it to do!</p>