我想制作一个暂停/设置屏幕

时间:2017-08-23 13:22:16

标签: java pause

我一直在玩游戏一段时间了。我遇到了一个我似乎无法解决的问题。

我写了这样的话,当我按下字母“Q”时,会出现暂停菜单。但我的游戏并没有冻结。我试过Thread.sleep();方法,但整个游戏冻结。

游戏代码

package flappyBird;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.*;             // for Random function
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.Timer;

public class FlappyBird extends JFrame implements ActionListener, MouseListener, KeyListener
{   


    GUI guis = new GUI();

    Font myFont = new Font("Arial", 1, 65);

    public static FlappyBird flappyBird;

    public final int WIDTH = 800, HEIGHT = 800;

    public Renderer renderer;

    public Rectangle bird;

    public ArrayList<Rectangle> columns;

    public int ticks, yMotion, score;

    public boolean gameOver, started;

    public Random rand;

    public FlappyBird()
    {
        JFrame jframe = new JFrame();
        Timer timer = new Timer(20, this);

        renderer = new Renderer();
        rand = new Random();

        jframe.add(renderer);
        jframe.setTitle("Flappy Block");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setSize(WIDTH, HEIGHT);
        jframe.addMouseListener(this);
        jframe.addKeyListener(this);
        jframe.setResizable(false);
        jframe.setVisible(true);

        bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
        columns = new ArrayList<Rectangle>();

        addColumn(true);
        addColumn(true);
        addColumn(true);
        addColumn(true);

        timer.start();
    }

    public void addColumn(boolean start)
    {
        int space = 300;
        int width = 100;
        int height = 50 + rand.nextInt(300);

        if (start)
        {
            columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height));
            columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space));
        }
        else
        {
            columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height));
            columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));
        }
    }

    public void paintColumn(Graphics g, Rectangle column)
    {
        g.setColor(Color.green.darker());
        g.fillRect(column.x, column.y, column.width, column.height);
    }

    public void jump()
    {



        if (gameOver)
        {
            bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
            columns.clear();
            yMotion = 0;
            score = 0;

            addColumn(true);
            addColumn(true);
            addColumn(true);
            addColumn(true);

            gameOver = false;
        }

        if (!started)
        {
            started = true;
        }
        else if (!gameOver)
        {
            if (yMotion > 0)
            {
                yMotion = 0;
            }

            yMotion -= 13;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        int speed = 10;

        ticks++;


              if (started)
        {
            for (int i = 0; i < columns.size(); i++)
            {
                Rectangle column = columns.get(i);

                column.x -= speed;
            }

            if (ticks % 2 == 0 && yMotion < 15)
            {
                yMotion += 2;
            }

            for (int i = 0; i < columns.size(); i++)
            {
                Rectangle column = columns.get(i);

                if (column.x + column.width < 0)
                {
                    columns.remove(column);

                    if (column.y == 0)
                    {
                        addColumn(false);
                    }
                }
            }

            bird.y += yMotion;

            for (Rectangle column : columns)
            {
                if (column.y == 0 && bird.x + bird.width / 2 > column.x + column.width / 2 - 10 && bird.x + bird.width / 2 < column.x + column.width / 2 + 10)
                {
                    score++;
                }

                if (column.intersects(bird))
                {
                    gameOver = true;

                    if (bird.x <= column.x)
                    {
                        bird.x = column.x - bird.width;

                    }
                    else
                    {
                        if (column.y != 0)
                        {
                            bird.y = column.y - bird.height;
                        }
                        else if (bird.y < column.height)
                        {
                            bird.y = column.height;
                        }
                    }
                }
            }

            if (bird.y > HEIGHT - 120 || bird.y < 0)
            {
                gameOver = true;
            }

            if (bird.y + yMotion >= HEIGHT - 120)
            {
                bird.y = HEIGHT - 120 - bird.height;
                gameOver = true;
            }

        }


        renderer.repaint();
    }

    public void repaint(Graphics g)
    {

        g.setColor(Color.cyan);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.orange);
        g.fillRect(0, HEIGHT - 120, WIDTH, 120);

        g.setColor(Color.green);
        g.fillRect(0, HEIGHT - 120, WIDTH, 20);

        g.setColor(Color.red);
        g.fillRect(bird.x, bird.y, bird.width, bird.height);

        for (Rectangle column : columns)
        {
            paintColumn(g, column);
        }

        g.setColor(Color.white);
        g.setFont(new Font("Arial", 1, 100));

        if (!started)
        {
            g.drawString("Click to start!", 75, HEIGHT / 2 - 50);

        }

        if (gameOver)
        {
            g.setFont(myFont);
            g.drawString("Final Score:", 5, HEIGHT / 5 - 65);
            g.drawString("Game Over!", 200, HEIGHT / 2 - 70);
            g.drawString(String.valueOf(score),  WIDTH / 2 - 25, 100);
            ;
        }

        if (!gameOver && started)
        {
            g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100);
        }
    }

    public void DrawMenu(Graphics g)
    {
        guis.setVisible(true);
    }

    public static void main(String[] args)
    {
        flappyBird = new FlappyBird();


    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
        jump();
    }


    @Override
    public void keyPressed(KeyEvent e)
    {
          int keyCode = e.getKeyCode();

          if (keyCode == KeyEvent.VK_SPACE)
              jump();
          if (keyCode == KeyEvent.VK_ESCAPE)              

                  guis.setVisible(true);
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
    }

    public void keyReleased(KeyEvent e) {}

    public void keyTyped(KeyEvent e) {}

        @Override
    public void mouseReleased(MouseEvent e)
    {
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
    }
}

