-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.cpp
94 lines (85 loc) · 1.79 KB
/
ball.cpp
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
94
#include "ball.h"
#include "ofMain.h"
Ball::Ball() {
}
Ball::Ball(float x, float y, int r, float xV, float yV, int score, int bleft, int lives, ofColor color) {
this->x = x;
this->y = y;
this->r = r;
this->xV = xV;
this->yV = yV;
this->score = score;
this->bleft = bleft;
this->lives = lives;
this->color = color;
}
void Ball::drawBall() {
ofSetColor(color);
ofDrawCircle(x, y, 2*r);
ofSetCircleResolution(100);
}
//resets ball with diffrent velocity and position
void Ball::resetBall() {
x = ofGetWidth() / 2;
y = ofGetHeight() / 2;
xV = ofRandom(0, 2);
yV = ofRandom(0, 2);
}
void Ball::moveBall() {
x += xV;
y += yV;
//if the ball goes beyond the player paddle reset ball
//otherwise if the ball goes beyond comp paddle gain 100 points and reset ball
if (x <= r) {
x = r;
xV *= -1;
--lives;
//if the score is more than 0 then lose a point otherwise keep at 0
if (score == -1) {
score = 0;
}
else if (score > 0) {
score -= 1;
}
resetBall();
}
else if (x >= ofGetWindowWidth() - r) {
score += 100;
resetBall();
}
//bounderies limit the ball can go to from top to bottom
if (y <= (150 + r)) {
y = (150 + r);
yV *= -1;
}
else if (y >= ofGetWindowHeight() - r) {
y = ofGetWindowHeight() - r;
yV *= -1;
}
}
//ball bounces off the player paddle and increases score by 1/increases velocity by .5
void Ball::bounceOff(float px, float py, float pw, float ph) {
if ((x - r) <= (px + pw)) {
if (y < (py + ph)) {
if (y > (py)) {
xV *= -1;
xV += .5;
++score;
}
}
}
}
//ball bounces off the computer paddle
void Ball::bounceOffc(float cx, float cy, float cw, float ch) {
if ((x + r) >= (cx)) {
if (y < (cy + ch)) {
if (y > (cy)) {
xV *= -1;
}
}
}
}
//moves the bullet with a speed of 5
void Ball::projectileMove() {
x += 5;
}