Just wanted to update… I changed my code to use booleans like youve suggested. The performance increased by at least 500% lol. The paddles are now super responsive its ridiculous. I might actually have to turn down their speed. Thank you so much! Why the hell does that make such a huge difference??
Heres what Ive done so far. Added scores and all that. I NEED to use double buffering because the flickering is killing me! Any critiques??
package pong;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import javax.swing.JFrame;
public class Game extends JFrame {
private static final long serialVersionUID = 1L;
private Rectangle paddleOne;
private Rectangle paddleTwo;
private int ballXPosition;
private int ballYPosition;
private final int BALL_VELOCITY;
private final int BALL_RADIUS;
private int ballXSpeed;
private int ballYSpeed;
Timer gameTimer;
int playerOneScore;
int playerTwoScore;
//booleans for key presses
private boolean wPressed;
private boolean sPressed;
private boolean upPressed;
private boolean downPressed;
public Game() {
setSize(640, 480);
setTitle("Pong... Almost");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(new KeyListen());
setResizable(false);
setVisible(true);
playerOneScore = 0;
playerTwoScore = 0;
paddleOne = new Rectangle(5, 75, 15, 80);
paddleTwo = new Rectangle(this.getWidth() - 20, 75, 15, 80);
ballXPosition = this.getWidth() / 2;
ballYPosition = this.getHeight() / 2;
BALL_RADIUS = 10;
BALL_VELOCITY = 8;
ballXSpeed = BALL_VELOCITY;
ballYSpeed = BALL_VELOCITY;
wPressed = false;
sPressed = false;
upPressed = false;
downPressed = false;
}
public void paint(Graphics g) {
// background
g.setColor(Color.black);
g.fillRect(0, 0, 640, 480);
//ball
g.setColor(Color.green);
g.fillOval(ballXPosition, ballYPosition, BALL_RADIUS * 2, BALL_RADIUS * 2);
//paddle 1
g.setColor(Color.green);
g.fillRect(paddleOne.x, paddleOne.y, paddleOne.width, paddleOne.height);
//paddle2
g.fillRect(paddleTwo.x, paddleTwo.y, paddleTwo.width, paddleTwo.height);
//score
g.setFont(new Font("Arial Bold", Font.PLAIN, 40));
g.drawString(Integer.toString(playerOneScore), 250, 75);
g.drawString(Integer.toString(playerTwoScore), 350, 75);
}
public void run() {
//loop to constantly repaint the screen
while(true) {
//collision detection between the ball and paddles
if ((paddleOne.x + 15) >= ballXPosition &&
paddleOne.y <= ballYPosition && paddleOne.y + paddleOne.height >= ballYPosition) {
ballXSpeed = BALL_VELOCITY;
}
if (paddleTwo.x <= (ballXPosition + (BALL_RADIUS * 2)) &&
paddleTwo.y <= ballYPosition && paddleTwo.y + paddleTwo.height >= ballYPosition) {
ballXSpeed = -(BALL_VELOCITY);
}
/* Collision detections between the ball and all four walls*/
if(ballXPosition > this.getWidth() - (BALL_RADIUS * 2)) {
ballXSpeed = -(BALL_VELOCITY);
playerOneScore++;
}
else if(ballXPosition < 0) {
ballXSpeed = BALL_VELOCITY;
playerTwoScore++;
}
if(ballYPosition > this.getHeight() - (BALL_RADIUS * 2)) {
ballYSpeed = -(BALL_VELOCITY);
}
else if(ballYPosition < BALL_RADIUS * 2) {
ballYSpeed = BALL_VELOCITY;
}
ballXPosition += ballXSpeed;
ballYPosition += ballYSpeed;
//moving paddles
if (wPressed) {
paddleOne.y -= 10;
}
if (sPressed) {
paddleOne.y += 10;
}
if (upPressed){
paddleTwo.y -= 10;
}
if (downPressed) {
paddleTwo.y += 10;
}
repaint();
//set all key presses to false if paddles are going off screen.. fix this its sloppy!
if (paddleOne.y <= 30) {
wPressed = false;
}
if (paddleOne.y > 395 ) {
sPressed = false;
}
if (paddleTwo.y <= 30) {
upPressed = false;
}
if (paddleTwo.y > 395) {
downPressed = false;
}
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private class KeyListen implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
//paddle one move
if (key == KeyEvent.VK_W && paddleOne.y >= 30) {
wPressed = true;
}
if (key == KeyEvent.VK_S && paddleOne.y < 395 ) {
sPressed = true;
}
//paddle two move
if (key == KeyEvent.VK_UP && paddleTwo.y >= 30) {
upPressed = true;
}
if (key == KeyEvent.VK_DOWN && paddleTwo.y < 395) {
downPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
//paddle one stop
if (key == KeyEvent.VK_W) {
wPressed = false;
}
if (key == KeyEvent.VK_S) {
sPressed = false;
}
//paddle two stop
if (key == KeyEvent.VK_UP) {
upPressed = false;
}
if (key == KeyEvent.VK_DOWN) {
downPressed = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
}
package pong;
public class GameDriver {
public static void main(String[] args) {
Game gameTest = new Game();
gameTest.run();
}
}