-1

I've been trying to learn more about Java recently, so I followed an online guide on how to build a game in Java. Everything worked fine, but I wanted to add on more so I could make it my own. It's been going OK so far, but I've recently reached a personal impasse. Here's the code so far, including what I've added on my own (my question is at the bottom):

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PongGame extends JComponent implements ActionListener, MouseMotionListener {
    public static PongGame game = new PongGame();
    private int ballX = 400;
    private int ballY = 250;
    private int paddleX = 0;
    private int ballYSpeed = 2;
    private int ballXSpeed = 2;
    private static int time = 15;
    public static Timer t = new Timer(time, game);
    public static void main(String[] args) {
        JFrame window = new JFrame("Pong Game by Ethan");
        window.add(game);
        window.pack();
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        t.start();
        window.addMouseMotionListener(game);
    }
    public Dimension getPreferredSize() {
        return new Dimension(800, 600);
    }
    @Override
    protected void paintComponent(Graphics g) {
        //draw the background
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 800, 600);
        //draw the paddle
        g.setColor(Color.BLACK);
        g.fillRect(paddleX, 510, 150, 15);
        //draw the ball
        g.setColor(Color.BLACK);
        g.fillOval(ballX, ballY, 25, 25);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        ballX = ballX + ballXSpeed;
        ballY = ballY + ballYSpeed;
        if (ballX >= paddleX && ballX <= paddleX + 150 && ballY >= 485) {
            ballYSpeed = -2;
            lessTime();
        }
        if (ballX >=775) {
            ballXSpeed = -2;
        }
        if (ballX <= 0) {
            ballXSpeed = 2;
        }
        if (ballY <= 0) {
            ballYSpeed = 2;
        }
        if (ballY == 500) {
            PongGame.infoBox("GAME OVER","");
            t.stop();
            System.exit(0);
        }
        repaint();
    }
    @Override
    public void mouseDragged(MouseEvent e) {
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        paddleX = e.getX() - 75;
        repaint();
    }
    public static void infoBox(String infoMessage, String titleBar) {
        JOptionPane.showMessageDialog(null, infoMessage, "Game Over" + titleBar, JOptionPane.INFORMATION_MESSAGE);
    }
    public static void lessTime() {
        time--;
    }
}

As you can see, I have a variable in there called time near the top which is used by Timer t right below it, and a method named lessTime at the bottom which just removes 1 from the time variable when called. I have it set to call the lessTime method in the first if statement when the ball bounces off of the paddle to increase the speed of the game (I'm working towards a hit counter), but it doesn't seem to increase the speed at all.

I've tried using --time;, time--;, and time = time - 1; all inside the lessTime method and by themselves in the if statement, but none of them remove any amount from the time variable. Can someone explain why the time variable isn't affected by either the method or alone inside the if statement, and also how I can fix it?

Thank you!

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Ethan
  • 51
  • 4
  • 5
    You're confusing the modification of the value of the `time` variable with modification of the value contained in the `Timer`. Changing one won't affect the other, because Java is a [pass by value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) language. – azurefrog Jan 15 '17 at 21:31

1 Answers1

3

The problem you're encountering is because Java is a "pass-by-value" language. This means that when you pass your int time variable into the constructor you're using to instantiate your Timer t variable, you have actually passed in the value of int time (15), not the int timevariable itself. The result is that if you change the value of your int time variable by decrementing it (so that it's now 14), the value inside your Timer t variable is still 15.

The shortest solution I can think of to get around your impasse is to add a line of code to your lessTime() method, like so:

public static void lessTime() {
    time--;
    // set t to a new Timer with the new decremented time value.
    t = new Timer(time, game);
}
James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • 1
    The `Timer` can also be modified after construction using `setDelay`. – Andreas Jan 15 '17 at 21:44
  • Actually, it should be modified using `setDelay`. Otherwise you would need to `stop` it first and `start` the new one afterwards. – Andreas Jan 15 '17 at 21:57
  • This did work, but like Andreas said I had to use t.stop(); and t.start(); before and after resetting the timer. I also tried using t.setDelay(); like you suggested, but any value inside the parentheses set the game to a different pace but wouldn't increase continually like stop and start did. Do you have any suggestions on how to use setDelay? – Ethan Jan 16 '17 at 21:48