David Buck / Mbed 2 deprecated Pong

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MobileLCD.h"
00003 
00004 MobileLCD lcd (5,6,7,12,13);
00005 AnalogIn left (20);
00006 AnalogIn right (19);
00007 
00008 int r;
00009 int le;
00010 
00011 int ball_x = 64;
00012 int ball_y = 64;
00013 int ball_tempX = 64;
00014 int ball_tempY = 64;
00015 int dir = 1 ; 
00016 
00017 
00018 void moveBall(void);
00019 
00020 int main() {
00021 
00022 lcd.background(0x000000);
00023     while(1) {    
00024 lcd.cls();
00025 
00026 le = left  * 100 + 8;
00027 r = right * 100 + 8;
00028 
00029 //center line
00030 lcd.fill(65,0,1,130,0xffffff);
00031 
00032 //left paddle
00033 lcd.fill(4,le,4,16,0xffffff);
00034 
00035 //right paddle
00036 lcd.fill(120,r,4,16,0xffffff);
00037 
00038 //ball
00039 moveBall();
00040 lcd.locate(3,5);
00041 lcd.printf("%.0f",dir);
00042 lcd.locate(3,7);
00043 lcd.printf("%.0f",le);
00044     //wait(0.3);
00045     }
00046 }
00047 
00048 void moveBall(){
00049 
00050     ball_tempX = ball_x;
00051     ball_tempY = ball_y;
00052 
00053     if (dir == 1 && ball_x > 5 && ball_y > 5){
00054      
00055          if( ball_x == 5 && ball_y >= le && ball_y <= le + 8){
00056                   dir = rand() % 2 + 3;
00057          }else{    
00058                  ball_x--;
00059                  ball_y--;
00060          }    
00061               
00062     } else if (dir == 2 && ball_x > 30 && ball_y < 5){
00063 
00064          if( ball_x == 6 && ball_y >= le && ball_y <= le + 8){
00065                   dir = rand() % 2 + 3;
00066          }else{    
00067                  ball_x--;
00068                  ball_y++;
00069          }
00070 
00071     } else if (dir == 3 && ball_x < 130 && ball_y > 5){
00072 
00073          if( ball_x + 5 == 120 && ball_y >= r && ball_y <= r + 20){
00074                   dir = rand() % 1 + 2;
00075          }else{    
00076                  ball_x++;
00077                  ball_y--;
00078          }
00079 
00080     } else if (dir == 4 && ball_x < 130 && ball_y < 475){
00081 
00082          if( ball_x + 5 == 120 && ball_y >= r && ball_y <= r + 20){
00083                   dir = rand() % 1 + 2;
00084          }else{    
00085                  ball_x++;
00086                  ball_y++;
00087          }
00088 
00089     } else { 
00090 
00091         if (dir == 1 || dir == 3)   dir++;
00092         else if (dir == 2 || dir == 4)    dir--;
00093 
00094     } 
00095    
00096 lcd.fill (ball_x,ball_y,4,4,0xffffff);
00097 }