Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Packman中分解矩形点_Java_Swing_Oop_Collision Detection_Swingutilities - Fatal编程技术网

Java 如何在Packman中分解矩形点

Java 如何在Packman中分解矩形点,java,swing,oop,collision-detection,swingutilities,Java,Swing,Oop,Collision Detection,Swingutilities,我的游戏类似于小贩。我的问题是,如果小贩吃了这个点,除了第一个点会像背景一样改变颜色外,每个点都不会消失。我知道我有一个方法,但当我只画一个点时,它做了我想要的。我只是想澄清一下派克曼吃了什么。我在WindowBuilder中创建了这个窗口(我只是想试试),我希望它不会有问题 public class Hra extends JFrame { private JPanel contentPane; PackMan packman ; Points point ;

我的游戏类似于小贩。我的问题是,如果小贩吃了这个点,除了第一个点会像背景一样改变颜色外,每个点都不会消失。我知道我有一个方法,但当我只画一个点时,它做了我想要的。我只是想澄清一下派克曼吃了什么。我在WindowBuilder中创建了这个窗口(我只是想试试),我希望它不会有问题

public class Hra extends JFrame {

    private JPanel contentPane;
    PackMan packman ;
    Points point ;
    boolean check;
    ArrayList<Body> points = new ArrayList<Body>();

   static int x =900;
   static int y=600;    
   Color packCol = Color.BLACK;
   Color pointCol = Color.WHITE;

        /**
     * Launch the application.
     */
    public static void main(String[] args) {
        
                    Game frame = new Game();
                    frame.setVisible(true);
            
    }
public void inicialization() {
    for (int i = 0; i < 4; i++) {
        Point point = new Point(x, y, 20, pointCol);
        x +=100;
        points.add(point);
    }


}



 public GAME() {
             inicialization();
       
        packman = new PackMan(0, 900, 900,packCol);

        point = new point(900,800,20,pointCol);

        check = false;
        
           
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setBounds(100, 100, 450, 300);

        contentPane = new JPanel();

        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);

        getContentPane().setBackground(Color.YELLOW);

    JLabel lblNewLabel = new JLabel("Score:" + packman.getScore());
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(309, Short.MAX_VALUE))
        );
        


 gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(229, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);
        
        this.addKeyListener(new KeyListener() { 
    

    @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                switch(e.getKeyCode()) {
                case 37: //left
                    packman.setCoordinatesX(packman.getCoordinatesX()-10);
                    repaint();
                    chechCollision();
                    break;
                case 38: //up
                    packman.setCoordinatesY(packman.getCoordinatesY()-10);
                    repaint();
                    chechCollision();
                    break;
                case 39://right
                    packman.setCoordinatesX(packman.getCoordinatesX()+10);
                    repaint();
                    chechCollision();
                    break;
                case 40://down
                    packman.setCoordinatesY(packman.getCoordinatesY()+10);
                    chechCollision();
                    repaint();
                    break;
                }
                
            
            }
            
            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                
            }
        });
    }
    

@Override
        public void paint(Graphics g) {
            // TODO Auto-generated method stub
            super.paint(g);
        g.fillRect(packman.getCoordinatesX(), packman.getCoordinatesY(), 50, 50);
    g.setColor(point.getColor());
    
    g.fillRect(point.getPointX(), body.getPointY(), 50, 50);
    g.setColor(Color.BLACK);


 for(int i =0;i<points.size();i++){
    if (kontrola) {
        g.clearRect(points.get(i).getPointX(), points.get(i).getPointY(), 50, 50);
        


  } 


}


            for (int i = 0; i < bodiky.size(); i++) {
        g.fillRect(points.get(i).getPointX(), points.get(i).getPointY(), 50, 50);
        g.setColor(points.get(i).getColor());
        
    }
        }   
            
        

    



            
        }

        public void checkCollision() {
            if (packman.getCoordinatesX() == point.getPointX() &&  packman.getCoordinatesY() == point.getPointY()) {
            packman.setScore(packman.getScore() + point.getValueOfPoint());
        lblNewLabel.setText("Score:" + packman.getScore() );
            check = true;
            point.setColor(Color.YELLOW);
            point.setValueOfPoint(0);
            repaint();
        }
                
            }
        }
    }

    public class Point{
    private Color color;
    private int pointX;
    private int pointY;
    private double valueofPoint;
    public int getCoordinatesPointX() {
        return pointX;
    }
    public Point(int pointX, int pointY, double valueofPoint,Color color) {
        super();
        this.pointX = pointX;
        this.pointY = pointY;
        this.valueofPoint= valueofPoint;
        this.color = color;
    }
    public void sePointX(int pointX) {
        this.pointX = pointX;
    }
    public int getPointY() {
        return pointY;
    }
    public void setPointY(int pointY) {
        this.pointY = pointY;
    }
    public double getValueofPoint() {
        return valueofPoint;
    }
    public void setValueofPoint(double valueofPoint) {
        this.valueofPoint = valueofPoint;
    }
    public void setColor(Color color){
        this.color = color;
}
public Color getColor(){
return color;
}        
}

    public class PackMan {
    
    private double score;
    private int coordinatesX;
    private int coordinatesY;
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public int getCoordinatesX() {
        return coordinatesX;
    }
    public void setCoordinatesX(int coordinatesX) {
        this.coordinatesX = coordinatesX;
    }
    public int getCoordinatesY() {
        return coordinatesY;
    }
    public void setCoordinatesY(int coordinatesY) {
        this.coordinatesY = coordinatesY;
    }
    public PackMan(double score, int coordinatesX, int coordinatesY) {
        super();
        this.score = score;
        this.coordinatesX = coordinatesX;
        this.coordinatesY = coordinatesY;
        
    }
    
    
    }
