Noob Questions From Nickropheliac : My Stupid Interval Bug

Sorry for flood this forum so often, but I always need help so I can apply my new knowledge to my games in the future. What I’m trying to do, is increase the ballDeltaX, ballDeltaY, and paddleSpeed every rallyInterval. When I run this it will only do this once. I tried running it in a loop, but my effort was futile. Please help, JGO

p.s Sorry for posting 50 times a day.


		import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

public class PongPanel extends JPanel implements ActionListener, KeyListener {
	private Game game;

	private boolean running = true;

	private boolean upPressed = false;
	private boolean downPressed = false;
	private boolean wPressed = false;
	private boolean sPressed = false;

	private int ballX = 400;
	private int ballY = 200;
	private int diameter = 20;
	private int ballDeltaX = -5;
	private int ballDeltaY = 5;

	private int playerOneX = 25;
	private int playerOneY = 220;
	private int playerOneWidth = 20;
	private int playerOneHeight = 60;

	private int playerTwoX = 750;
	private int playerTwoY = 220;
	private int playerTwoWidth = 20;
	private int playerTwoHeight = 60;

	private int paddleSpeed = 10;

	Random random = new Random();
	private final float hue = random.nextFloat();
	private final float saturation = 0.9f; 
	 private float luminance = 1.0f; 

	
	private int deaths;
	private int rallyScore = 0;
	private int rallyInterval = rallyScore & 5;

	private long currentTime = System.currentTimeMillis();
	public static Thread thread;

	private int[] Colors = new int[getWidth() * getHeight()];

	public PongPanel() {
		Random rand = new Random();
		setBackground(new Color(rand.nextInt(0xFF4AF6)));

		setFocusable(true);
		addKeyListener(this);

		Timer timer = new Timer(1000 / 60, this);
		timer.start();
	}

	public void actionPerformed(ActionEvent e) {
		step();
	}

	public void step() {

		if (upPressed) {
			if (playerTwoY - paddleSpeed > 0) {
				playerTwoY -= paddleSpeed;
			}
		}
		if (downPressed) {
			if (playerTwoY + paddleSpeed + playerTwoHeight < getHeight()) {
				playerTwoY += paddleSpeed;
			}
		}

		if (wPressed) {
			if (playerOneY - paddleSpeed > 0) {
				playerOneY -= paddleSpeed;
			}
		}
		if (sPressed) {
			if (playerOneY + paddleSpeed + playerOneHeight < getHeight()) {
				playerOneY += paddleSpeed;
			}
		}

		int nextBallLeft = ballX + ballDeltaX;
		int nextBallRight = ballX + diameter + ballDeltaX;
		int nextBallTop = ballY + ballDeltaY;
		int nextBallBottom = ballY + diameter + ballDeltaY;

		int playerOneRight = playerOneX + playerOneWidth;
		int playerOneTop = playerOneY;
		int playerOneBottom = playerOneY + playerOneHeight;

		float playerTwoLeft = playerTwoX;
		float playerTwoTop = playerTwoY;
		float playerTwoBottom = playerTwoY + playerTwoHeight;

		if (nextBallTop < 0 || nextBallBottom > getHeight()) {
			ballDeltaY *= -1;
	
		}

		if (nextBallLeft < playerOneRight) {

			if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) {

				System.out.println("You died. Your rally was " + rallyScore + ".");
				deaths++;
				ballX = 400;
				ballY = 200;
			} else {
				rallyScore++; 
				ballDeltaX *= -1;
			   rallyInterval++; 
			}
		}

