0

我有 2 个类用于 pacman 游戏的开始,我对按键侦听器或按键绑定的工作方式感到非常困惑。我有向上、向下、向右和向左移动的方法。我花了几个小时试图弄清楚我,它不会调用常规方法,并且组件让我感到困惑。有人可以帮我看看如何获​​得箭头键调用方法的最终结果吗?我有下面的代码,但我取出了所有关键测试。

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class GUI {
JPanel myPanel = new JPanel(new GridLayout(50, 50, 1, 1));
int[][] myLabels = new int[50][50];

//Set Game Black
public int[][] setBlack(int[][] arrayLabels){
    myLabels = arrayLabels;
    for (int i = 0; i < myLabels.length; i++) {
        for (int j = 0; j < myLabels[0].length; j++) {
            JLabel black = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
            myLabels[i][j] = 1;
        }
    }
    return myLabels;
}


//get panel any time changes are made to array
public JPanel getPanel(int[][] arrayLabels){
    myLabels = arrayLabels;
    for (int i = 0; i < myLabels.length; i++) {
        for (int j = 0; j < myLabels[0].length; j++) {
            JLabel black = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
            JLabel pacman = new JLabel(new ImageIcon("pacman.png"), JLabel.CENTER);
            if(myLabels[i][j] == 1){
                myPanel.add(black);
            }
            if(myLabels[i][j] == 2){
                myPanel.add(pacman);
            }
        }
    }
    return myPanel;
}

//moves pacman up
public int[][] moveUp(int[][] arrayLabels){
    myLabels = arrayLabels;

    for (int i = 0; i < myLabels.length; i++) {
        for (int j = 0; j < myLabels[0].length; j++) {
            if(myLabels[i][j] == 2){
                myLabels[i][j] = 1;
                myLabels[i-1][j] = 2;
            }
        }
        }
    return myLabels;
}

//=========
//moves pacman up

public void setLabel(int[][] arrayLabels){
    myLabels = arrayLabels;
}

public int[][] moveUpKey(){

    for (int i = 0; i < myLabels.length; i++) {
        for (int j = 0; j < myLabels[0].length; j++) {
            if(myLabels[i][j] == 2){
                myLabels[i][j] = 1;
                myLabels[i-1][j] = 2;
            }
        }
        }
    return myLabels;
}
//////===========================
//moves pacman down
public int[][] moveDown(int[][] arrayLabels){
    myLabels = arrayLabels;

    for (int i = 0; i < myLabels.length; i++) {
        for (int j = 0; j < myLabels[0].length; j++) {
            if(myLabels[i][j] == 2){
                myLabels[i][j] = 1;
                myLabels[i+1][j] = 2;
            }
        }
        }
    return myLabels;
}

//moves pacman right
    public int[][] moveRight(int[][] arrayLabels){
        myLabels = arrayLabels;

        for (int i = 0; i < myLabels.length; i++) {
            for (int j = 0; j < myLabels[0].length; j++) {
                if(myLabels[i][j] == 2){
                    myLabels[i][j] = 1;
                    myLabels[i][j+1] = 2;
                }
            }
            }
        return myLabels;
    }

    //moves pacman left
            public int[][] moveLeft(int[][] arrayLabels){
                myLabels = arrayLabels;

                for (int i = 0; i < myLabels.length; i++) {
                    for (int j = 0; j < myLabels[0].length; j++) {
                        if(myLabels[i][j] == 2){
                            myLabels[i][j] = 1;
                            myLabels[i][j-1] = 2;
                        }
                    }
                    }
                return myLabels;
            }



}

游戏类:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.util.*;



public class Game {


   public static void main(String[] args) throws Exception {

        final GUI gui = new GUI();


        JFrame f = new JFrame("Frame Test");


        JPanel panel = new JPanel(new GridLayout(50, 50, 1, 1));

        int[][] labels = new int[50][50];



      JLabel black2 = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
      JLabel pacman2 = new JLabel(new ImageIcon("pacman.png"), JLabel.CENTER);
      panel.setLayout(new GridLayout(50, 50, 1, 1));

                labels = gui.setBlack(labels);
                panel = gui.getPanel(labels);




                labels[10][10] = 2;



                //labels = gui.moveUp(labels);

                panel.removeAll(); 
                panel = gui.getPanel(labels);
                panel.repaint();






                //labels[i][j]

       //.revalidate();


       //do the thing 
        f.setContentPane(panel);
        f.setSize(1000, 1000);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);



   }


}
4

1 回答 1

2

基本上,myPanel充当游戏的核心容器。

这可能是注册键绑定的最佳候选...

GUI为您的类创建一个构造函数。在此构造函数中,您将需要注册您的键绑定,例如...

public GUI() {

    InputMap im = myPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = myPanel.getActionMap();

    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
    am.put("up", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            moveUp(myLabels);
        }
    });
    // Register other key strokes...

}

ps-我不知道为什么您的移动方法需要JLabel[][]参数,因为myLables无论如何这些方法都可以访问,但这只是我......

确保您已阅读如何使用键绑定以获取更多详细信息

于 2014-01-15T01:19:10.587 回答