-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.pde
87 lines (75 loc) · 1.47 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
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
class Ball{
float maxSpeed = 9;
PVector location;
PVector velocity; //Changes Location
PVector acceleration; //Changes Velocity
boolean moving;
Ball(){
location = new PVector(width/2,height/2);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
moving = false;
}
float getX(){
return location.x;
}
float getY(){
return location.y;
}
boolean getMoving(){
return moving;
}
void drawBall(){
noStroke();
fill(255,0,0);
if(moving == false)
{
location.x = paddle.x + PADDLEWIDTH / 2;
location.y = PADDLEY - BALL_RADIUS;
}
ellipse(location.x,location.y,BALL_RADIUS * 2,BALL_RADIUS * 2);
}
void ballStart()
{
velocity.y = random(3,5);
acceleration.x = ACCELERATION;
acceleration.y = ACCELERATION;
moving = true;
}
void move(){
velocity.add(acceleration);
location.add(velocity);
velocity.limit(maxSpeed);
}
void outOfBounds(){
if(location.x > width || location.x < 0)
{
flipX();
}
if(location.y < 0 + BALL_RADIUS)
{
flipY();
}
if(location.y > (PADDLEY + PADDLEHEIGHT/2 + 10))
{
ballReset();
lives--;
}
}
void ballReset()
{
velocity.setMag(0);
acceleration.setMag(0);
location.x = width/2;
location.y = height/2;
moving = false;
}
void flipY(){
velocity.y *= -1;
acceleration.y *= -1;
}
void flipX(){
velocity.x *= -1;
acceleration.x *= -1;
}
}