簡體   English   中英

我正在嘗試使用while循環重復一條if語句,但是它一直死機

[英]I am trying to repeat an if statement using a while loop, but it keeps freezing

因此,我嘗試制作類似Snake的游戲,但遇到了一些問題。 我想使用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

更重要的是,您不應該在這樣的事件處理程序中循環。 事件處理程序應執行並快速返回,否則您將阻塞事件線程。 應該使用計時器充當心跳並定期更新位置/重繪。

請記住,您將來不想將Swing用於更高級的游戲。

---快速解決方案:

只需刪除您的while()。 通過KeyListener界面,您可以抽象出“按下此鍵的同時執行此操作”的概念。 而是在實現接口時說:“只要按下此鍵,就執行此操作”。 這是一個事件。

這是您的工作代碼:

http://pastebin.com/b0CZDxpD

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM