Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed wave_player
Fork of ECE2036Lab2StarterCode by
ball.cpp
00001 #include "mbed.h" 00002 #include "ball.h" 00003 #include "Speaker.h" 00004 #include "uLCD_4DGL.h" 00005 //#include "tempModule.h" 00006 00007 Speaker mySpeaker(p26);//Must be PWM pin or runtime error will occur 00008 00009 /**** Constructors ****/ 00010 00011 Ball::Ball() { 00012 setXSign(-1); setYSign(0); 00013 setFx(0); setFy(0); 00014 setVx(1.6); setVy(1.2); 00015 setX(0); setY(0); 00016 setRadius(5); 00017 } 00018 00019 Ball::Ball(PinName pin) 00020 { 00021 setXSign(-1); setYSign(0); 00022 setFx(0.0); setFy(0.0); 00023 setVx(1.6); setVy(1.2); 00024 setX(0); setY(0); 00025 setRadius(5); 00026 tempSensor = new TempModule(pin); 00027 } 00028 00029 Ball::Ball(PinName pin, float vx, float vy) 00030 { 00031 setXSign(-1); setYSign(0); 00032 setFx(0.0); setFy(0.0); 00033 setVx(vx); setVy(vy); 00034 setX(0); setY(0); 00035 setRadius(5); 00036 tempSensor = new TempModule(pin); 00037 } 00038 00039 /**** Set Functions ****/ 00040 00041 // This sets the lowest velocity, for Thermal pong, or the constant velocity of x 00042 void Ball::setVx(float set_vx) {vx = set_vx; } 00043 // This sets the lowest velocity, for Thermal pong, or the constant velocity of y 00044 void Ball::setVy(float set_vy) {vy = set_vy; } 00045 void Ball::setXSign(int set_xSign) {xSign = set_xSign; } 00046 void Ball::setYSign(int set_ySign) {ySign = set_ySign; } 00047 void Ball::setRadius(int set_radius) {radius = set_radius; } 00048 void Ball::setFx(float set_fx) {fx = set_fx; } 00049 void Ball::setFy(float set_fy) {fy = set_fy; } 00050 void Ball::setX(int set_x) {x = set_x; } 00051 void Ball::setY(int set_y) {y = set_y; } 00052 void Ball::setLose(bool set_lose) {lose = set_lose; } 00053 00054 /**** Get Functions ****/ 00055 00056 int Ball::getFutureX() { 00057 // calculate new X position 00058 int xTemp; 00059 xTemp = getFx() + ( getXSign() * getVx() ); 00060 return xTemp; 00061 } 00062 00063 int Ball::getFutureY() { 00064 // calculate new Y position 00065 int yTemp; 00066 yTemp = getFy() + ( getYSign() * getVy() ); 00067 return yTemp; 00068 } 00069 00070 int Ball::getRadius() {return radius;} 00071 int Ball::getXSign() {return xSign;} 00072 int Ball::getYSign() {return ySign;} 00073 int Ball::getX() {return x;} 00074 int Ball::getY() {return y;} 00075 float Ball::getFx() {return fx;} 00076 float Ball::getFy() {return fy;} 00077 float Ball::getVx() {return vx;} 00078 float Ball::getVy() {return vy;} 00079 bool Ball::getLose() {return lose;} 00080 00081 /**** Member Functions ****/ 00082 00083 void Ball::reverseXDirection() { 00084 // negate the sign when ball hits wall or paddle 00085 int xTemp; 00086 xTemp = getXSign(); 00087 setXSign(-xTemp); 00088 } 00089 00090 void Ball::reverseYDirection() { 00091 // negate the sign when ball hits wall or paddle 00092 int yTemp; 00093 yTemp = getYSign(); 00094 setYSign(-yTemp); 00095 } 00096 00097 void Ball::startPong(int count, uLCD_4DGL *my_uLCD) { 00098 // initialize starting pointion for the ball 00099 int rnd = 0; 00100 srand(count); 00101 rnd = (rand() % (118 - 2 * getRadius())) + getRadius(); 00102 setFx( rnd ); setX( rnd ); 00103 rnd = (rand() % (127 - 2 * getRadius())) + getRadius(); 00104 setFy( rnd ); setY( rnd ); 00105 rnd = rand() % 2; 00106 setYSign( ((float)rnd - 0.5) * 2 ); 00107 } 00108 00109 void Ball::update(uLCD_4DGL *update_uLCD) { 00110 // moves the ball on the screen 00111 update_uLCD->filled_circle(getX(), getY(), getRadius(), BLACK); 00112 setFx(getFx()+(getXSign() * (1.5*getVx()))); 00113 setFy(getFy()+(getYSign() * (1.5*getVy()))); 00114 setX(getFutureX()); 00115 setY(getFutureY()); 00116 update_uLCD->filled_circle(getX(), getY(), getRadius(), WHITE); 00117 00118 } 00119 00120 void Ball::resetBall() { 00121 // resets ball location for new game 00122 setVx(1.6); setVy(1.2); 00123 } 00124 00125 void Ball::testConditions( Paddle *my_paddle, uLCD_4DGL *my_uLCD ) { 00126 if ((getFx()+getXSign() * getVx() <= getRadius()+3)) 00127 { 00128 mySpeaker.PlayNote(300.0, 0.05, 0.1); 00129 reverseXDirection(); 00130 } 00131 if ((getFy()+getYSign() * getVy() <= getRadius()+3) || (getFy()+getYSign() * getVy() >= 125-getRadius())) 00132 { 00133 mySpeaker.PlayNote(300.0, 0.05, 0.1); 00134 reverseYDirection(); 00135 } 00136 if (((getFx()+getXSign() * getVx() >= my_paddle->getX()) && (getFx()+getXSign() * getVx() <= my_paddle->getX()+3)) && 00137 ((getFy()+getYSign() * getVy() >= my_paddle->getY()) && (getFy()+getYSign() * getVy() <= my_paddle->getY()+my_paddle->getLength()))) 00138 { 00139 mySpeaker.PlayNote(300.0, 0.05, 0.1); 00140 reverseYDirection(); 00141 } 00142 if ((getFx()+getXSign() * getVx() >= 123-getRadius())) 00143 { 00144 mySpeaker.PlayNote(300.0, 0.7, 0.3); 00145 setVx(0); 00146 setVy(0); 00147 setLose(1); 00148 } 00149 if ((getFx()+getXSign() * getVx() >= my_paddle->getX()-(getRadius()+2.5)) && (getFy()+getYSign() * getVy() <= my_paddle->getY()+my_paddle->getLength()) 00150 && (getFy()+getYSign() * getVy() >= my_paddle->getY())) 00151 { 00152 mySpeaker.PlayNote(400.0, 0.05, 0.1); 00153 reverseXDirection(); 00154 my_paddle->setScore(1); 00155 00156 /* my_uLCD->locate(1,1); 00157 my_uLCD->printf("%d", my_paddle->getScore()); 00158 */ 00159 } 00160 } 00161
Generated on Tue Jul 19 2022 14:32:52 by
1.7.2