暂停菜单代码

package flappyBird;

import java.awt.EventQueue;    
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import java.awt.Color;

public class GUI {      

    private JFrame frame;

    /**
     * Launch the application.
     */
    public void guis() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI window = new GUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public GUI() {
        initialize();           

            JFrame jframe = new JFrame();

            jframe.setTitle("Pause Menu");
            jframe.setResizable(false);



    }

    /**
     * Initialize the contents of the frame.
     */


    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(Color.GRAY);
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setResizable(false);

        JButton btnContinue = new JButton("Continue");
        btnContinue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


                frame.dispose();
            }

        });
        btnContinue.setBounds(146, 67, 113, 23);
        frame.getContentPane().add(btnContinue);

        JButton btnExit = new JButton("Exit Game");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {


                if (JOptionPane.showConfirmDialog(null, "Are you sure?", "Exit",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                  System.exit(0);
                } else {

                }
            }




        });
        btnExit.setBounds(146, 181, 113, 23);
        frame.getContentPane().add(btnExit);

        JButton btnShare = new JButton("Share");
        btnShare.setBounds(10, 101, 89, 23);
        frame.getContentPane().add(btnShare);

        JButton btnSaveHighscore = new JButton("Save High Score ");
        btnSaveHighscore.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnSaveHighscore.setBounds(137, 135, 132, 23);
        frame.getContentPane().add(btnSaveHighscore);

        JButton btnLoadHighScore = new JButton("Load High Score");
        btnLoadHighScore.setBounds(137, 101, 132, 23);
        frame.getContentPane().add(btnLoadHighScore);

        JButton btnNewButton = new JButton("Cheat Codes");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

                JOptionPane.showMessageDialog(null, "Press A A A B for 100 points instant.");

            }
             });
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {



            }
        });
        btnNewButton.setBounds(294, 101, 105, 23);
        frame.getContentPane().add(btnNewButton);
    }
    private static void addPopup(Component component, final JPopupMenu popup) {
        component.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }
            private void showMenu(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }


    public void setVisible(boolean b){

        guis();
    }


}

1 个答案:

答案 0 :(得分:0)

你必须引入一个名为&#34;暂停&#34;的新布尔变量。或类似的东西。

进入菜单时,必须将其设置为true,退出菜单后将其设置为false。我虽然无法找到你的代码。

在你的actionPerformed中你必须检查暂停标志,如果它没有增加时间/不重绘等等。

所以它基本上是

public void actionPerformed(ActionEvent e)
{
    if(!paused)
    ...
}

以及其他地方:

"when q-key pressed"
{
    paused = !paused; // toggle the Pause
    if(paused) DrawPauseMenu();
}
相关问题