		if (nextBallRight > playerTwoLeft) {

			if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) {

				System.out.println("You died. Your rally was " + rallyScore + ".");

				deaths++;

				ballX = 400;
				ballY = 200;
			} else {
				rallyScore++;
				ballDeltaX *= -1;
			    rallyInterval++;
			}
		}

		ballX += ballDeltaX;
		ballY += ballDeltaY;
		
		if ( rallyInterval == 5){
			ballDeltaX = ballDeltaX -2; 
			ballDeltaY = ballDeltaY +2; 
			paddleSpeed = paddleSpeed + 2 ;
		}
		
		repaint();
	}

	public synchronized void Score() {

	}

	public void RandomColor() {

	}

	public void paintComponent(Graphics g) {

		super.paintComponent(g);
		Random rand = new Random();
	
		for (int lineY = 0; lineY < getHeight(); lineY += 50) {
			g.setColor(new Color(rand.nextInt(0xFF4AF6)));
			g.fillRoundRect(400, lineY, 20, 800, 0, 0);
		}

		g.setColor(Color.WHITE);
		g.fillRoundRect(ballX, ballY, 25, 25, 5, 25);
	for (int tick = 0; tick > getHeight(); tick--){
	
            g.setColor(new Color (rand.nextInt(0xFF4AF6)));
	}
	g.setColor(new Color(rand.nextInt(0xFF4AF6)));
	g.fillRoundRect(playerOneX, playerOneY, playerOneWidth,
					playerOneHeight, 50, 5);

			g.fillRoundRect(playerTwoX, playerTwoY, playerTwoWidth,
					playerTwoHeight, 50, 5);
	
			g.setColor(Color.WHITE);
			g.fillRoundRect(335, 180, 150, 150 , 50, 50);
			  g.setFont(new Font(Font.DIALOG, Font.BOLD, 150));
		      String rally = " " + rallyScore;
			  g.setColor(new Color (0xFF2172));
		      g.drawString(rally, 328, 310);
			  g.dispose();	
	}

	

	public void keyTyped(KeyEvent e) {
	}

	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			upPressed = true;
		} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			downPressed = true;
		} else if (e.getKeyCode() == KeyEvent.VK_W) {
			wPressed = true;
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			sPressed = true;
		}
	}

	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			upPressed = false;
		} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			downPressed = false;
		} else if (e.getKeyCode() == KeyEvent.VK_W) {
			wPressed = false;
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			sPressed = false;
		}
	}

;
		

Of corse if you run it will only do it once, beacouse you have a clear condition, only when rallyInterval == 5 increase the speed of the balls and the paddle, this is only for once. So you have to make a generalized counter variable:


if(rallyInterval == intervalCounter){
	ballDeltaX -= 2; 
	ballDeltaY += 2; 
	paddleSpeed += 2 ;
	intervalCounter += 5;
}

So intervalCounter is the counter variable, an integer, with first value 5, the initialized one, after goes from 5 in 5, so next increase will be at 10, and so on.

I tried this before already, but unfortunately it slowed down the movement speed of the ball after a single rally. Perhaps it’s the placement of the code. I put the code within the brackets of the else statement

Which else statement? Isn’t possible to slow down the speed, it dosen’t have anything to do with.

I fixed the slower movement thing. It was just a stupid operator mistake of mine. However, the delta still changes after one rally. Even when the rallyInterval == counterInterval. It’s peculiar, and I can hardly speculate what’s wrong since I’m still just a 3 week java noob. I’m just learning a lot from the progress that I’ve been making, and the ideas I’ve decided to implement.

I would give you some pseudo code but my computer is acting up right now, Look at your rallyInterval variable which is defined at the beginning of your class.


rallyInterval = rallyScore & 5;

/Edit 2: Additionally you could do something like this @ http://pastebin.com/6xbQ7Kve (Fixed)

I tried the code you provided and the ball spazzed out and jolted around the screen repeatedly, bouncing off the walls, hitting the paddle 169 times

You didn’t expect me to write the final code for you, Did you?

Change the ‘&’ to a ‘%’

That’s exactly what I put in the code I provided him with. :confused:

Ok, you could of edited your post, it’s misleading since the snippet differs from the rest of the code.

It’s in the pastebin link, As I took the time to format his entire class. I may have broke his code accidentally in the process. But, The fix is in there. Sorry for misleading :frowning:

What does the ‘%’ operator actually contain? What is the difference between this operator and the bit, gang? :slight_smile:

