Best way to implement game loop without freezing UI thread

前端 未结 3 1905
余生分开走
余生分开走 2021-01-12 01:10

I\'m trying to make a simple 2D game in Java.

So far I have a JFrame, with a menubar, and a class which extends JPanel and overrides it\'s

相关标签:
3条回答
  • 2021-01-12 01:26

    You need one thread for you game loop and one thread to handle Swing UI events like mouse clicks and keypresses.

    When you use the Swing API, you automatically get an additional thread for your UI called the event dispatch thread. Your callbacks are executed on this thread, not the main thread.

    Your main thread is fine for your game loop if you want the game to start automatically when the programs runs. If you want to start and stop the game with a Swing GUI, then have then main thread start a GUI, then the GUI can create a new thread for the game loop when the user wants to start the game.

    No, your menu bar will not freeze up if you put your game loop in the main thread. Your menu bar will freeze up if your Swing callbacks take a long time to finish.

    Data that is shared between the threads will need to be protected with locks.

    I suggest you factor your Swing code into its own class and only put your game loop inside your main class. If you're using the main thread for your game loop, this is a rough idea of how you could design it.

    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    
    class GUI extends JFrame {
        GameCanvas canvas = new GameCanvas();
        final int FRAME_HEIGHT = 400;
        final int FRAME_WIDTH = 400;
    
    
        public GUI() {
            // build and display your GUI
            super("Game");
    
            JMenuBar menuBar = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
            JMenuItem startMenuItem = new JMenuItem("Pause");
            menuBar.add(fileMenu);
            fileMenu.add(startMenuItem);
    
            super.add(canvas);
            super.setVisible(true);
            super.setSize(FRAME_WIDTH, FRAME_WIDTH);
            super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            super.setJMenuBar(menuBar);
        }
    }
    
    public class Main {
    
        public static void main(String args[]) {
            GUI ui = new GUI(); // create and display GUI        
    
            gameLoop(); // start the game loop
        }
    
        static void gameLoop() {
            // game loop    
        }
    }
    
    0 讨论(0)
  • 2021-01-12 01:40

    Java is really suited for event-driven programming. Basically, set up a timer event and listen. At each 'tick-tock' you update your game logic. At each GUI-event you update your data structures that the game logic method will read.

    0 讨论(0)
  • 2021-01-12 01:42

    Your game loop (model) should not be anywhere near any of your GUI classes (view). It uses your GUI classes--but even that you probably want to do through an intermediary (controller). A good way to ensure that you are doing it right is to check that your model doesn't have a single "include javax.swing.???".

    The best thing you could do is to keep the game loop in it's own thread. Whenever you want to make a change in the GUI, use SwingWorker or whatever the young kids use now to force it onto the GUI thread for just that one operation.

    This is actually awesome because it makes you think in terms of GUI Operations (which would constitute your controller). For instance, you might have a class called "Move" that would have the GUI logic behind a move. Your game loop might instantiate a "Move" with the right values (item to move, final location) and pass it to a GUI loop for processing.

    Once you get to that point, you realize that simply adding a trivial "undo" for each GUI operation allows you to easily undo any number of operations. You will also find it easier to replace your Swing GUI with a web GUI...

    0 讨论(0)
提交回复
热议问题