-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_230406a_heeyun.pde
83 lines (72 loc) · 1.43 KB
/
sketch_230406a_heeyun.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
80
81
82
83
import processing.sound.*;
SoundFile hitSound;
int circleCount = 5;
Circle[] circles = new Circle[circleCount];
void setup() {
size(500, 500);
hitSound = new SoundFile(this, "data/boing.wav");
for (int i = 0; i < circleCount; i++) {
circles[i] = new Circle(random(width), random(height), random(-5, 5), random(-5, 5), 20);
}
}
void draw() {
background(255);
for (int i = 0; i < circleCount; i++) {
circles[i].move();
circles[i].checkBoundaryCollision();
circles[i].checkWallCollision();
circles[i].display();
}
}
class Circle {
float x;
float y;
float dx;
float dy;
float r;
Circle(float x, float y, float dx, float dy, float r) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.r = r;
}
void move() {
x += dx;
y += dy;
}
void checkBoundaryCollision() {
if (x < r) {
x = r;
dx *= -1;
hitSound.play();
} else if (x > width - r) {
x = width - r;
dx *= -1;
hitSound.play();
}
if (y < r) {
y = r;
dy *= -1;
hitSound.play();
} else if (y > height - r) {
y = height - r;
dy *= -1;
hitSound.play();
}
}
void checkWallCollision() {
if (x < r || x > width - r) {
dx *= -1;
hitSound.play();
}
if (y < r || y > height - r) {
dy *= -1;
hitSound.play();
}
}
void display() {
fill(0, 0, 255);
ellipse(x, y, r*2, r*2);
}
}