-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBall.pde
56 lines (48 loc) · 1.29 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
class Ball {
float x;
float y;
float w;
float h;
float speedX;
float speedY;
Ball () {
x = width/2;
y = height/2;
w = 10;
h = 10;
speedX = 2*level;
speedY = level;
}
void display() {
ellipseMode(CENTER);
noStroke();
fill(255);
rect(x, y, w, h);
}
void move(RightPaddle pR, LeftPaddle pL) {
x += speedX;
y += speedY;
if (x+w >= pR.x && x <= pR.x + pR.w && y+w >= pR.y && y <= pR.y + pR.h) { // Ball hits right paddle
paddle.play();
speedX *= -1;
}
if (x >= pL.x && x < pL.x+pL.w && y+h >= pL.y && y <= pL.y + pL.h) { // Ball hits left paddle
paddle.play();
speedX *= -1;
}
if (y <= 0 || y+h >= height) { // Ball hits top or bottom of the screen
sides.play();
speedY *= -1;
}
if (x > width) { // If ball leaves screen from the right side
point.play();
x = width/2; // Ball is returned to the middle of the width of the screen
score.scoreL ++; // Left score increases by one
}
if (x < 0) { // If ball leaves screen from the left side
point.play();
x = width/2; // Ball is returned to the middle of the width of the screen
score.scoreR ++; // Right score increases by one
}
}
}