公共类Hra扩展JFrame{
私有JPanel内容窗格;
小贩小贩;
点数;
布尔检查;
ArrayList points=新的ArrayList();
静态int x=900;
静态int y=600;
Color packCol=Color.BLOCK;
Color pointCol=Color.WHITE;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
游戏框架=新游戏();
frame.setVisible(true);
}
公共空间非专业化(){
对于(int i=0;i<4;i++){
点=新点(x,y,20,pointCol);
x+=100;
点。添加(点);
}
}
公共游戏(){
非专业化();
包装工=新包装工(0,900,900,包装工);
点=新点(900800,20,点col);
检查=错误;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100450300);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
getContentPane().setBackground(颜色:黄色);
JLabel lblNewLabel=新JLabel(“分数:+packman.getScore());
GroupLayout gl_contentPane=新的GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel,GroupLayout.PREFERRED\u大小,107,GroupLayout.PREFERRED\u大小)
.addContainerGap(309,简称最大值))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel,GroupLayout.PREFERRED\u大小,24,GroupLayout.PREFERRED\u大小)
.addContainerGap(229,简称最大值))
);
contentPane.setLayout(gl_contentPane);
this.addKeyListener(新的KeyListener(){
@凌驾
public void keyTyped(KeyEvent e){
//TODO自动生成的方法存根
}
@凌驾
公共无效密钥已释放(密钥事件e){
//TODO自动生成的方法存根
开关(如getKeyCode()){
案例37://左
packman.setCoordinateESX(packman.getCoordinateESX()-10);
重新油漆();
chechCollision();
打破
案例38://以上
packman.setCoordinates(packman.getCoordinates()-10);
重新油漆();
chechCollision();
打破
案例39://对
packman.setCoordinateESX(packman.getCoordinateESX()+10);
重新油漆();
chechCollision();
打破
案例40://向下
packman.setCoordinates(packman.getCoordinates()+10);
chechCollision();
重新油漆();
打破
}
}
@凌驾
按下公共无效键(按键事件e){
//TODO自动生成的方法存根
}
});
}
@凌驾
公共空间涂料(图g){
//TODO自动生成的方法存根
超级油漆(g);
g、 fillRect(packman.getCoordinateESX(),packman.getCoordinatesY(),50,50);
g、 setColor(point.getColor());
g、 fillRect(point.getPointX(),body.getPointY(),50,50);
g、 设置颜色(颜色为黑色);
对于(int i=0;i

公共类Hra扩展JFrame{
定时器;
私有JPanel内容窗格;
小贩小贩;
点数;
布尔检查;
ArrayList points=新的ArrayList();
静态int x=900;
静态int y=600;
Color packCol=Color.BLOCK;
Color pointCol=Color.WHITE;
静态方向vysledek=新方向(空);
静态int newX=900;
静态int newY=200;
静态int xVel=1;
静态int-yVel=1;
静态整数计数=1;
静态JLabel giflable=newjlabel(newimageicon(//图像路径));
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
游戏框架=新游戏();
frame.setVisible(true);
frame.getContentPane().add(gifLabel);
gifLabel.setLocation(packman.getCoordinateESX(),packman.getCoordinatesY()-38);
}
公共空间非专业化(){
对于(int i=0;i<4;i++){
点=新点(x,y,20,pointCol);
y-=100;
点。添加(点);
}
对于(int i=0;i<4;i++){
点p=新点(newX,newY,20,pointCol);
newX-=100;
增加(p);
}
}
公共游戏(){
非专业化();
包装工=新包装工(0,900,900,包装工);
点=新点(900800,20,点col);
胆碱酯酶
public void checkCollision() {
            if (packman.getCoordinatesX() == point.getPointX() &&  packman.getCoordinatesY() == point.getPointY()) {
            packman.setScore(packman.getScore() + point.getValueOfPoint());
        lblNewLabel.setText("Score:" + packman.getScore() );
            check = true;
            point.setColor(Color.YELLOW);
            point.setValueOfPoint(0);
            repaint();
            points.remove(i);
        }
    public class Hra extends JFrame {
        Timer timer;
        private JPanel contentPane;
        PackMan packman ;
        Points point ;
        boolean check;
        ArrayList<Point> points = new ArrayList<Point>();
      
       static int x =900;
       static int y=600;    
       Color packCol = Color.BLACK;
       Color pointCol = Color.WHITE;
       static Direction vysledek = new Direction(null);
       static int newX = 900;
       static int newY =200;
       static int xVel = 1;
       static int yVel = 1;
       static int count = 1;
       static JLabel gifLabel = new JLabel(new ImageIcon(//path of Image));
      
            /**
         * Launch the application.
         */
        public static void main(String[] args) {
            
                        Game frame = new Game();
                        frame.setVisible(true);
                        frame.getContentPane().add(gifLabel);                                                                  
   gifLabel.setLocation(packman.getCoordinatesX(),packman.getCoordinatesY()-38);
                    
        }
    public void inicialization() {
        for (int i = 0; i < 4; i++) {
            Point point = new Point(x, y, 20, pointCol);
            y -=100;
            points.add(point);
        }
    for (int i = 0; i < 4; i++) {       
        Point p = new Point(newX, newY, 20, pointCol);
        newX-=100;
        points.add(p);
    }
    
    }
    
    
    
     public GAME() {
                 inicialization();
           
            packman = new PackMan(0, 900, 900,packCol);
    
            point = new point(900,800,20,pointCol);
    
            check = false;
            
            timer = new Timer(10,this);
            timer.start();
               
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            setBounds(100, 100, 450, 300);
    
            contentPane = new JPanel();
    
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    
            setContentPane(contentPane);
    
            getContentPane().setBackground(Color.YELLOW);
    
        JLabel lblNewLabel = new JLabel("Score:" + packman.getScore());
            GroupLayout gl_contentPane = new GroupLayout(contentPane);
            gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_contentPane.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(309, Short.MAX_VALUE))
            );
            
    
    
     gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_contentPane.createSequentialGroup()
                        .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(229, Short.MAX_VALUE))
            );
            contentPane.setLayout(gl_contentPane);
            
            this.addKeyListener(new KeyListener() { 
        
    
        @Override
                public void keyTyped(KeyEvent e) {
                    // TODO Auto-generated method stub
                    
                }
                
                @Override
                public void keyReleased(KeyEvent e) {
                    // TODO Auto-generated method stub
                    switch(e.getKeyCode()) {
                    case 37: //left
                        count = 1;
                        repaint();
                        chechCollision();
                        break;
                    case 38: //up
                        count =2;
                        repaint();
                        chechCollision();
                        break;
                    case 39://right
                        count = 3;
                        repaint();
                        chechCollision();
                        break;
                    case 40://down
                        count = 4;
                        chechCollision();
                        repaint();
                        break;
                    }
                    
                
                }
                
                @Override
                public void keyPressed(KeyEvent e) {
                    // TODO Auto-generated method stub
                    
                }
            });
        }
        
    
    @Override
            public void paint(Graphics g) {
                // TODO Auto-generated method stub
                super.paint(g);
            g.fillRect(packman.getCoordinatesX(), packman.getCoordinatesY(), 50, 50);
        g.setColor(point.getColor());
        
      
    
    
            
    
    
      
    
    
   
    
    
                for (int i = 0; i < points.size(); i++) {            
          g.drawOval(points.get(i).getPointX(), points.get(i).getPointY(), 50, 50);
          g.fillOval(points.get(i).getPointX(), points.get(i).getPointY(), 50, 50);
            
        }
            }   
                
            
    
        
    
    
    
                
            }
    
            public void checkCollision() {
                if (packman.getCoordinatesX() == point.getPointX() &&  packman.getCoordinatesY() == point.getPointY()) {
                packman.setScore(packman.getScore() + point.getValueOfPoint());
            lblNewLabel.setText("Score:" + packman.getScore() );
                points(i);
                repaint();
            }
                    
                }
            }
        
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (count == 1) {
            packman.setCoordinatesX(packman.getCoordinates() - xVel);
            gifLabel.setLocation(packman.getCoordinatesX(), packman.getCoordinatesY()-38);
            repaint();
         chechCollision();
        
        }
        
        if (count ==2) {
            packman.setCoordinatesY(packman.getCoordinatesY() - yVel);
            gifLabel.setLocation(packman.getCoordinatesX(), packman.getCoordinatesY()-38);
            repaint();
             chechCollision();
        }
        if (count ==3) {
            packman.setCoordinatesX(packman.getSouradniceX() + xRychlost);
            gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
            repaint();
             chechCollision();
        }
        if (count ==4) {
            packman.setCoordinatesY(packman.getCoordinatesY() + yVel);
            gifLabel.setLocation(packman.getSouradniceX(), packman.getCoordinatesY()-38);
            repaint();
             chechCollision();
        }
        
    }
      
   public class Direction {
private Directions directions;
private int index;


public Smery getDirection() {
    return directions;
}
public void setSmery(Directions directions) {
    this.directions = directions;
}
public directions(Directions directions) {
    super();
    this.directions = directions;

        
}

public enum Smery {
doprava,doleva,nahoru,dolu;
}

   
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class PackmanGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new PackmanGUI());
    }
    
    private DrawingPanel drawingPanel;
    
    private final GameModel model;
    
    private JFrame frame;
    
    private JPanel scorePanel;
    
    private JTextField scoreField;
    
    private Timer timer;
    
    public PackmanGUI() {
        this.model = new GameModel();
    }

    @Override
    public void run() {
        frame = new JFrame("Packman");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        scorePanel = createScorePanel();
        frame.add(scorePanel, BorderLayout.BEFORE_FIRST_LINE);
        drawingPanel = new DrawingPanel(model);
        frame.add(drawingPanel, BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        
        setKeyBindings();
        
        timer = new Timer(40, new MotionListener(this, model));
        timer.start();
    }
    
    private void setKeyBindings() {
        String up = "up";
        String down = "down";
        String left = "left";
        String right = "right";
        String pressed = " pressed";
        String released = " released";
        
        InputMap inputMap = drawingPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = drawingPanel.getActionMap();
        
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), up + pressed); 
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), down + pressed); 
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), left + pressed);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), right + pressed);
        
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), up + released); 
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), down + released); 
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), left + released);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), right + released);
        
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), up + pressed); 
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), down + pressed); 
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), left + pressed);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), right + pressed);
        
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), up + released); 
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), down + released); 
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), left + released);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), right + released);
        
        Packman packman = model.getPackman();
        actionMap.put(up + pressed, new NavigationAction(Direction.UP, packman));
        actionMap.put(down + pressed, new NavigationAction(Direction.DOWN, packman));
        actionMap.put(left + pressed, new NavigationAction(Direction.LEFT, packman));
        actionMap.put(right + pressed, new NavigationAction(Direction.RIGHT, packman));
        
        actionMap.put(up + released, new NavigationAction(Direction.NONE, packman));
        actionMap.put(down + released, new NavigationAction(Direction.NONE, packman));
        actionMap.put(left + released, new NavigationAction(Direction.NONE, packman));
        actionMap.put(right + released, new NavigationAction(Direction.NONE, packman));
    }
    
    private JPanel createScorePanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBackground(Color.YELLOW);
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        Font font = panel.getFont().deriveFont(Font.BOLD, 36f);
        
        JLabel label = new JLabel("Score:");
        label.setFont(font);
        panel.add(label);
        
        scoreField = new JTextField(10);
        scoreField.setBackground(Color.YELLOW);
        scoreField.setEditable(false);
        scoreField.setFont(font);
        scoreField.setHorizontalAlignment(JTextField.TRAILING);
        updateScore(model.getPackman().getScore());
        panel.add(scoreField);
        
        return panel;
    }
    
    public void updateScore(double score) {
        scoreField.setText(Double.toString(score));
    }

    public JFrame getFrame() {
        return frame;
    }
    
    public void repaint() {
        drawingPanel.repaint();
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        
        private final GameModel model;
        
        public DrawingPanel(GameModel model) {
            this.model = model;
            this.setBackground(Color.BLACK);
            this.setPreferredSize(new Dimension(800, 800));
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            Packman packman = model.getPackman();
            Point point = packman.getCenterPoint();
            
            // Draw body
            int x = point.x - 25;
            int y = point.y - 25;
            
            g.setColor(Color.YELLOW);
            g.fillArc(x, y, 50, 50, 30, 300);
            
            // Draw eye
            x = point.x;
            y = point.y - 18;
            
            g.setColor(Color.BLACK);
            g.fillOval(x, y, 8, 8);
            
            // Draw pellets
            for (Pellet pellet : model.getPellets()) {
                g.setColor(pellet.getColor());
                point = pellet.getPoint();
                x = point.x - 10;
                y = point.y - 10;
                g.fillOval(x, y, 20, 20);
            }
        }
        
    }
    
    public interface KeyDirectionActionHandler {
        public void keyDirectionActionPerformed(Direction direction);
    }
    
    public class NavigationAction extends AbstractAction {

        private static final long serialVersionUID = 1L;
        
        private Direction direction;
        
        private KeyDirectionActionHandler keyDirectionActionHandler;
        
        public NavigationAction(Direction direction, 
                KeyDirectionActionHandler keyDirectionActionHandler) {
            this.direction = direction;
            this.keyDirectionActionHandler = keyDirectionActionHandler;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            keyDirectionActionHandler.keyDirectionActionPerformed(direction);
        }
        
    }
    
    public enum Direction {
        NONE, UP, DOWN, RIGHT, LEFT;
    }

    
    public class MotionListener implements ActionListener {
        
        private final PackmanGUI frame;
        
        private final GameModel model;

        public MotionListener(PackmanGUI frame, GameModel model) {
            this.frame = frame;
            this.model = model;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            model.getPackman().move();
            model.checkCollisions();
            frame.updateScore(model.getPackman().getScore());
            frame.repaint();
        }
        
    }
    
    public class GameModel {
        
        private final List<Pellet> pellets;
        
        private final Packman packman;
        
        public GameModel() {
            this.packman = new Packman(0, new Point(400, 400));
            this.pellets = new ArrayList<>();
            inicialization();
        }
        
        public void inicialization() {
            int x = 600;
            int y = 600;
            
            for (int i = 0; i < 4; i++) {
                Pellet pellet = new Pellet(new Point(x, y), 20, Color.WHITE);
                y -= 100;
                pellets.add(pellet);
            }
            
            for (int i = 0; i < 4; i++) {
                Pellet pellet = new Pellet(new Point(x, y), 20, Color.WHITE);
                x -= 100;
                pellets.add(pellet);
            }
        }
        
        public void checkCollisions() {
            Point packmanPoint = packman.getCenterPoint();
            for (int index = 0; index < pellets.size(); index++) {
                Pellet pellet = pellets.get(index);
                Point pelletPoint = pellet.getPoint();
                
                // Collision detection
                int deltaX = packmanPoint.x - pelletPoint.x;
                int deltaY = packmanPoint.y - pelletPoint.y;
                double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
                if (distance < 20) {
                    pellets.remove(pellet);
                    packman.incrementScore(pellet.getValue());
                }
            }
        }
            

        public List<Pellet> getPellets() {
            return pellets;
        }

        public Packman getPackman() {
            return packman;
        }
        
    }
    
    public class Packman implements KeyDirectionActionHandler {
        
        private double score;
        
        private Direction direction;
        
        private Point centerPoint;

        public Packman(double score, Point centerPoint) {
            this.score = score;
            this.centerPoint = centerPoint;
            this.direction = Direction.NONE;
        }

        public double getScore() {
            return score;
        }

        public void incrementScore(double score) {
            this.score += score;
        }

        public Point getCenterPoint() {
            return centerPoint;
        }

        public void setCenterPoint(Point centerPoint) {
            this.centerPoint = centerPoint;
        }
        
        public void move() {
            int distance = 6;
            int deltaX = 0;
            int deltaY = 0;
            
            switch (direction) {
            case NONE:
                deltaX = 0;
                deltaY = 0;
                break;
            case UP:
                deltaX = 0;
                deltaY = -distance;
                break;
            case DOWN:
                deltaX = 0;
                deltaY = distance;
                break;
            case LEFT:
                deltaX = -distance;
                deltaY = 0;
                break;
            case RIGHT:
                deltaX = distance;
                deltaY = 0;
            }
            
            int x = centerPoint.x + deltaX;
            int y = centerPoint.y + deltaY;
            this.centerPoint = new Point(x, y);
        }

        @Override
        public void keyDirectionActionPerformed(Direction direction) {
            this.direction = direction;
        }
        
    }
    
    public class Pellet {
        
        private double value;
        
        private Color color;
        
        private Point point;

        public Pellet(Point point, double value, Color color) {
            this.point = point;
            this.value = value;
            this.color = color;
        }

        public double getValue() {
            return value;
        }

        public void setValue(double value) {
            this.value = value;
        }

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
        }

        public Point getPoint() {
            return point;
        }

        public void setPoint(Point point) {
            this.point = point;
        }
        
    }
    
}