按钮点击后,为什么我的游戏不会重新开始玩游戏?

时间:2016-12-08 17:03:28

标签: java swing user-interface

我正在尝试让JButton'返回'返回游戏,我在游戏运行时运行布尔打印。单击按钮后它开始运行,但我的线程的其余部分被冻结,为什么会这样,我该如何解决这个问题?

我尝试添加一个显示运行状态的调试行(在while循环和if(运行)语句内部,在if语句之外),在单击后退按钮后,它显示运行为true。这应该表明线程的其余部分应该正常工作,但事实并非如此。因此,当我点击转义键时,它将运行设置为false并显示按钮。当我单击后退时,再次将运行设置为true并重新绘制屏幕。检查地点说它再次运行但屏幕仍然不接受用户输入或更新。

   private static final long serialVersionUID = 1L;

    static JPanel panel;
    Client client = Multiplayer.client;

    // map variables
    ArrayList<Integer> world = new ArrayList<Integer>();
    int width, height;
    int visibleWidth = 20;
    int visibleHeight = 20;
    int count = 0;

    static boolean running = true;

    // map variables
    int xOffset = 0, yOffset = 0;
    static double speed = 1;

    // game loop variables
    double interpolation = 0;
    final int TICKS_PER_SECOND = 25;
    final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
    final int MAX_FRAMESKIP = 5;

    // option menu

    JButton exit;    
    static JButton back;

    JButton options;    
    Player player = Multiplayer.client.player;

    public MainPanel(int width, int height) {
        this.width = width;
        this.height = height;

        this.panel = this;
        panel.setSize(new Dimension(width, height));
        panel.requestFocus();

        // add option menu buttons
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        exit = new Button("Exit", Loader.cropImage(Loader.loadImage("/SpriteSheet.png"), 7, 0, 96, 32),
                Loader.cropImage(Loader.loadImage("/SpriteSheet.png"), 9, 0, 96, 32));
        back = new Button("Back", Loader.cropImage(Loader.loadImage("/SpriteSheet.png"), 7, 0, 96, 32),
                Loader.cropImage(Loader.loadImage("/SpriteSheet.png"), 9, 0, 96, 32));
        options = new Button("Options", Loader.cropImage(Loader.loadImage("/SpriteSheet.png"), 7, 0, 96, 32),
                Loader.cropImage(Loader.loadImage("/SpriteSheet.png"), 9, 0, 96, 32));
        gbc.insets = new Insets(1, 10, 1, 10);
        gbc.gridx = 0;
        gbc.gridy = 0;
        options.setVisible(false);
        exit.setVisible(false);
        back.setVisible(false);
        panel.add(options, gbc);
        gbc.gridy = 1;
        panel.add(back, gbc);
        gbc.gridy = 2;
        panel.add(exit, gbc);

        panel.setVisible(true);

        back.addActionListener(new ActionListener() {    

            public void actionPerformed(ActionEvent e) {
                clearButtons();
                return;
            }               
        });

        try {
            WorldGenerator(Multiplayer.client.f);
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }

    public void clearButtons() {
        running = true;
        options.setVisible(false);
        exit.setVisible(false);
        back.setVisible(false);
        this.repaint();
    }
    public void WorldGenerator(File file) throws IOException {

        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line = "";
        while ((line = br.readLine()) != null) {
            // System.out.println(line);
            for (int i = 0; i < line.length(); i++) {
                world.add(Integer.parseInt(line.charAt(i) + ""));
            }
        }
        br.close();
        fr.close();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // render map
        for (int y = 0; y < Multiplayer.client.worldHeight; y++) {
            for (int x = 0; x < Multiplayer.client.worldWidth; x++) {

                if (count == world.size()) {
                    count = 0;
                }

                g.drawImage(Tile.getTexture(world.get(count)), (x * Tile.WIDTH) + xOffset, (y * Tile.HEIGHT) + yOffset,
                        null);
                count++;

            }

        }

        // render player and position
        for (int i = 0; i < Multiplayer.client.entityList.size(); i++) {
            if (Multiplayer.client.entityList.get(i) instanceof Player) {
                if (Multiplayer.client.entityList.get(i).getUUID() == player.getUUID()) {
                    g.drawImage(player.getImg(), player.getX(), player.getY(), null);
                    g.setColor(Color.RED);
                    // player bounds box
                    g.drawRect(player.getX(), player.getY(), player.width, player.height);

                    // System.out.println(xOffset + ", " + yOffset);
                } else {
                    g.drawImage(Multiplayer.client.entityList.get(i).getImg(),
                            Multiplayer.client.entityList.get(i).getX(), Multiplayer.client.entityList.get(i).getY(),
                            null);
                }
            }
        }
        //options menu backdrop
        if(!running) {
            g.setColor(new Color(0,0,0,85));
            g.fillRect(0, 0, panel.getWidth(), panel.getHeight());
        }
    }       

    @Override
    public void run() {
        long lastLoopTime = System.nanoTime();
        final int TARGET_FPS = 60;
        final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
        long lastFpsTime = 0;

        while (true) {
            long now = System.nanoTime();
            long updateLength = now - lastLoopTime;
            lastLoopTime = now;
            double delta = updateLength / ((double) OPTIMAL_TIME);

            lastFpsTime += updateLength;
            if (lastFpsTime >= 10000000) {
                lastFpsTime = 0;
                System.out.println(running);

                // escape - options menu
                if (Keyboard.keys.contains(27) ) {
                    if(running) {
                        running = false;
                        back.setVisible(true);
                        this.repaint();                         
                    }
                    else {
                        running = true;
                        back.setVisible(false);
                        this.repaint();
                    }
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                    // left - done
                    if (Keyboard.keys.contains(37)) {
                        if (player.getX() < panel.getWidth() / 6 && xOffset < 0) {
                            xOffset += speed;
                            player.setX(panel.getWidth() / 6);
                        }
                        if (Tile.getTile(Tile.getTileAt(((player.getX() - 1) / Tile.WIDTH),
                                (player.getY() / Tile.HEIGHT))).isSolid
                                || Tile.getTile(Tile.getTileAt(((player.getX() - 1) / Tile.WIDTH),
                                        (player.getY() / Tile.WIDTH) + ((player.height / 2) / Tile.HEIGHT))).isSolid
                                || Tile.getTile(Tile.getTileAt(((player.getX() - 1) / Tile.WIDTH),
                                        ((player.getY() / Tile.HEIGHT) + (player.height / Tile.HEIGHT)))).isSolid) {
                        } else {
                            player.setX((int) (player.getX() - speed));
                            client.sendMessage("0005;" + client.uuid + ";" + player.getX() + ";" + player.getY());

                        }
                    }
                    // right - done
                    if (Keyboard.keys.contains(39)) {
                        if (player.getX() + player.getWidth() > (panel.getWidth() / 6) * 5
                                && (xOffset * -1) + panel.getWidth() < client.worldWidth * Tile.WIDTH) {
                            xOffset -= speed;
                            player.setX(((panel.getWidth() / 6) * 5) - player.getWidth());
                        }
                        if (Tile.getTile(Tile.getTileAt(
                                (((player.getX() + ((xOffset) * -1) + 1) / Tile.WIDTH) + (player.width / Tile.WIDTH)),
                                (player.getY() / Tile.HEIGHT))).isSolid
                                || Tile.getTile(Tile.getTileAt(
                                        (((player.getX() + ((xOffset) * -1) + 1) / Tile.WIDTH)
                                                + (player.width / Tile.WIDTH)),
                                        (player.getY() / Tile.HEIGHT) + ((player.height / 2) / Tile.HEIGHT))).isSolid
                                || Tile.getTile(Tile.getTileAt(
                                        (((player.getX() + ((xOffset) * -1) + 1) / Tile.WIDTH)
                                                + (player.width / Tile.WIDTH)),
                                        (player.getY() / Tile.HEIGHT) + (player.height / Tile.HEIGHT))).isSolid) {
                        } else {
                            player.setX((int) (player.getX() + speed));
                            client.sendMessage("0005;" + client.uuid + ";" + player.getX() + ";" + player.getY());

                        }
                    }

                    // up
                    if (Keyboard.keys.contains(38)) {
                        if (player.getY() < panel.getHeight() / 8 && yOffset < 0) {
                            yOffset += speed;
                            player.setY(panel.getHeight() / 8);
                        }
                        if (Tile.getTile(Tile.getTileAt((player.getX() / Tile.WIDTH),
                                ((player.getY() + ((yOffset) * -1) - 2) / Tile.HEIGHT))).isSolid
                                || Tile.getTile(
                                        Tile.getTileAt((player.getX() / Tile.WIDTH) + (player.width / Tile.WIDTH),
                                                ((player.getY() + ((yOffset) * -1) - 2) / Tile.HEIGHT))).isSolid) {
                        } else {
                            player.setY((int) (player.getY() - speed));
                            client.sendMessage("0005;" + client.uuid + ";" + player.getX() + ";" + player.getY());

                        }
                    }
                    // down
                    if (Keyboard.keys.contains(40)) {
                        if (player.getY() + player.getHeight() > (panel.getHeight() / 8) * 7
                                && (yOffset * -1) + panel.getHeight() < client.worldHeight * Tile.HEIGHT) {
                            yOffset -= speed;
                            player.setY(((panel.getHeight() / 8) * 7) - player.getHeight());
                        }
                        if (Tile.getTile(Tile.getTileAt((player.getX() / Tile.WIDTH),
                                ((player.getY() + ((yOffset) * -1) + 1) / Tile.HEIGHT)
                                        + (player.height / Tile.HEIGHT))).isSolid
                                || Tile.getTile(
                                        Tile.getTileAt((player.getX() / Tile.WIDTH) + (player.width / Tile.WIDTH),
                                                ((player.getY() + ((yOffset * -1)) + 1) / Tile.HEIGHT)
                                                        + (player.height / Tile.HEIGHT))).isSolid) {
                        } else {
                            player.setY((int) (player.getY() + speed));
                            client.sendMessage("0005;" + client.uuid + ";" + player.getX() + ";" + player.getY());

                        }
                    }

                    this.repaint();                 
            }
        }
    }
  }

0 个答案:

没有答案