简体   繁体   English

重置游戏循环/重启游戏Java Swing

[英]Reset game loop / restart game java swing

I am working on a Java Snake Game. 我正在开发Java Snake游戏。 Most of the basic functionality is done ubt I am trying to add a restart function that will be triggered when you press ENTER when the game over. 大多数基本功能已完成,但我尝试添加重启功能,该功能将在游戏结束时按ENTER时触发。 All logics semms to work, the snake resets to start position, apple gets new random position etc. But for some reason my JPanels won't repaint. 所有逻辑都起作用,蛇复位到开始位置,苹果获得新的随机位置等。但是由于某些原因,我的JPanels不会重新绘制。 Could you guys take a look and see if you can figure out what I'm doing wrong. 你们可以看看看看是否可以弄清楚我在做什么错。

I've made a snapshot out of my code with the game loop, reset function etc. Just let me know if you need more. 我已经使用游戏循环,重置功能等从代码中制作了快照。请告诉我是否需要更多内容。

/*
     * Main class, game loop, keyListener etc
     */

    /*
     * Here is my run method with game loop when gameOver, snake will no longer
     * update.
     * 
     */
    @Override
    public void run() {
        // game loop
        while (running) {
            if (!gameOver) {                
                update();
            }

            MainFrame.paint();

            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

/*
 * KeyListener, when ENTER is pressed call resetGame() method.
 */
if(key.getKeyCode()==KeyEvent.VK_ENTER && gameOver) {
    resetGame();
}


    /*
     * resetGame method where I reset variables, create new default snake, 
     * clear JPanels etc.
     */
    public void resetGame() {
        score = 0;
        snake.clear(); // clear arraylist

        // .removAll() on both JPanels
        MainFrame.clear();

        // fields
        gameOver = false;
        newGame = true;

        direction = Direction.RIGHT;

        // create default snake
        snake = new ArrayList<Point>();
        size = 5;
        for (int i = 0; i < size; i++) {
            snake.add(new Point((size - i - 1) * MainFrame.getTileSize(), 0));
        }
        System.out.println(snake);

        // create apple and set to random location
        apple = randomApple();
    }

    /*
     * MainFrame extends JFrame  
     * methods for repainting and clearing JPanels
     */

    public static void paint() {

        gamePanel.repaint();
        sideHud.repaint();
    }

    public static void clear() {
        gamePanel.removeAll();
        sideHud.removeAll();

        paint();
    }

My JPanels are pretty basic. 我的JPanels非常基础。 I just paint everything in the paintComponent method with Graphics2D object. 我只是用Graphics2D对象绘制paintComponent方法中的所有内容。

This is my most recent solution (which didn't work). 这是我最近的解决方案(不起作用)。 I have also tried, leaving the game loop and then calling on run() again in the resetGame() method, I tried creating a new Main object when ENTER is pressed, but none of them worked. 我也尝试过,离开游戏循环,然后在resetGame()方法中再次调用run(),我尝试在按ENTER键时创建一个新的Main对象,但是它们都不起作用。

Thanks in advance. 提前致谢。

Well first check if the game is over via an if so create a Boolean that is set to true when the game is over. 首先,通过if检查游戏是否结束,创建一个布尔值,当游戏结束时将其设置为true。 When that Boolean becomes true create a method that is called that resets all the variables in your game method and any other variables related to the game. 当该布尔值变为true时,创建一个称为的方法,该方法将重置游戏方法中的所有变量以及与游戏相关的任何其他变量。 Then call the game method (whatever triggers your game to work). 然后调用游戏方法(任何触发游戏运行的方法)。 Voila your game has reset hope that helped. 瞧,您的游戏重新设定了希望的希望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM