-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbouncingBall_changingSpeed.pde
More file actions
46 lines (36 loc) · 992 Bytes
/
Copy pathbouncingBall_changingSpeed.pde
File metadata and controls
46 lines (36 loc) · 992 Bytes
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
// Bouncing ball changing speed
// The Coding Train / Daniel Shiffman
// Processing Intro Series
float x, y, xspeed, yspeed, radius;
int maxSpeed = 10;
void setup() {
size(640, 360);
pixelDensity(2);
xspeed = 5;
yspeed = 5;
radius = 25;
//Start at a random location!
x = random(radius, width-radius);
y = random(radius, height-radius);
}
void draw() {
background(0);
noStroke();
if (x < radius || x > width - radius) {
xspeed = constrain(random(xspeed, xspeed*2), -maxSpeed, maxSpeed) * -1;
//abs() returns the absolute value
if (abs(xspeed) == maxSpeed) {
xspeed = xspeed/5; //reset
}
}
if (y < radius || y > height - radius) {
yspeed = constrain(random(yspeed, yspeed*2), -maxSpeed, maxSpeed) * -1;
//abs() returns the absolute value
if (abs(yspeed) == maxSpeed) {
yspeed = yspeed/5; //reset
}
}
circle(x, y, radius*2);
x = x + xspeed;
y = y + yspeed;
}