-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.java
More file actions
45 lines (36 loc) · 996 Bytes
/
ball.java
File metadata and controls
45 lines (36 loc) · 996 Bytes
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
import java.awt.*;
import java.util.*;
public class ball extends Rectangle{
Random rand;
int xVelocity;
int yVelocity;
int initial_speed = 2;
public ball(int x, int y, int width, int height){
super(x , y, width, height);
rand = new Random();
int randomXDir = rand.nextInt(2);
if(randomXDir == 0){
randomXDir--;
}
setXDirection(randomXDir*initial_speed);
int randomYDir = rand.nextInt(2);
if(randomYDir == 0){
randomYDir--;
}
setYDirection(randomYDir*initial_speed);
}
public void setYDirection(int randY){
yVelocity = randY;
}
public void setXDirection(int randX){
xVelocity = randX;
}
public void move(){
x+=xVelocity;
y+=yVelocity;
}
public void draw(Graphics g){
g.setColor(Color.white);
g.fillOval(x, y, width, height);
}
}