|
|
@ -1,10 +1,28 @@ |
|
|
|
package PingPong; |
|
|
|
|
|
|
|
import java.awt.*; |
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
public class Ball extends Rectangle { |
|
|
|
|
|
|
|
int xVelocity; |
|
|
|
int yVelocity; |
|
|
|
int ballSpeed = 3; |
|
|
|
Random random; |
|
|
|
|
|
|
|
Ball(int x, int y , int width, int height){ |
|
|
|
super(x,y,width,height); |
|
|
|
random =new Random(); |
|
|
|
int randomXDirection = random.nextInt(2); |
|
|
|
if (randomXDirection == 0) |
|
|
|
randomXDirection--; |
|
|
|
setXDirection(randomXDirection * ballSpeed); |
|
|
|
|
|
|
|
int randomYDirection = random.nextInt(2); |
|
|
|
if (randomYDirection == 0) |
|
|
|
randomYDirection--; |
|
|
|
setYDirection(randomYDirection * ballSpeed); |
|
|
|
} |
|
|
|
public void setXDirection (int randomXDirection){ |
|
|
|
xVelocity = randomXDirection; |
|
|
|
} |
|
|
|