-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathBall.java
More file actions
71 lines (59 loc) · 1.41 KB
/
Ball.java
File metadata and controls
71 lines (59 loc) · 1.41 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.awt.*;
public class Ball
{
public static final int RADIUS = 10;
private int x, y;
private int dx, dy;
private Color color;
private BallSpeedAttributes attributesSpeed;
public Ball(Color c, int x, int y)
{
color = c;
this.x = x;
this.y = y;
dx = (Math.random() < .5) ? 1 : -1;
dy = (Math.random() < .5) ? 1 : -1;
attributesSpeed = new BallSpeedAttributes();
}
public void move()
{
attributesSpeed.incrementDelay();
if (attributesSpeed.isTimeToSpeedUp())
attributesSpeed.incrementSpeed();
x += (int) (getUnitDX() * attributesSpeed.getSpeed());
y += (int) (getUnitDY() * attributesSpeed.getSpeed());
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
private double getUnitDX()
{
return ((double) dx / (double) (Math.sqrt(dx * dx + dy * dy)));
}
private double getUnitDY()
{
return ((double) dy / (double) (Math.sqrt(dx * dx + dy * dy)));
}
public void bounceSide()
{
dx = -dx;
dy = (int) (Math.random() * 8) - 4;
move();
move();
}
public void bounceTop()
{
dy = -dy;
move();
}
public void draw(Graphics g)
{
g.setColor(color);
g.fillOval(x - RADIUS, y - RADIUS, RADIUS * 2, RADIUS * 2);
}
}