pong game
Dependencies: 4DGL-uLCD-SE mbed
Fork of uLCD144G2_demo by
main.cpp
00001 // uLCD-144-G2 demo program for uLCD-4GL LCD driver library 00002 // 00003 #include "mbed.h" 00004 #include "math.h" 00005 #include "uLCD_4DGL.h" 00006 #define PI 3.14159265358979323846 00007 00008 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; 00009 AnalogIn a1(p19); 00010 AnalogIn a2(p20); 00011 DigitalIn start(p18); 00012 00013 int bx = 64; 00014 int by = 64; 00015 int P1x1 = 0, P1x2 = 3, P1y1, P1y2; 00016 int P2x1 = 124, P2x2 = 127, P2y1, P2y2; 00017 float angle; 00018 int speed; 00019 int P1S = 0, P2S = 0; 00020 int hold, h1, h2; 00021 00022 00023 00024 int main() 00025 { 00026 00027 // // basic printf demo = 16 by 18 characters on screen 00028 // uLCD.printf("\nHello uLCD World\n"); //Default Green on black text 00029 // uLCD.printf("\n Starting Demo..."); 00030 // uLCD.text_width(4); //4X size text 00031 // uLCD.text_height(4); 00032 // uLCD.color(RED); 00033 // for (int i=5; i>=0; --i) { 00034 // uLCD.locate(1,2); 00035 // uLCD.printf("%2D",i); 00036 // wait(.5); 00037 // 00038 // } 00039 // uLCD.cls(); 00040 00041 00042 uLCD.text_width(1); 00043 uLCD.text_height(1); 00044 uLCD.baudrate(300000); 00045 00046 // Initialize random angle between 0 and 360 00047 srand (time(NULL)); 00048 do { 00049 angle = rand()%361; 00050 } while ((angle<20)||(angle>70&&angle<110)||(angle>160&&angle<200)||(angle>250&&angle<290)||(angle>340)); 00051 00052 ///////////////////////////////////////////////////// 00053 00054 while(1) { 00055 if (start==0) { 00056 hold=0; 00057 h1=0; 00058 h2=0; 00059 } 00060 00061 if (hold==1) 00062 speed=0; 00063 else 00064 speed=5; 00065 00066 // Set position of the paddles 00067 P1y1 = (a1*103)+8; 00068 P1y2 = P1y1 + 16; 00069 P2y1 = (a2*103+8); 00070 P2y2 = P2y1 + 16; 00071 00072 // Control motion of ball 00073 uLCD.filled_circle(bx, by , 2, 0); 00074 00075 if((bx>122 && (by>P2y1 && by<P2y2)) || (bx<5 && (by>P1y1 && by<P1y2))) 00076 angle = 180 - angle; 00077 else if(by>125 || by<10)// I think this is where our problem is 00078 angle = -angle; 00079 00080 //P1 Scores 00081 else if(bx>122) { 00082 P1S++; 00083 bx = 5; 00084 by = P1y1+8; 00085 hold=1; 00086 h1=1; 00087 angle = rand()%120+120+180; 00088 00089 //P2 Scores 00090 } else if(bx<5) { 00091 P2S++; 00092 bx = 122; 00093 by = P2y1+8; 00094 hold=1; 00095 h2=1; 00096 angle = rand()%120+120; 00097 } 00098 00099 if (hold==1&&h1==1) { 00100 bx = 6; 00101 by = P1y1+8; 00102 } else if (hold==1&&h2==1) { 00103 bx = 121; 00104 by = P2y1+8; 00105 } else { 00106 bx = bx + speed*cos(angle*2*PI/360); 00107 by = by - speed*sin(angle*2*PI/360); 00108 } 00109 00110 // Print paddles and ball 00111 uLCD.filled_circle(bx, by, 2, 0xffffff); 00112 uLCD.filled_rectangle(0, P1y1, 3, P1y2, 0xffffff); 00113 uLCD.filled_rectangle(124, P2y1, 127, P2y2, 0xffffff); 00114 uLCD.filled_rectangle(0, 9, 3, P1y1-1, 0); 00115 uLCD.filled_rectangle(0, P1y2+1, 3, 127, 0); 00116 uLCD.filled_rectangle(124, 9, 127, P2y1-1, 0); 00117 uLCD.filled_rectangle(124, P2y2+1, 127, 127, 0); 00118 uLCD.line(5, 8, 122, 8, 0xffffff); 00119 00120 // Print scores 00121 uLCD.locate(2,0); 00122 uLCD.printf("P1: %d P2: %d",P1S,P2S); 00123 wait(.005); 00124 } 00125 00126 }
Generated on Fri Jul 29 2022 02:53:24 by 1.7.2