简体   繁体   中英

Java: Stuck making a pause function

I'm a bit of a newbie to Java and I'm trying to create a pong-like game. I have created a pause function that stops the game from running when the player hits the space key. I'm very stuck on exactly how to do this. Right now when I hit space, the ball stops but when I unpause, the ball gets much faster than before. I'm also quite sure this way of doing it just isn't going to work. Any help on how to do this would be much appreciated.

Here is my graphics class

public class GraphicRender extends JPanel implements ActionListener {

    //Object for Random
    Random random = new Random();

    //Timer on 5ms clock
    Timer timer = new Timer(5, this);

    //Variables for the coordinates of the player and enemy
    public static int paddleLX = 12, paddleLY = 400, paddleLW = 5, paddleLH = 70, paddleLXV, paddleLYV;
    public static int ballX = 400, ballY = 400, ballXV, ballYV;

    public static boolean gamePaused = false;

    //Constructor to initialise objects
    public GraphicRender() {
        timer.start();

        this.setFocusable(true);
        this.requestFocusInWindow(true);

        //Adds the keyListener
        addKeyListener(new GetKeyStroke());

    }

    //Graphic rendering
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.BLACK);
        draw(g);


    }

    public void draw(Graphics g) {
        g.setColor(Color.GRAY);

        //Walls
        g.fillRect(0, 1, 8, 800);
        g.fillRect(0, 1, 800, 8);
        g.fillRect(0, 755, 800, 8);
        g.fillRect(777, 1, 8, 800);

        //Ball
        g.setColor(Color.WHITE);
        g.fillRect(ballX, ballY, 5, 5);

        //Left Paddle
        g.fillRect(paddleLX, paddleLY, paddleLW, paddleLH);


    }

    public void startBall() {
        ballXV = -1;
        ballX += ballXV;
        ballY += ballYV;
        repaint();

    }

    public void startGame() {
        gamePaused = false;
        startBall();

    }

    public static void pauseGame() {
        gamePaused = true;
    }


    public void actionPerformed(ActionEvent e) {

        if(!gamePaused) 
            startGame();
        if(gamePaused)
            pauseGame();

    }

}

and here is the Key Bindings class

package game.util;

import game.render.GraphicRender;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GetKeyStroke extends KeyAdapter{

    public void keyPressed(KeyEvent e) {

        GraphicRender gRndObj = new GraphicRender();

        if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            if(!GraphicRender.gamePaused) {
                System.out.println("Game is not paused");
                GraphicRender.pauseGame();

            }else{
                System.out.println("Game is paused");
                gRndObj.startGame();
            }
        }
    }

}

I am aware this code is extremely messy, so sorry about that

  1. Your gamePaused() method should not be static. Make it an instance variable.
  2. Your GetKeyStroke class should not be creating a new GraphicRender object. Instead pass the current viewed GraphicRender object into this class via a constructor parameter.
  3. Then call your pause and start methods on the current viewed GraphicRender object.
  4. For my money, I'd have my pause method call stop() on the Timer object and the restart method call start() on my Timer object.
  5. Better to use Key Bindings for this rather than a KeyListener. The Oracle Key Bindings tutorial will explain why this is so.

To pass the reference of one class into another, simply use a parameter such as a constructor parameter. For example,

class MyBar {

   private MyFoo myFoo;

   public MyBar(MyFoo myFoo) {
      this.myFoo = myFoo;
   }

   public void someMethod() {
      if (!myFoo.isBazz()) {
         myFoo.setBazz(true);
      } else {
         myFoo.startBazzer();
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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