-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.java
More file actions
87 lines (59 loc) · 1.33 KB
/
Ball.java
File metadata and controls
87 lines (59 loc) · 1.33 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package pong;
import java.awt.*;
public class Ball {
private int x, y, cx, cy, speed, size;
private Color color ;
static final int MAX_SPEED = 6;
public Ball(int x, int y, int cx, int cy, int speed, int size, Color color){
this.x = x;
this.y = y;
this.cx = cx;
this.cy = cy;
this.speed = speed;
this.size= size;
this.color = color;
}
public void paint(Graphics g){
g.setColor(color);
g.fillOval(x, y, size, size);
}
public void moveBall(){
x += cx;
y += cy;
}
public void bounceOffEdges(int top, int bottom){
if (y > bottom - size) {
reverseY();
}
if (y < top) {
reverseY();
}
}
public void reverseY() {
cy *= -1 ;
}
public void reverseX() {
cx *= -1;
}
public int getY(){
return y;
}
public int getX(){
return x;
}
public int getSize (){
return size;
}
public void increaseSpeed(){
if (speed < MAX_SPEED){
speed ++;
cx = (cx / Math.abs(cx) * speed) ;
if (cy < 0) {
cy = -1 * speed;
}
else {
cy = speed;
}
}
}
}