% is the modulo operator, it returns the remainder of division.

& in the context of integers is the bitwise AND operator.

So, Basically if you do;

if (rallyScore % 5 == 0)

That’s like saying, If rallyScore can be divided by five and, The result is zero. Then the condition is true. If the result is anything other than 0, the condition is false. You can assign 5 to your rallyInterval to change it in the future.

the % operator returns the remainder in division.

For example:
17 % 8 = 1, because 8 can go into 17 twice with a remainder of 1.

thus:
11 % 5 = 1
16 % 6 = 4
(and so forth)

EDIT: Belated pizza-ninja, didnt see his post. :smiley:

Oh, okay! That actually makes sense. Thanks guys! But what I don’t understand is why now that i’ve done this, the ball is jittering and enormous speeds and bouncing all of the walls xD

import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
 
import javax.swing.JPanel;
import javax.swing.Timer;
 
public class PongPanel extends JPanel implements ActionListener, KeyListener {
 
    private Game game;
 
    private boolean running = true;
 
    private boolean upPressed = false, downPressed = false, wPressed = false, sPressed = false;
 
    private int ballX = 400, ballY = 200;
    private int diameter = 20;
    private int ballDeltaX = -5, ballDeltaY = 5;
 
    private int playerOneX = 25, playerOneY = 220;
    private int playerOneWidth = 20, playerOneHeight = 60;
 
    private int playerTwoX = 750, playerTwoY = 220;
    private int playerTwoWidth = 20, playerTwoHeight = 60;
 
    private int paddleSpeed = 10;
 
    Random random = new Random();
    private final float hue = random.nextFloat();
    private final float saturation = 0.9f;
    private float luminance = 1.0f;
 
    private int deaths;
    private int rallyScore = 0;
    private int rallyInterval = 5;
 
    private long currentTime = System.currentTimeMillis();
    public static Thread thread;
 
    private int[] Colors = new int[getWidth() * getHeight()];
 
    public PongPanel() {
        Random rand = new Random();
        setBackground(new Color(rand.nextInt(0xFF4AF6)));
 
        setFocusable(true);
        addKeyListener(this);
 
        Timer timer = new Timer(1000 / 60, this);
        timer.start();
    }
 
    public void actionPerformed(ActionEvent e) {
        step();
    }
 
    public void step() {
 
        if (upPressed) {
            if (playerTwoY - paddleSpeed > 0) {
                playerTwoY -= paddleSpeed;
            }
        }
        if (downPressed) {
            if (playerTwoY + paddleSpeed + playerTwoHeight < getHeight()) {
                playerTwoY += paddleSpeed;
            }
        }
 
        if (wPressed) {
            if (playerOneY - paddleSpeed > 0) {
                playerOneY -= paddleSpeed;
            }
        }
        if (sPressed) {
            if (playerOneY + paddleSpeed + playerOneHeight < getHeight()) {
                playerOneY += paddleSpeed;
            }
        }
 
        int nextBallLeft = ballX + ballDeltaX;
        int nextBallRight = ballX + diameter + ballDeltaX;
        int nextBallTop = ballY + ballDeltaY;
        int nextBallBottom = ballY + diameter + ballDeltaY;
 
        int playerOneRight = playerOneX + playerOneWidth;
        int playerOneTop = playerOneY;
        int playerOneBottom = playerOneY + playerOneHeight;
 
        float playerTwoLeft = playerTwoX;
        float playerTwoTop = playerTwoY;
        float playerTwoBottom = playerTwoY + playerTwoHeight;
 
        if (nextBallTop < 0 || nextBallBottom > getHeight()) {
            ballDeltaY *= -1;
 
        }
 
        if (nextBallLeft < playerOneRight) {
 
            if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) {
 
                System.out.println("You died. Your rally was " + rallyScore + ".");
                deaths++;
                ballX = 400;
                ballY = 200;
            } else {
                rallyScore++;
                ballDeltaX *= -1;
            }
        }
 
