java - 我试图使用 while 循环重复 if 语句,但它一直卡住

标签 java if-statement for-loop keyevent

所以我正在尝试制作一款像《贪吃蛇》这样的游戏,但我遇到了一些问题。 我想使用 while 循环重复 moveup 类内部的 if 语句,但是 当我尝试使用 KeyListener 监听向上箭头键的按键来中断 while 循环时,它的行为就像只循环一次(向上移动 5 个像素)。该循环应该使蛇继续上升而不必单击多次,但它只移动五个(我设置的值)像素。这是代码:

public class Snake extends JFrame implements KeyListener {
int ballX = 50;
int ballY = 50;
int playerX = 250;
int playerY = 250;
boolean up = false;
boolean right = false;
boolean down = false;
boolean left = true;

class DrawPane extends JPanel {

    public void paintComponent(Graphics gc) {
        Random rand = new Random();
        int dotsX = rand.nextInt(500) + 1;
        int dotsY = rand.nextInt(500) + 1;
        Graphics g = this.getGraphics();
        setVisible(true);
        super.paintComponents(gc);

        gc.setColor(Color.BLUE);
        gc.fillRect(ballX, ballY, 10, 10);

        gc.setColor(Color.RED);
        gc.fillRect(playerX, playerY, 10, 10);

    }

}

public Snake(String title) {
    super(title);
    JFrame frame = new JFrame();
    setContentPane(new DrawPane());
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
    this.setSize(500, 500);
    this.setBackground(Color.YELLOW);
    this.setResizable(false);
    this.addKeyListener(this);
}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        up = true;
        right = false;
        down = false;
        left = false;
        moveup(e);
    }

    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        up = false;
        right = false;
        down = true;
        left = false;
        movedown(e);
    }

    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        up = false;
        right = true;
        down = false;
        left = false;
        moveright(e);
    }

    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        up = false;
        right = false;
        down = false;
        left = true;
        moveleft(e);
    }

}

public void moveup(KeyEvent e) {
    while (up) {
        if (playerX < 500 || playerX > 0) {
            playerX = playerX + 5;
            repaint();
            if (e.getKeyChar() == KeyEvent.VK_UP)
                break;
        }
    }
}

public void moveright(KeyEvent e) {
    if (playerX < 500 || playerX > 0) {
        playerX = playerX + 5;
        repaint();
        // moveright();
    }
}

public void movedown(KeyEvent e) {
    if (playerY < 500 || playerY > 0) {
        playerY = playerY + 5;
        repaint();
        // movedown();
    }
}

public void moveleft(KeyEvent e) {
    if (playerX < 500 || playerX > 0) {
        playerX = playerX - 5;
        repaint();
        // moveleft();
    }
}

public void snakePanel() {

    JPanel panel1 = new JPanel();
    panel1.setBackground(Color.red);

}

public void ActionListener() {

}

public static void main(String[] args) {
    Snake r = new Snake("Snake");

}

}

最佳答案

由于中断,它永远不会循环。实际上你是在说:

if keycode is up
  loop
    move up
    break if keycode is up // Will always be true

更重要的是,您不应该在这样的事件处理程序中循环。事件处理程序应该快速执行并返回,否则您将阻塞事件线程。应该使用计时器作为心跳并定期更新位置/重绘。

关于java - 我试图使用 while 循环重复 if 语句,但它一直卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24543534/

相关文章:

java - Java对二维数组求和的方法

java - Spring和Soap客户端,使用池化吗?

java - 使用搜索服务器服务层与数据库层来查找实体

java - Switch 语句 : Is the logic different in C v/s. Java 等其他语言?

r - 总结使用带有 for 循环的 dplyr

java - Java中嵌套循环的类型

java - 每次循环运行时都会实例化一个新对象吗?

如果条件存在,Python 将 1 附加到列表,将 0 附加到所有其他列表

jquery - 试图在每个语句中获取 this 的父级

c++ - 如何实现符合标准的迭代器/容器?