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.
main.cpp
00001 #include "mbed.h" 00002 #include "MobileLCD.h" 00003 MobileLCD lcd(p5, p6, p7, p8, p9); 00004 Serial pc(USBTX, USBRX); // tx, rx 00005 00006 // ******************************************************************************** 00007 // * Startup variables 00008 // ******************************************************************************** 00009 //Ball speed 00010 int speedh; 00011 int speedv; 00012 00013 //Ball position 00014 int ballx = 5; 00015 int bally = 65; 00016 00017 //Score 00018 int scorel; 00019 int scorer; 00020 00021 //Paddle size 00022 const int paddle = 30; 00023 00024 //Win level 00025 const int winner = 3; 00026 00027 //Paddle position 00028 int lefty = 53; 00029 int righty = 50; 00030 00031 //Joystick position & input variables 00032 AnalogIn inl(p20); 00033 AnalogIn inr(p19); 00034 float joystickl; 00035 float joystickr; 00036 00037 // Number of players 00038 int players = 0; 00039 00040 // Loop until winner declared 00041 int playing = 1; 00042 00043 const int ballcolour = 0xFFFFFF; 00044 00045 // ******************************************************************************** 00046 00047 // ******************************************************************************** 00048 // * Display score, wait & wipe 00049 // ******************************************************************************** 00050 void displayScore( int left, int right ) { 00051 lcd.locate ( 3, 2 ) ; 00052 lcd.printf( "%i %i ",left,right ); 00053 //Check if we have a winner 00054 if ( left == winner) { 00055 lcd.locate( 3,4 ); 00056 lcd.printf( "Left won!" ); 00057 playing = 0; 00058 lcd.fill(0, 0, 5, 130, 0x0000FF); //left 00059 lcd.fill(126, 0, 4, 130, 0x000FFF); //right 00060 wait(2); 00061 } 00062 if ( right == winner) { 00063 lcd.locate( 3,4 ); 00064 lcd.printf( "Right won!" ); 00065 playing = 0; 00066 lcd.fill(0, 0, 5, 130, 0x0000FF); //left 00067 lcd.fill(126, 0, 4, 130, 0x000FFF); //right 00068 wait(2); 00069 } 00070 wait( 2 ); 00071 lcd.fill(13, 0, 110, 25, 0x0000FF); 00072 } 00073 // ******************************************************************************** 00074 00075 00076 // ******************************************************************************** 00077 // * Setup screen to start 00078 // ******************************************************************************** 00079 void startup( void ) { 00080 //speed 00081 speedh = 1; 00082 speedv = -1; 00083 00084 //Score 00085 scorel = 0; 00086 scorer = 0; 00087 00088 // *** Screen *** 00089 lcd.background(0x0000FF); 00090 lcd.cls(); 00091 lcd.fill(1, lefty, 4, paddle, 0x00FF00); //left 00092 lcd.fill(126, righty, 4, paddle, 0x00FF00); //right 00093 lcd.fill(ballx, bally, 4, 4, ballcolour); //ball 00094 displayScore(scorel,scorer); 00095 00096 00097 // *** Calibrate analog in *** 00098 // * Start game at higheest voltage level for both inpout pins 00099 00100 } 00101 // ******************************************************************************** 00102 00103 00104 // ******************************************************************************** 00105 // Move the Ball 00106 // ******************************************************************************** 00107 void moveBall ( void ) { 00108 00109 int retVal =0; 00110 00111 //Remove ball from screen 00112 lcd.fill( ballx,bally,4,4,0x0000FF ); 00113 00114 // Normal x movement 00115 if ( (ballx + speedh > 4) && (ballx + speedh < 122)) { 00116 ballx = ballx + speedh; 00117 00118 } else { 00119 //Bounce off right paddle 00120 if (ballx+speedh >= 122) { 00121 if ( (bally>righty)&&(bally<righty+paddle) ) { 00122 ballx=122; 00123 speedh=speedh*(-1); 00124 ballx=ballx+speedh; 00125 } else { 00126 //Left scores 00127 scorel = scorel + 1; 00128 displayScore( scorel,scorer ); 00129 //Ball speed 00130 speedh = -1; 00131 speedv = +1; 00132 00133 //Ball position 00134 ballx = 122; 00135 bally = righty+(paddle/2); 00136 } 00137 } 00138 //Bounce off left paddle 00139 if (ballx+speedh <=4) { 00140 00141 if ( (bally>lefty)&&(bally<lefty+paddle) ) { 00142 ballx=4; 00143 speedh=speedh*(-1); 00144 ballx=ballx+speedh; 00145 } else { //Right scores 00146 scorer = scorer + 1; 00147 displayScore( scorel,scorer ); 00148 //Ball speed 00149 speedh = 1; 00150 speedv = -1; 00151 00152 //Ball position 00153 ballx = 5; 00154 bally = lefty+(paddle/2); 00155 } 00156 } 00157 } //else 00158 00159 00160 // Normal y movement 00161 if ( (bally + speedv > 0) && (bally + speedv < 126)) { 00162 bally = bally + speedv; 00163 00164 } else { 00165 // Bounce bottom 00166 if ( bally + speedv >= 126 ) { 00167 bally = 125; 00168 speedv = speedv*(-1); 00169 bally = bally + speedv; 00170 } 00171 // Bounce top 00172 if (bally + speedv <= 0 ) { 00173 bally = 0; 00174 speedv = speedv*(-1); 00175 bally = bally + speedv; 00176 00177 } 00178 } // else 00179 00180 // Draw ball if it is to be moved 00181 if ( retVal == 0 ) { 00182 lcd.fill(ballx, bally, 4, 4, ballcolour); //ball 00183 } 00184 00185 } 00186 00187 // ******************************************************************************** 00188 // Move the Paddles 00189 // ******************************************************************************** 00190 00191 void movePaddles( void ) { 00192 00193 //*** Left *** 00194 00195 lcd.fill(1, lefty, 4, paddle, 0x0000FF); //Clear paddle 00196 //Read paddle if 2 player 00197 if ( players == 2 ) { 00198 joystickl = inl.read(); 00199 joystickl = joystickl * 100; 00200 joystickl = 53 - joystickl; 00201 lefty = joystickl*(130/47); 00202 } else { 00203 if ( lefty>bally ) { 00204 lefty--; 00205 } 00206 if ( lefty<bally ) { 00207 lefty++; 00208 } 00209 } 00210 //Auto move paddle if 1 player 00211 00212 00213 if ( lefty <0 ) { 00214 lefty = 0; 00215 } 00216 if ( lefty > (130-paddle) ) { 00217 lefty=130-paddle; 00218 } 00219 00220 lcd.fill(1, lefty, 4, paddle, 0x00FF00); //Draw paddle 00221 00222 //*** Right *** 00223 lcd.fill(126, righty, 4, paddle, 0x0000FF); //Clear paddle 00224 joystickr = inr.read(); 00225 joystickr = joystickr * 100; 00226 joystickr = 53 - joystickr; 00227 righty = joystickr*(130/47); 00228 if ( righty <0 ) { 00229 righty = 0; 00230 } 00231 if ( righty > (130-paddle) ) { 00232 righty=130-paddle; 00233 } 00234 lcd.fill(126, righty, 4, paddle, 0x00FF00); //Draw paddle 00235 } 00236 // ******************************************************************************** 00237 00238 // ******************************************************************************** 00239 // Main menu 00240 // ******************************************************************************** 00241 void menu( void ) { 00242 //Read right joystick level, change will start game 00243 joystickr = inr.read(); 00244 joystickr = joystickr * 100; 00245 joystickr = 53 - joystickr; 00246 float startr = joystickr*(130/47); 00247 00248 //Display menu options, control with left joystick 00249 lcd.background(0x0000FF); 00250 lcd.cls(); 00251 lcd.locate( 5,3 ); 00252 lcd.printf( "mPong" ); 00253 lcd.locate( 1,12 ); 00254 lcd.printf( "(left=choose;"); 00255 lcd.locate( 1,13 ); 00256 lcd.printf( " right=start)" ); 00257 int choose = 1; 00258 while ( choose==1 ) { 00259 00260 //*** Left *** 00261 joystickl = inl.read(); 00262 joystickl = joystickl * 100; 00263 joystickl = 53 - joystickl; 00264 lefty = joystickl*(130/47); 00265 00266 if ( lefty < 44 ) { 00267 lcd.background( 0x550000 ); 00268 lcd.locate (1,6); 00269 lcd.printf( "Single Player"); 00270 lcd.background( 0x0000FF ); 00271 lcd.locate (1,7); 00272 lcd.printf( "Double Player"); 00273 players = 1; 00274 } else { 00275 lcd.background( 0x0000FF ); 00276 lcd.locate (1,6); 00277 lcd.printf( "Single Player"); 00278 lcd.background( 0x550000 ); 00279 lcd.locate (1,7); 00280 lcd.printf( "Double Player"); 00281 players = 2; 00282 } 00283 wait(0.1); 00284 00285 //Read right joystick level, change will start game 00286 joystickr = inr.read(); 00287 joystickr = joystickr * 100; 00288 joystickr = 53 - joystickr; 00289 joystickr = joystickr*(130/47); 00290 00291 if ( (startr - joystickr) > 10 || (startr - joystickr ) < -10 ) { 00292 // Start game 00293 choose = 0; 00294 //Nice screen clear 00295 for ( choose=0; choose <65; choose++ ) { 00296 lcd.fill(65-choose, 0, 1, 130, 0x0000FF); 00297 lcd.fill(65+choose, 0, 1, 130, 0x0000FF); 00298 wait(0.02); 00299 playing=1; 00300 } 00301 } 00302 00303 } 00304 } 00305 // ******************************************************************************** 00306 int main() { 00307 while ( 1 ) { 00308 00309 menu(); 00310 startup(); 00311 00312 // Main Loop 00313 while (playing == 1) { 00314 moveBall(); 00315 movePaddles(); 00316 wait(0.02); 00317 } // while playing 00318 00319 } // while 1 00320 }
Generated on Sun Aug 7 2022 09:32:03 by
1.7.2