import java.awt.*;
import java.util.*;
import java.applet.*;
public class PongBall
{
//Variables============================================
//position and speed of ball
private int x_pos;
private int y_pos;
private int x_speed;
private int y_speed;
//sides of the ball
private int ball_top;
private int ball_bottom;
private int ball_left;
private int ball_right;
//sides of player 2 paddles
private int player_top2;
private int player_bottom2;
private int player_right2;
private int player_left2;
//sides of player 1 paddles
private int player_top1;
private int player_bottom1;
private int player_right1;
private int player_left1;
//score variables
private int scoreP1;
private int scoreP2;
String score1 = Integer.toString (scoreP1);
String score2 = Integer.toString (scoreP2);
//boundaries
private static final int right_out = 390;
private static final int left_out = 10;
private static final int down_out = 290;
private static final int up_out = 10;
//ball radius
private static final int radius = 10;
//sets ball position/speed
public PongBall (int x_pos, int y_pos)
{
this.x_pos = x_pos;
this.y_pos = y_pos;
x_speed = 2;
y_speed = 2;
}
//moves ball and checks if the ball is out of bounds
public void move ()
{
x_pos += x_speed;
y_pos += y_speed;
isBallOut ();
}
//Checks to see if ball hits a side, and then bounces it back if it hits the top
// or resets and adds the score if it hits the sides
public void isBallOut ()
{
//checks the direction of ball
if (x_speed < 0)
{
//checks to see if the ball is out
if (x_pos < left_out)
{
x_pos = 200;
y_pos = 150;
scoreP2 += 1;
//changes the int score to a string
score2 = Integer.toString (scoreP2);
}
}
//the folowing ifs are similar as above
else if (x_speed > 0)
{
if (x_pos > right_out)
{
x_pos = 200;
y_pos = 150;
scoreP1 += 1;
score1 = Integer.toString (scoreP1);
}
}
if (y_speed < 0)
{
if (y_pos < up_out)
{
//changes the direction of the ball
y_speed = -y_speed;
}
}
else if (y_speed > 0)
{
if (y_pos > down_out)
{
//changes direction
y_speed = -y_speed;
}
}
}
//The test for collision construct was taken from a tutorial on the given site
//Tests the collision of the ball with paddle 2
public void testForCollisionPlayer2 (Paddle player2)
{
//sets the position of the sides of the ball
ball_top = y_pos - radius;
ball_bottom = y_pos + radius;
ball_left = x_pos - radius;
ball_right = x_pos + radius;
//gets the position of the paddle
player_top2 = player2.getYPos ();
player_bottom2 = player2.getYPos () + player2.getYSize ();
player_right2 = player2.getXPos () + player2.getXSize ();
player_left2 = player2.getXPos ();
//Test if the y position of the ball and the paddle matches
if ((ball_top >= player_top2 - radius) && (ball_bottom <= player_bottom2 + radius))
{
//tests if the x position of the ball and the paddle matches
if (ball_right >= player_left2)
{
//Changes the direction of the ball
x_speed = -x_speed;
}
}
}
//Same as about for paddle 1
public void testForCollisionPlayer1 (Paddle player1)
{
ball_top = y_pos - radius;
ball_bottom = y_pos + radius;
ball_left = x_pos - radius;
ball_right = x_pos + radius;
player_top1 = player1.getYPos ();
player_bottom1 = player1.getYPos () + player1.getYSize ();
player_right1 = player1.getXPos () + player1.getXSize ();
player_left1 = player1.getXPos ();
if ((ball_top >= player_top1 - radius) && (ball_bottom <= player_bottom1 + radius))
{
if (ball_left <= player_right1)
{
x_speed = -x_speed;
}
}
}
//returns speed
public int getXSpeed ()
{
return x_speed;
}
//returns player1 score
public int getScore1 ()
{
return scoreP1;
}
//returns player2 score
public int getScore2 ()
{
return scoreP2;
}
//returns y position of ball
public int getYPos ()
{
return y_pos;
}
public void paint (Graphics g)
{
//puts score on the screen
g.setColor (Color.yellow);
g.drawString (score1, 50, 50);
g.drawString (score2, 325, 50);
//put ball on the screen
g.setColor (Color.green);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
//ends game if player reaches 10
if (scoreP1 == 10)
{
g.setColor (Color.red);
g.drawString ("Player 1 Wins!", 150, 75);
}
if (scoreP2 == 10)
{
g.setColor (Color.red);
g.drawString ("Player 2 Wins!", 150, 75);
}
}
}