I’m making a simple little pong game for my java class but I can’t figure out how to get the paddle on the right side to bounce the ball back over to the left side, the ball just goes through the paddle. So here’s some of the code I got and a screenshot.
int x_pos = 100;
int y_pos = 100;
int x_width = 30;
int y_height = 30;
int right = 5;
int left = -5;
int up = -5;
int down = 5;
int width, height;
int player1X = 50;
int player1Y = 200;
int player1W = 10;
int player1H = 100;
int player2X;
int player2Y = 200;
int player2W = 10;
int player2H = 100;
int player1Score = 0;
int player2Score = 0;
boolean moveBallDown, moveBallRight;
boolean movePlayer1Up, movePlayer1Down;
boolean movePlayer2Up, movePlayer2Down;
boolean game, gameOver;
public void start ()
{
Thread th = new Thread (this);
th.start ();
game = true;
}
@Override
public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.setColor(Color.blue);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
g.fillOval(x_pos, y_pos, x_width, y_height);
g.fillRect(player1X, player1Y, player1W, player1H);
g.fillRect(player2X, player2Y, player2W, player2H);
g.setColor(Color.yellow);
g.drawString("Player1 Score: ", 20, 10);
g.drawString(Integer.toString(player1Score), 105, 10);
g.drawString("Player2 Score: ", getWidth() - 150, 10);
g.drawString(Integer.toString(player2Score), getWidth() - 70, 10);
if (gameOver)
g.drawString("GAME OVER", getWidth()/2-30, getHeight()/2);
}
public void drawShape (int new_x, int new_y)
{
x_pos = new_x;
y_pos = new_y;
}
public void drawPlayer1 ( int new_y)
{
player1Y = new_y;
}
public void drawPlayer2 ( int new_y)
{
player2X = getWidth() - 50;
player2Y = new_y;
}
public void run ()
{
moveBallRight = true;
moveBallDown = true;
while (game)
{
moveShape();
drawShape(x_pos, y_pos);
movePlayer();
drawPlayer1(player1Y);
drawPlayer2(player2Y);
repaint();
try
{
Thread.sleep (25);
}
catch (InterruptedException ex){}
if (player1Score == 10 || player2Score == 10)
{
game = false;
gameOver = true;
}
}
}
public void moveShape()
{
if(moveBallRight)
{
x_pos += right;
if (x_pos >= (getWidth() - x_width)){
moveBallRight = false;
player1Score++;
}
}
else
{
x_pos += left;
if ( x_pos <= 0){
moveBallRight = true;
player2Score++;
}
}
if(moveBallDown)
{
y_pos += down;
if(y_pos >= (getHeight()-y_height))
moveBallDown = false;
}
else
{
y_pos += up;
if(y_pos <= 0)
moveBallDown = true;
}
//bounces ball off left paddle
if(x_pos == (player1X + player1W) && (y_pos >= player1Y || (y_pos+30) >= player1Y) && y_pos <= (player1Y+75))
moveBallRight = true;
//Supposed to bounce ball off right paddle
if((x_pos + x_width) == player2X && (y_pos >= player2Y || (y_pos+30) >= player2Y) && y_pos <= (player2Y+75))
moveBallRight = false;
}
Just to clarify a couple things x/y_pos is the starting point to draw the ball player x/y is the starting point for the paddles. I have getWidth() for the right paddle in the drawPlayer2 if whoever runs this makes the window bigger.
Anyone know what I’m doing wrong here for the last two lines of code?