![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
One player pong with seven segment display for score keeping
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed wave_player
Fork of ECE2036Lab2StarterCode by
ball.cpp@3:c93d1b51785c, 2016-03-17 (annotated)
- Committer:
- dcleary
- Date:
- Thu Mar 17 20:38:26 2016 +0000
- Revision:
- 3:c93d1b51785c
Pong
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dcleary | 3:c93d1b51785c | 1 | #include "mbed.h" |
dcleary | 3:c93d1b51785c | 2 | #include "ball.h" |
dcleary | 3:c93d1b51785c | 3 | #include "Speaker.h" |
dcleary | 3:c93d1b51785c | 4 | #include "uLCD_4DGL.h" |
dcleary | 3:c93d1b51785c | 5 | //#include "tempModule.h" |
dcleary | 3:c93d1b51785c | 6 | |
dcleary | 3:c93d1b51785c | 7 | Speaker mySpeaker(p26);//Must be PWM pin or runtime error will occur |
dcleary | 3:c93d1b51785c | 8 | |
dcleary | 3:c93d1b51785c | 9 | /**** Constructors ****/ |
dcleary | 3:c93d1b51785c | 10 | |
dcleary | 3:c93d1b51785c | 11 | Ball::Ball() { |
dcleary | 3:c93d1b51785c | 12 | setXSign(-1); setYSign(0); |
dcleary | 3:c93d1b51785c | 13 | setFx(0); setFy(0); |
dcleary | 3:c93d1b51785c | 14 | setVx(1.6); setVy(1.2); |
dcleary | 3:c93d1b51785c | 15 | setX(0); setY(0); |
dcleary | 3:c93d1b51785c | 16 | setRadius(5); |
dcleary | 3:c93d1b51785c | 17 | } |
dcleary | 3:c93d1b51785c | 18 | |
dcleary | 3:c93d1b51785c | 19 | Ball::Ball(PinName pin) |
dcleary | 3:c93d1b51785c | 20 | { |
dcleary | 3:c93d1b51785c | 21 | setXSign(-1); setYSign(0); |
dcleary | 3:c93d1b51785c | 22 | setFx(0.0); setFy(0.0); |
dcleary | 3:c93d1b51785c | 23 | setVx(1.6); setVy(1.2); |
dcleary | 3:c93d1b51785c | 24 | setX(0); setY(0); |
dcleary | 3:c93d1b51785c | 25 | setRadius(5); |
dcleary | 3:c93d1b51785c | 26 | tempSensor = new TempModule(pin); |
dcleary | 3:c93d1b51785c | 27 | } |
dcleary | 3:c93d1b51785c | 28 | |
dcleary | 3:c93d1b51785c | 29 | Ball::Ball(PinName pin, float vx, float vy) |
dcleary | 3:c93d1b51785c | 30 | { |
dcleary | 3:c93d1b51785c | 31 | setXSign(-1); setYSign(0); |
dcleary | 3:c93d1b51785c | 32 | setFx(0.0); setFy(0.0); |
dcleary | 3:c93d1b51785c | 33 | setVx(vx); setVy(vy); |
dcleary | 3:c93d1b51785c | 34 | setX(0); setY(0); |
dcleary | 3:c93d1b51785c | 35 | setRadius(5); |
dcleary | 3:c93d1b51785c | 36 | tempSensor = new TempModule(pin); |
dcleary | 3:c93d1b51785c | 37 | } |
dcleary | 3:c93d1b51785c | 38 | |
dcleary | 3:c93d1b51785c | 39 | /**** Set Functions ****/ |
dcleary | 3:c93d1b51785c | 40 | |
dcleary | 3:c93d1b51785c | 41 | // This sets the lowest velocity, for Thermal pong, or the constant velocity of x |
dcleary | 3:c93d1b51785c | 42 | void Ball::setVx(float set_vx) {vx = set_vx; } |
dcleary | 3:c93d1b51785c | 43 | // This sets the lowest velocity, for Thermal pong, or the constant velocity of y |
dcleary | 3:c93d1b51785c | 44 | void Ball::setVy(float set_vy) {vy = set_vy; } |
dcleary | 3:c93d1b51785c | 45 | void Ball::setXSign(int set_xSign) {xSign = set_xSign; } |
dcleary | 3:c93d1b51785c | 46 | void Ball::setYSign(int set_ySign) {ySign = set_ySign; } |
dcleary | 3:c93d1b51785c | 47 | void Ball::setRadius(int set_radius) {radius = set_radius; } |
dcleary | 3:c93d1b51785c | 48 | void Ball::setFx(float set_fx) {fx = set_fx; } |
dcleary | 3:c93d1b51785c | 49 | void Ball::setFy(float set_fy) {fy = set_fy; } |
dcleary | 3:c93d1b51785c | 50 | void Ball::setX(int set_x) {x = set_x; } |
dcleary | 3:c93d1b51785c | 51 | void Ball::setY(int set_y) {y = set_y; } |
dcleary | 3:c93d1b51785c | 52 | void Ball::setLose(bool set_lose) {lose = set_lose; } |
dcleary | 3:c93d1b51785c | 53 | |
dcleary | 3:c93d1b51785c | 54 | /**** Get Functions ****/ |
dcleary | 3:c93d1b51785c | 55 | |
dcleary | 3:c93d1b51785c | 56 | int Ball::getFutureX() { |
dcleary | 3:c93d1b51785c | 57 | // calculate new X position |
dcleary | 3:c93d1b51785c | 58 | int xTemp; |
dcleary | 3:c93d1b51785c | 59 | xTemp = getFx() + ( getXSign() * getVx() ); |
dcleary | 3:c93d1b51785c | 60 | return xTemp; |
dcleary | 3:c93d1b51785c | 61 | } |
dcleary | 3:c93d1b51785c | 62 | |
dcleary | 3:c93d1b51785c | 63 | int Ball::getFutureY() { |
dcleary | 3:c93d1b51785c | 64 | // calculate new Y position |
dcleary | 3:c93d1b51785c | 65 | int yTemp; |
dcleary | 3:c93d1b51785c | 66 | yTemp = getFy() + ( getYSign() * getVy() ); |
dcleary | 3:c93d1b51785c | 67 | return yTemp; |
dcleary | 3:c93d1b51785c | 68 | } |
dcleary | 3:c93d1b51785c | 69 | |
dcleary | 3:c93d1b51785c | 70 | int Ball::getRadius() {return radius;} |
dcleary | 3:c93d1b51785c | 71 | int Ball::getXSign() {return xSign;} |
dcleary | 3:c93d1b51785c | 72 | int Ball::getYSign() {return ySign;} |
dcleary | 3:c93d1b51785c | 73 | int Ball::getX() {return x;} |
dcleary | 3:c93d1b51785c | 74 | int Ball::getY() {return y;} |
dcleary | 3:c93d1b51785c | 75 | float Ball::getFx() {return fx;} |
dcleary | 3:c93d1b51785c | 76 | float Ball::getFy() {return fy;} |
dcleary | 3:c93d1b51785c | 77 | float Ball::getVx() {return vx;} |
dcleary | 3:c93d1b51785c | 78 | float Ball::getVy() {return vy;} |
dcleary | 3:c93d1b51785c | 79 | bool Ball::getLose() {return lose;} |
dcleary | 3:c93d1b51785c | 80 | |
dcleary | 3:c93d1b51785c | 81 | /**** Member Functions ****/ |
dcleary | 3:c93d1b51785c | 82 | |
dcleary | 3:c93d1b51785c | 83 | void Ball::reverseXDirection() { |
dcleary | 3:c93d1b51785c | 84 | // negate the sign when ball hits wall or paddle |
dcleary | 3:c93d1b51785c | 85 | int xTemp; |
dcleary | 3:c93d1b51785c | 86 | xTemp = getXSign(); |
dcleary | 3:c93d1b51785c | 87 | setXSign(-xTemp); |
dcleary | 3:c93d1b51785c | 88 | } |
dcleary | 3:c93d1b51785c | 89 | |
dcleary | 3:c93d1b51785c | 90 | void Ball::reverseYDirection() { |
dcleary | 3:c93d1b51785c | 91 | // negate the sign when ball hits wall or paddle |
dcleary | 3:c93d1b51785c | 92 | int yTemp; |
dcleary | 3:c93d1b51785c | 93 | yTemp = getYSign(); |
dcleary | 3:c93d1b51785c | 94 | setYSign(-yTemp); |
dcleary | 3:c93d1b51785c | 95 | } |
dcleary | 3:c93d1b51785c | 96 | |
dcleary | 3:c93d1b51785c | 97 | void Ball::startPong(int count, uLCD_4DGL *my_uLCD) { |
dcleary | 3:c93d1b51785c | 98 | // initialize starting pointion for the ball |
dcleary | 3:c93d1b51785c | 99 | int rnd = 0; |
dcleary | 3:c93d1b51785c | 100 | srand(count); |
dcleary | 3:c93d1b51785c | 101 | rnd = (rand() % (118 - 2 * getRadius())) + getRadius(); |
dcleary | 3:c93d1b51785c | 102 | setFx( rnd ); setX( rnd ); |
dcleary | 3:c93d1b51785c | 103 | rnd = (rand() % (127 - 2 * getRadius())) + getRadius(); |
dcleary | 3:c93d1b51785c | 104 | setFy( rnd ); setY( rnd ); |
dcleary | 3:c93d1b51785c | 105 | rnd = rand() % 2; |
dcleary | 3:c93d1b51785c | 106 | setYSign( ((float)rnd - 0.5) * 2 ); |
dcleary | 3:c93d1b51785c | 107 | } |
dcleary | 3:c93d1b51785c | 108 | |
dcleary | 3:c93d1b51785c | 109 | void Ball::update(uLCD_4DGL *update_uLCD) { |
dcleary | 3:c93d1b51785c | 110 | // moves the ball on the screen |
dcleary | 3:c93d1b51785c | 111 | update_uLCD->filled_circle(getX(), getY(), getRadius(), BLACK); |
dcleary | 3:c93d1b51785c | 112 | setFx(getFx()+(getXSign() * (1.5*getVx()))); |
dcleary | 3:c93d1b51785c | 113 | setFy(getFy()+(getYSign() * (1.5*getVy()))); |
dcleary | 3:c93d1b51785c | 114 | setX(getFutureX()); |
dcleary | 3:c93d1b51785c | 115 | setY(getFutureY()); |
dcleary | 3:c93d1b51785c | 116 | update_uLCD->filled_circle(getX(), getY(), getRadius(), WHITE); |
dcleary | 3:c93d1b51785c | 117 | |
dcleary | 3:c93d1b51785c | 118 | } |
dcleary | 3:c93d1b51785c | 119 | |
dcleary | 3:c93d1b51785c | 120 | void Ball::resetBall() { |
dcleary | 3:c93d1b51785c | 121 | // resets ball location for new game |
dcleary | 3:c93d1b51785c | 122 | setVx(1.6); setVy(1.2); |
dcleary | 3:c93d1b51785c | 123 | } |
dcleary | 3:c93d1b51785c | 124 | |
dcleary | 3:c93d1b51785c | 125 | void Ball::testConditions( Paddle *my_paddle, uLCD_4DGL *my_uLCD ) { |
dcleary | 3:c93d1b51785c | 126 | if ((getFx()+getXSign() * getVx() <= getRadius()+3)) |
dcleary | 3:c93d1b51785c | 127 | { |
dcleary | 3:c93d1b51785c | 128 | mySpeaker.PlayNote(300.0, 0.05, 0.1); |
dcleary | 3:c93d1b51785c | 129 | reverseXDirection(); |
dcleary | 3:c93d1b51785c | 130 | } |
dcleary | 3:c93d1b51785c | 131 | if ((getFy()+getYSign() * getVy() <= getRadius()+3) || (getFy()+getYSign() * getVy() >= 125-getRadius())) |
dcleary | 3:c93d1b51785c | 132 | { |
dcleary | 3:c93d1b51785c | 133 | mySpeaker.PlayNote(300.0, 0.05, 0.1); |
dcleary | 3:c93d1b51785c | 134 | reverseYDirection(); |
dcleary | 3:c93d1b51785c | 135 | } |
dcleary | 3:c93d1b51785c | 136 | if (((getFx()+getXSign() * getVx() >= my_paddle->getX()) && (getFx()+getXSign() * getVx() <= my_paddle->getX()+3)) && |
dcleary | 3:c93d1b51785c | 137 | ((getFy()+getYSign() * getVy() >= my_paddle->getY()) && (getFy()+getYSign() * getVy() <= my_paddle->getY()+my_paddle->getLength()))) |
dcleary | 3:c93d1b51785c | 138 | { |
dcleary | 3:c93d1b51785c | 139 | mySpeaker.PlayNote(300.0, 0.05, 0.1); |
dcleary | 3:c93d1b51785c | 140 | reverseYDirection(); |
dcleary | 3:c93d1b51785c | 141 | } |
dcleary | 3:c93d1b51785c | 142 | if ((getFx()+getXSign() * getVx() >= 123-getRadius())) |
dcleary | 3:c93d1b51785c | 143 | { |
dcleary | 3:c93d1b51785c | 144 | mySpeaker.PlayNote(300.0, 0.7, 0.3); |
dcleary | 3:c93d1b51785c | 145 | setVx(0); |
dcleary | 3:c93d1b51785c | 146 | setVy(0); |
dcleary | 3:c93d1b51785c | 147 | setLose(1); |
dcleary | 3:c93d1b51785c | 148 | } |
dcleary | 3:c93d1b51785c | 149 | if ((getFx()+getXSign() * getVx() >= my_paddle->getX()-(getRadius()+2.5)) && (getFy()+getYSign() * getVy() <= my_paddle->getY()+my_paddle->getLength()) |
dcleary | 3:c93d1b51785c | 150 | && (getFy()+getYSign() * getVy() >= my_paddle->getY())) |
dcleary | 3:c93d1b51785c | 151 | { |
dcleary | 3:c93d1b51785c | 152 | mySpeaker.PlayNote(400.0, 0.05, 0.1); |
dcleary | 3:c93d1b51785c | 153 | reverseXDirection(); |
dcleary | 3:c93d1b51785c | 154 | my_paddle->setScore(1); |
dcleary | 3:c93d1b51785c | 155 | |
dcleary | 3:c93d1b51785c | 156 | /* my_uLCD->locate(1,1); |
dcleary | 3:c93d1b51785c | 157 | my_uLCD->printf("%d", my_paddle->getScore()); |
dcleary | 3:c93d1b51785c | 158 | */ |
dcleary | 3:c93d1b51785c | 159 | } |
dcleary | 3:c93d1b51785c | 160 | } |
dcleary | 3:c93d1b51785c | 161 |