-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbouncing_ball.pde
93 lines (79 loc) · 1.07 KB
/
bouncing_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
float xpos, ypos , xSpeed, ySpeed, xAcel, yAcel;
void setup ()
{
size (640,480);
xpos = 0;
ypos = 0;
xSpeed = 5;
ySpeed = 5;
xAcel = 0;
yAcel = 0;
}
void draw ()
{
background (255);
xSpeed = xSpeed * 0.99;
ySpeed = ySpeed * 0.99;
xAcel = xAcel * 0.99;
yAcel = yAcel * 0.99;
if (xSpeed > 5)
{
xSpeed = 5;
}
if (xSpeed < -5)
{
xSpeed = -5;
}
if (ySpeed > 5)
{
ySpeed = 5;
}
if (ySpeed < -5)
{
ySpeed = -5;
}
if (xpos > width)
{
xSpeed *= -1;
}
if (ypos > height)
{
ySpeed *= -1;
}
if (xpos < 0)
{
xSpeed *= -1;
}
if (ypos < 0)
{
ySpeed *= -1;
}
xpos+= xSpeed;
xSpeed+= xAcel;
ypos+= ySpeed;
ySpeed+= yAcel;
ellipse(xpos, ypos, 50, 50);
}
void mousePressed ()
{
fill(random(255), random(255), random(255), 100);
}
void keyPressed ()
{
if (keyCode == 'A')
{
xAcel -= 0.01;
}
else if (keyCode == 'D')
{
xAcel += 0.01;
}
else if (keyCode == 'W')
{
yAcel -= 0.01;
}
else if (keyCode == 'S')
{
yAcel += 0.01;
}
}