-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.java
46 lines (41 loc) · 1.24 KB
/
Ball.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package Pong;
import java.awt.*;
//import java.awt.event.*;
import java.util.*;
//import javax.swing.*;
public class Ball extends Rectangle{
Random random;
int xVelocity;
int yVelocity;
int initialSpeed = 2;
SoundManager bounceSound; // Add a SoundManager field
Ball(int x, int y, int width, int height){
super(x,y,width,height);
bounceSound = new SoundManager("Pong/plastic-ball-bounce-14790.wav"); // Initialize SoundManager
random = new Random();
int randomXDirection = random.nextInt(2);
if (randomXDirection == 0){
randomXDirection--;
}
setXDirection(randomXDirection*initialSpeed);
int randomYDirection = random.nextInt(2);
if (randomYDirection == 0){
randomYDirection--;
}
setYDirection(randomYDirection*initialSpeed);
}
public void setXDirection(int randomXDirection){
xVelocity = randomXDirection;
}
public void setYDirection(int randomYDirection){
yVelocity = randomYDirection;
}
public void move(){
x += xVelocity;
y += yVelocity;
}
public void draw(Graphics g){
g.setColor(Color.WHITE);
g.fillOval(x, y, height, width);
}
}