gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:40:41 26 4
gpt4 key购买 nike

所以我正在尝试制作一款像《贪吃蛇》这样的游戏,但我遇到了一些问题。我想使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com