-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.pde
79 lines (67 loc) · 1.65 KB
/
Ball.pde
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
import java.util.ArrayList;
class Ball {
private final int TRAIL_SIZE = 10;
private float x;
private float y;
private float speed;
private float d;
private float r;
private ArrayList<Float> oldXs;
private float h = 0;
private float s = 50;
private Obstacle obstacle;
public Ball(float d, float speed, float x) {
this.x = x;
this.y = height / 2.0;
this.d = d;
this.r = d / 2;
this.speed = speed;
oldXs = new ArrayList<Float>();
obstacle = new Obstacle();
}
public Ball() {
this(14, 10, 0);
}
public void draw(Player player) {
obstacle.draw();
x += speed;
if (speed < 0 && player.checkCollision(x-r)) {
println("bounce");
speed = speed * -1;
speed = _score < 10 ? speed * 1.1 : speed * 1.02;
_score++;
sendOscMessage("hit", _score);
} else if (x < 0) {
println("game over");
changeState(State.GAME_OVER);
} else if (x+r > width) {
speed = -abs(speed);
println("change direction");
}
if (obstacle.checkCollision(x, r)) {
speed = speed * -1;
obstacle.explode();
}
oldXs.add(x);
if (oldXs.size() > TRAIL_SIZE) {
oldXs.remove(0);
}
h = (h + 1) % 100;
s = 50 + (s + 1) % 50;
int i = 0;
noStroke();
for (Float oldX : oldXs) {
if (i==TRAIL_SIZE-1) {
fill(0, 0, 100);
ellipse(oldX+random(-2, 2), y, d, d);
} else if (i > TRAIL_SIZE - 3) {
fill(h, s, 100);
ellipse(oldX+random(-2, 2), y, speed*2, speed*2);
} else {
fill(h, s, 6 * i);
ellipse(oldX+random(-2, 2), y, speed*2, speed*2);
}
i++;
}
}
}