0

What I want is that when I press the arc shape(in this case the pacman)and collided with the oval shape(its food) the oval shape will randomly go display to another location in the frame.

Jay Gorio
  • 335
  • 2
  • 4
  • 22

2 Answers2

2

The basic idea is to determine if the player intersects the food, something like...

public boolean intersects() {
    int tx = xLocation;
    int ty = yLocation;
    int rx = xrandomLocation;
    int ry = yrandomLocation;
    int rw = 20 + rx;
    int rh = 20 + ry;
    int tw = 100 + tx;
    int th = 100 + ty;
    //      overflow || intersect
    return ((rw < rx || rw > tx)
            && (rh < ry || rh > ty)
            && (tw < tx || tw > rx)
            && (th < ty || th > ry));
}

When the method returns true, you want to calculate a new location off the food

Maybe something like...

 private Random random = new Random();
 //...
 xrandomLocation = random.nextInt(getWidth()) - 20;
 yrandomLocation = random.nextInt(getHeight()) - 20;

Java's 2D Graphics API has a really powerful shapes API which allows you to check for collisions more simply

You should also stop using KeyListener API and start using the Key Bindings API, which help solve the focus related issues the KeyListener suffers from.

Also, all the suggestions I made in your last question still stand

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-2

Your structure is a bit off, There is no need for the PacmanObject to extend JPannel or implement KeyListener. Wherever your JPannel is, it will need to use the method addKeyListener, the KeyListener is usually anonymous. Yes you will need to check whether the food and the pacman are in the same position after every time the pacman moves. You will also need to generate a random position but neither of these things are hard. I have written a frame for you to write your code into, of course the nested classes should probably be moved to their own files.

package pacman;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Pacman extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Pacman frame = new Pacman();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Pacman() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new View();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
    }

    public class View extends JPanel {

        PacmanObject p;
        FoodObject f;

        /**
         * Create the panel.
         */
        public View() {
            super();
            addKeyListener(new KeyListener() {

                @Override
                public void keyTyped(KeyEvent e) {

                }

                @Override
                public void keyPressed(KeyEvent e) {
                    onKeyEvent(e);
                }

                @Override
                public void keyReleased(KeyEvent e) {

                }

            });
        }


        public void onKeyEvent(KeyEvent keyPress) {
            p.move(keyPress);
            if (/* check if f and p colide*/true) {
                f.reposition();
            }
            p.repaint(this.getGraphics());
            f.repaint(this.getGraphics());
        }

    }

    public class FoodObject {

        // Fields

        FoodObject() {
            // init fields
        }

        public void repaint(Graphics graphics) {
            // repaint
        }

        public void reposition() {
            // generate random position
        }

    }

    public class PacmanObject {

        // Fields

        PacmanObject() {
            // init fields
        }

        public void repaint(Graphics graphics) {
            // repaint
        }

        public void move(KeyEvent keyPress) {
            /// Move pacman
        }
    }

}

Hope that helps

Devman
  • 310
  • 2
  • 10
  • Mistake number one- propagating the use of `KeyListener` when better APIs exist; Mistake number two- using `getGraphics` – MadProgrammer Feb 12 '16 at 02:38
  • No mistake one would be trying to use swing in the first place. I'm just trying to help the guy get a working program. So what if its not using the most efficient API? – Devman Feb 12 '16 at 02:46
  • 2
    *"So what if its not using the most efficient API?"* - Because it's propagating bad ideas and a solution which is going to, in the short and long term, cause more issues to the OP - a mess the rest of us we are going to have clean up. As professional forum, we should present professional solutions to people's problems, but of course, if you want to post rubbish, that's your choice – MadProgrammer Feb 12 '16 at 02:51
  • So tell us which API to use instead. Don't just go tell everyone they are wrong. Feel free to edit my answer until you are happy with it. – Devman Feb 12 '16 at 03:11
  • Well, start by taking a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) to see how painting should be done (ps- the OP's on the right track) and [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for a replacement for `KeyListener` and all it's problems – MadProgrammer Feb 12 '16 at 03:18
  • There you go, go ahead and use my answer if you like but please have a look at the documentation the @MadProgrammer suggested as it may save you some troubles down the road. – Devman Feb 12 '16 at 03:30