如何在CardLayout中使游戏在面板中运行/工作

时间:2019-08-20 07:16:16

标签: java multithreading jpanel cardlayout

我目前正在开发一款利用CardLayout在面板之间进行切换的游戏。一旦玩家单击主菜单面板上的 Classic Game 按钮,游戏就应该进入包含“经典游戏”模式的“卡片” /面板。它确实有效,即我可以从“主菜单”面板切换到“经典游戏”面板,但是问题是我无法玩游戏,因为游戏只是“冻结”,游戏无法移动或正常工作。我无法返回主菜单,应该会在按ESC键后发生。

这是MainMenu类:

public class MainMenu extends JFrame
{
    private JPanel contPanel;
    private MyJPanel menuPanel;
    private ClassicGame classicPanel;
    private FirstAF advancedPanel;
    private Game3072 game3072;
    private CreditsFrame creditsPanel;
    private HelpFrame helpPanel;

    CardLayout cl = new CardLayout();   

    public MainMenu(){
        classicPanel = new ClassicGame(this);
        game3072 = new Game3072(this);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable (false);

        contPanel = new JPanel();
        menuPanel = new MyJPanel(this);
        creditsPanel = new CreditsFrame(this);
        advancedPanel = new FirstAF(this);
        helpPanel = new HelpFrame(this);

        contPanel.setLayout(cl);

        contPanel.add(menuPanel, "1");
        contPanel.add(classicPanel, "2");
        contPanel.add(creditsPanel, "3");
        contPanel.add(advancedPanel, "4");
        contPanel.add(game3072, "5");
        contPanel.add(helpPanel, "6");

        cl.show(contPanel, "1");

        add(contPanel);
        pack();
        setLayout(new BorderLayout(0,0));
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void initiateMenu(){
        cl.show(contPanel, "1");
    }

    public void initiateCG(){
        cl.show(contPanel, "2");
        classicPanel.start();
    }

.
.
.
.

}

ClassicGame类:

public class ClassicGame extends JPanel implements KeyListener, Runnable
{
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 500; // can be changed according to programmer's choice
    public static final int HEIGHT = 600; // can be changed according to programmer's choice
    public static final Font main = new Font("Comic Sans MS", Font.PLAIN, 28);
    private Thread game;
    private boolean running;
    private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    private ClassicGameBoard board;

    private long startTime;
    private long elapsed;
    private boolean set;
    private MainMenu mainMenu;

    public ClassicGame(MainMenu mainMenu)
    {
        this.mainMenu = mainMenu;
        setFocusable(true);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        addKeyListener(this);
        setLayout(null);

        board = new ClassicGameBoard(WIDTH/2 - (ClassicGameBoard.BOARD_WIDTH)/2, HEIGHT - ClassicGameBoard.BOARD_HEIGHT - 10);
    }

    private void update()
    {
        board.update();
        Keyboard.update();

        if (board.getWinStat() == true)
        {
            stop();
            // a window will inform the player that he/she won the game
            JOptionPane.showMessageDialog(null, "YOU WON! /^o^/", "Message Notification", JOptionPane.PLAIN_MESSAGE);
            board.setWinStat(false);
            stop();
        }
        else if (board.getDeadStat() == true)
        {
            // a window will inform the player that he/she lost the game
            JOptionPane.showMessageDialog(null, "YOU LOST! /-^-/", "Message Notification", JOptionPane.PLAIN_MESSAGE);
            board.setDeadStat(false);
            stop();
        }
    }

    private void render()
    {
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        board.render(g);
        g.dispose();

        Graphics2D g2d = (Graphics2D) getGraphics();
        g2d.drawImage(image, 0, 0, null);
        g2d.dispose();
    }

    @Override
    public void run()
    {
        int fps = 0, updates = 0;
        long fpsTimer = System.currentTimeMillis();
        double nsPerUpdate = 1000000000.0 / 60;

        // last update time in nanoseconds
        double then = System.nanoTime();
        double unprocessed = 0;

        while(running)
        {
            boolean shouldRender = false;
            double now = System.nanoTime();
            unprocessed += (now - then)/nsPerUpdate;
            then = now;

            // update queue
            while(unprocessed >= 1)
            {
                updates++;
                update();
                unprocessed--;
                shouldRender = true;
            }

            // rendering
            if(shouldRender)
            {
                fps++;
                render();
                shouldRender = false;
            }
            else
            {
                try{
                    Thread.sleep(1);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }

        // FPS timer
        if(System.currentTimeMillis() - fpsTimer > 1000)
        {
            System.out.printf("%d fps %d updates", fps, updates);
            System.out.println();
            fps = 0;
            updates = 0;
            fpsTimer += 1000;
        }
    }

    public synchronized void start()
    {
        if(running) return;
        running = true;
        game = new Thread(this, "game");
        game.start();
    }

    public void stop()
    {
        if(!running) return;
        running = false;
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
        Keyboard.keyPressed(e);

        if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
        {
            stop();
            mainMenu.initiateMenu();
        }
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
        Keyboard.keyReleased(e);
    }

.
.
.
.

}

你们能找出我错过或错了哪一部分吗?谢谢!

0 个答案:

没有答案