        if (nextBallRight > playerTwoLeft) {
 
            if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) {
 
                System.out.println("You died. Your rally was " + rallyScore + ".");
 
                deaths++;
 
                ballX = 400;
                ballY = 200;
            } else {
                rallyScore++;
                ballDeltaX *= -1;
            }
        }
 
        ballX += ballDeltaX;
        ballY += ballDeltaY;
 
        if (rallyScore % rallyInterval == 0) {
            ballDeltaX = ballDeltaX - 2;
            ballDeltaY = ballDeltaY + 2;
            paddleSpeed = paddleSpeed + 2;
        }
 
        repaint();
    }
 
    public synchronized void Score() {
 
    }
 
    public void RandomColor() {
 
    }
 
    public void paintComponent(Graphics g) {
 
        super.paintComponent(g);
        Random rand = new Random();
 
        for (int lineY = 0; lineY < getHeight(); lineY += 50) {
            g.setColor(new Color(rand.nextInt(0xFF4AF6)));
            g.fillRoundRect(400, lineY, 20, 800, 0, 0);
        }
 
        g.setColor(Color.WHITE);
        g.fillRoundRect(ballX, ballY, 25, 25, 5, 25);
        for (int tick = 0; tick > getHeight(); tick--) {
 
            g.setColor(new Color(rand.nextInt(0xFF4AF6)));
        }
        g.setColor(new Color(rand.nextInt(0xFF4AF6)));
        g.fillRoundRect(playerOneX, playerOneY, playerOneWidth,
                playerOneHeight, 50, 5);
 
        g.fillRoundRect(playerTwoX, playerTwoY, playerTwoWidth,
                playerTwoHeight, 50, 5);
 
        g.setColor(Color.WHITE);
        g.fillRoundRect(335, 180, 150, 150, 50, 50);
        g.setFont(new Font(Font.DIALOG, Font.BOLD, 150));
        String rally = " " + rallyScore;
        g.setColor(new Color(0xFF2172));
        g.drawString(rally, 328, 310);
        g.dispose();
    }
 
    public void keyTyped(KeyEvent e) {
    }
 
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            upPressed = true;
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            downPressed = true;
        } else if (e.getKeyCode() == KeyEvent.VK_W) {
            wPressed = true;
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            sPressed = true;
        }
    }
 
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            upPressed = false;
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            downPressed = false;
        } else if (e.getKeyCode() == KeyEvent.VK_W) {
            wPressed = false;
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            sPressed = false;
        }
    }
}

Well, First off, Does it act erratic right from the start or, does it misbehave once the score = 5?

You were changing the variables every frame when rallyScore was a multiple of 5 (including zero).

if (nextBallLeft < playerOneRight) {
			
			if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) {
				
				System.out.println("You died. Your rally was " + rallyScore + ".");
				deaths++;
				ballX = 400;
				ballY = 200;
			} else {
				rallyScore++;
				ballDeltaX *= -1;
				
+				if (rallyScore % rallyInterval == 0) {
+					ballDeltaX = ballDeltaX - 2;
+					ballDeltaY = ballDeltaY + 2;
+					paddleSpeed = paddleSpeed + 2;
+				}
			}
		}
		
		if (nextBallRight > playerTwoLeft) {
			
			if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) {
				
				System.out.println("You died. Your rally was " + rallyScore + ".");
				
				deaths++;
				
				ballX = 400;
				ballY = 200;
			} else {
				rallyScore++;
				ballDeltaX *= -1;
				
+				if (rallyScore % rallyInterval == 0) {
+					ballDeltaX = ballDeltaX - 2;
+					ballDeltaY = ballDeltaY + 2;
+					paddleSpeed = paddleSpeed + 2;
+				}
			}
		}
		
		ballX += ballDeltaX;
		ballY += ballDeltaY;
		
-		/*if (rallyScore % rallyInterval == 0) {
-			ballDeltaX = ballDeltaX - 2;
-			ballDeltaY = ballDeltaY + 2;
-			paddleSpeed = paddleSpeed + 2;
-		}*/