0

我一直在制作俄罗斯方块游戏,它几乎完成了。我对 KeyEvents 和 Timer 有疑问。

我的计时器每 400 毫秒调用actionPerformed一次,它会下拉俄罗斯方块块。问题是,如果我从键盘上按下某些东西(下、左、右等),这些命令也会每 400 毫秒记录一次。我怎样才能让计时器actionPerformed每 400 毫秒调用一次,但这样我就可以随时随地按多少次我想要键盘上的东西并立即识别/响应?

编辑:这是人们问的一些代码

public class Game extends JPanel implements ActionListener {

public static final HashMap<Character, Color> colors = new HashMap<Character,Color>();
Timer timer;
Block kpl;
Grid gameBoard;
char[][] grid;
boolean paused = false;

public Game() {

    colors.put('P', Color.blue); colors.put('S', Color.green);
    colors.put('D', Color.pink); colors.put('L', Color.red);
    colors.put('Z', Color.orange); colors.put('A', Color.cyan);
    colors.put('T', Color.magenta);
    kpl = new Block();
    gameBoard = new Grid(21,10); // 21x10 sized gameboard
    grid = gameBoard.getGrid();
    addKeyListener(new KeyListening());
    setDoubleBuffered(true);
    setFocusable(true);
    timer = new Timer(100, this);
    timer.start();

}

@Override
public void paint(Graphics g) { // draws the current game situation
    super.paint(g);

        int height = getHeight()/grid.length;
        int width = getWidth()/grid[0].length/2;


        for (int row = 0; row < grid.length; ++row) {
            for (int column = 0; column < grid[row].length; ++column) {

                if (grid[row][column] == '.') { // if empty, paint white
                    g.setColor(Color.white);
                    g.fillRect(column*width, row*height , width, height);
                    g.setColor(Color.LIGHT_GRAY);
                    g.drawRect(column*width, row*height , width, height);
                }
                else if (grid[row][column] != '.') { // if not empty, paint with the color of the block
                    g.setColor(colors.get(grid[row][column]));
                    g.fillRect(column*width, row*height , width, height);
                    g.setColor(Color.BLACK);
                    g.drawRect(column*width, row*height , width, height);
                }
            }
        }

    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}



public void actionPerformed(ActionEvent e) {
    if (!gameBoard.isFallingBlock())
        gameBoard.drop(kpl); // if not any falling blocks, drops a new block

    gameBoard.updateView(); // moves the block down
    repaint();
}

    private class KeyListening extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        switch (key) {
            case KeyEvent.VK_LEFT:
                gameBoard.move("v"); // moves block left
                break;
            case KeyEvent.VK_RIGHT:
                gameBoard.move("o"); // moves block right
                break;
            case KeyEvent.VK_DOWN:
                gameBoard.updateView(); // moves block down
                break;
            case KeyEvent.VK_SPACE:
                gameBoard.move("p"); // drops the block to the bottom
                break;
            case KeyEvent.VK_UP: // rotates block
                gameBoard.move("k");
                break;
            case KeyEvent.VK_P: // makes the game pause
                if (paused) {
                    timer.start();
                    paused = false;
                    return;
                }

                timer.stop();
                paused = true;
                break;

        }

    }

}

}

由于我一直用自己的语言命名变量,因此不得不稍微翻译一下变量。GUI 也非常简单,因为我首先制作了我的俄罗斯方块的文本版本,所以它基本上在后台运行它的文本版本,只是描绘游戏的情况。

文本版本本身是 char 类型的矩阵,我在其中移动块,例如:

..P..
..ppp
.....
.....

解决了:

问题解决了!在来自键盘的每个命令之后,在 public void keyPressed() 中,我再次调用了 repaint() 方法,例如

        public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                gameBoard.move("v");
                repaint();
                break;

最初我的游戏会立即响应我的键盘命令,但在调用 repaint() 的 actionPerformed() 中每 400 毫秒绘制一次游戏的当前状态。现在我的块可以随时移动而没有任何“延迟”,并且它们仍然每 400 毫秒稳定下降 :)

4

3 回答 3

0

您可能想查看同一页面底部的Key Bindings及其示例。

取自tips4java博客

通过添加新的键绑定来添加新功能:

Action action = new AbstractAction() {...};
String keyStrokeAndKey = "control Z";
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
component.getInputMap(...).put(keyStroke, keyStrokeAndKey);
component.getActionMap().put(keyStrokeAndKey, action);

通过替换现有绑定的 Action 来更改现有功能:

Action action = new AbstractAction() {...};
KeyStroke keyStroke = KeyStroke.getKeyStroke("control Z");
InputMap im = component.getInputMap(...);
component.getActionMap().put(im.get(keyStroke), action);

使用不同的 KeyStroke 共享一个动作:

KeyStroke existingKeyStroke = KeyStroke.getKeyStroke("ENTER");
KeyStroke addedKeyStroke = KeyStroke.getKeyStroke("control Z");
InputMap im = component.getInputMap(...);
im.put(addedKeyStroke, im.get(existingKeyStroke));

删除键绑定:

KeyStroke remove = KeyStroke.getKeyStroke(...);
InputMap im =component.getInputMap(...);
im.put(remove, "none");

请注意,以上示例用于将绑定添加到各个组件。要在框架或对话框级别添加绑定,您需要使用根窗格的 InputMap 和 ActionMap:

frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)...
frame.getRootPane().getActionMap()...
于 2011-04-12T11:50:09.510 回答
0

I'd try decreasing the timer interval to make keypresses more responsive and use a counter to drop down the block once every so many intervals.

于 2011-04-12T12:40:51.520 回答
0

您需要确保您使用的是 SwingTimer并且没有阻塞事件调度线程。您很可能需要一个单独的actionPerformed方法来处理关键事件,而不是块丢弃。

于 2011-04-12T11:54:21.217 回答