-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClass18_yc3359.pde
61 lines (51 loc) · 1001 Bytes
/
Class18_yc3359.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
float x, y, speedX, speedY;
float diam = 10;
float rectSize = 200;
void setup() {
fullScreen();
fill(0, 255, 0);
reset();
}
void reset() {
x = width/2;
y = height/2;
speedX = random(3, 5);
speedY = random(3, 5);
}
void draw() {
background(255);
displayBall();
displayBar();
moveBall();
checkEdges();
}
void displayBall() {
ellipse(x, y, diam, diam);
}
void displayBar() {
rect(0, 0, 20, height);
rect(width-30, mouseY-rectSize/2, 10, rectSize);
}
void moveBall() {
x += speedX;
y += speedY;
}
void checkEdges() {
// if ball hits movable bar, invert X direction
if ( x > width-30 && x < width -20 && y > mouseY-rectSize/2 && y < mouseY+rectSize/2 ) {
speedX = speedX * -1;
}
// if ball hits wall, change direction of X
if (x < 25) {
speedX *= -1.1;
speedY *= 1.1;
x += speedX;
}
// if ball hits up or down, change direction of Y
if ( y > height || y < 0 ) {
speedY *= -1;
}
}
void mousePressed() {
reset();
}