Pong Video Game Demo - MBED + SmartGPU2 board

Dependencies:   SMARTGPU2 mbed

Revision:
0:9ef82d07ae18
Child:
1:3dc267ad8b64
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jul 10 03:40:04 2013 +0000
@@ -0,0 +1,221 @@
+/**************************************************************************************/
+/*SMARTGPU2 intelligent embedded graphics processor unit
+ those examples are for use the SMARTGPU2 with the mbed microcontoller, just connect tx,rx,and reset
+ Board:
+ http://vizictechnologies.com/#/smart-gpu-2/4577779046
+ 
+ www.vizictechnologies.com 
+ Vizic Technologies copyright 2013 */
+/**************************************************************************************/
+
+#include "mbed.h"
+#include "SMARTGPU2.h"
+
+SMARTGPU2 lcd(TXPIN,RXPIN,RESETPIN);  //create our object called "lcd"
+
+// defines for balls
+#define radiusBall1 15     //ball1 size
+#define colourBall1 GREEN  //ball1 colour
+#define radiusBall2 8      //ball2 size
+#define colourBall2 YELLOW //ball2 colour
+
+POINT point;
+NUMBEROFBYTES charsPrinted;
+
+//variables used by move ball methods
+int speedBall1=2; //ball1 moving speed - amount of pixels that ball move each time
+int speedBall2=3; //ball2 moving speed - amount of pixels that ball move each time 
+int dirx1=1;      //xball1 initial positive direction
+int diry1=1;      //yball1 initial positive direction
+int xBall1=300;   //x initial position of ball1
+int yBall1;       //y position of ball1
+int dirx2=-1;     //xball2 initial negative direction
+int diry2=1;      //yball2 initial positive direction
+int xBall2=30;    //x initial position of ball2
+int yBall2;       //y position of ball2
+
+//variables used by Pong method
+char score[7]={0,0,' ','P','T','S',0x00}; //array to save score
+char points;            
+char gameOver;          //game over flag
+char speedCounter;      //Counter that saves the speed 
+char ball1Active;       //acrive ball flag
+char ball2Active;       //active ball flag 
+int barSize = 60;       //size of bar in pixels 
+int bar=50;             //initial x position of the bar   
+int barCenter = (barSize/2)+bar; //we need to know the center of the bar
+
+/***************************************************/
+//function that move or update the actual position of the ball1
+void moveBall1(){
+   lcd.drawCircle(xBall1,yBall1,radiusBall1,BLACK,UNFILL);       // Erase previous ball position
+   xBall1+=(dirx1*speedBall1);                                   // Calculate new x coordinate for ball1 
+   yBall1+=(diry1*speedBall1);                                   // Calculate new y coordinate for ball1  
+   lcd.drawCircle(xBall1,yBall1,radiusBall1,colourBall1,UNFILL); // Draw new ball position
+   if((xBall1+speedBall1+radiusBall1)>318 | (xBall1-speedBall1-radiusBall1)<=1){       // if ball reaches the left or right corner, we invert moving direction 
+    dirx1= dirx1*(-1);
+   }
+   if((yBall1+speedBall1+radiusBall1)>233 | (yBall1-speedBall1-radiusBall1)<=21){      // if ball reaches the top or bottom corner, we invert moving direction 
+    if((yBall1-speedBall1-radiusBall1)<=21){                    // Bounce on top, only invert moving direction
+      diry1= diry1*(-1);                                        // We invert the moving direction by multiplying by -1
+    }else{                                                      // Bounce on bottom, check if inside the bar       
+      if((xBall1+speedBall1+radiusBall1)>bar & (xBall1-speedBall1-radiusBall1)<(bar+barSize)){  //if bounce inside the bar
+        diry1= diry1*(-1);                                      // We invert the moving direction by multiplying by -1
+        points++;                                               // Increase player current points
+        speedCounter++;                                         // Increase the speed counter
+        if(speedCounter>9){                                     // If we reach 10 counts we increase the ball1 bouncing speed
+          speedBall1++;          
+          speedCounter=0;                                       // Reset the counter
+        }                           
+      }else{                                                   // Bounce outside the bar
+        ball1Active=0;                                         // Clear ball1 active flag
+        lcd.drawCircle(xBall1,yBall1,radiusBall1,BLACK,UNFILL);// Delete this ball because bounce outside of the bar        
+        if(ball1Active==0 & ball2Active==0){                   // if we have lost both balls     
+          gameOver=1;                                          // Set game over flag
+        }         
+      }
+    }
+   } 
+}
+
+/***************************************************/
+//function that move or update the actual position of the ball2
+void moveBall2(){
+   lcd.drawCircle(xBall2,yBall2,radiusBall2,BLACK,FILL);       // Erase previous ball position
+   xBall2+=(dirx2*speedBall2);                                 // Calculate new x coordinate for ball2 
+   yBall2+=(diry2*speedBall2);                                 // Calculate new y coordinate for ball2
+   lcd.drawCircle(xBall2,yBall2,radiusBall2,colourBall2,FILL); // Draw new ball position
+   if((xBall2+speedBall2+radiusBall2)>318 | (xBall2-speedBall2-radiusBall2)<=1){       // if ball reaches the left or right corner, we invert moving direction 
+    dirx2= dirx2*(-1);
+   }
+   if((yBall2+speedBall2+radiusBall2)>233 | (yBall2-speedBall2-radiusBall2)<=21){      // if ball reaches the top or bottom corner, we invert moving direction 
+    if((yBall2-speedBall2-radiusBall2)<=21){                    // Bounce on top, only invert moving direction
+      diry2= diry2*(-1);        
+    }else{                                                      // Bounce on bottom, check if inside the bar       
+      if((xBall2+speedBall2+radiusBall2)>bar & (xBall2-speedBall2-radiusBall2)<(bar+barSize)){  //if bounce inside the bar
+        diry2= diry2*(-1);                                      // We invert the moving direction by multiplying by -1
+        points++;                                               // Increase player current points
+        speedCounter++;                                         // Increase the speed counter
+        if(speedCounter>9){                                     // If we reach 10 counts we increase the ball1 bouncing speed
+          speedBall2++;          
+          speedCounter=0;                                       // Reset the counter
+        }                           
+      }else{                                                   // Bounce outside the bar
+        ball2Active=0;                                         // Clear ball1 active flag
+        lcd.drawCircle(xBall2,yBall2,radiusBall2,BLACK,FILL);  // Delete this ball because bounce outside of the bar        
+        if(ball1Active==0 & ball2Active==0){                   // if we have lost both balls     
+          gameOver=1;                                          // Set game over flag
+        }         
+      }
+    }
+   } 
+}
+
+/***************************************************/
+/***************************************************/
+//The game methodology
+void pong(){
+  unsigned char i;
+  char score[7]={0,0,' ','P','T','S',0x00};
+  char buffer[4]="00 ";
+  
+  //reset all parameters
+  gameOver=0;            //reset flag
+  points=0;              //reset points
+  speedCounter=0;        //reset speed
+  ball1Active=1;         //acrive ball flag
+  ball2Active=1;         //active ball flag 
+  yBall1=25+radiusBall1; //y initial position of ball1 
+  yBall2=25+radiusBall2; //y initial position of ball2
+  
+  lcd.drawRectangle(0,20,319,239,MAGENTA,UNFILL);                        //draw corners
+  lcd.drawLine(bar,234,bar+barSize,234,WHITE);                           //draw Bar 
+  
+  while(gameOver==0){                                                    //while game over flag is zero                          
+    buffer[0]=(points/10)+0x30, buffer[1]=(points%10)+0x30;              //fill buffer that counts
+    lcd.setTextColour(RED);
+    lcd.setTextSize(FONT1);
+    lcd.setTextBackFill(FILLED);      
+    lcd.string(2,2,30,35,buffer,&charsPrinted);                          //display current points
+      for(i=0;i<10;i++){                                                 //check 30 times if the player touches the screen
+        if(lcd.touchScreen(&point)){                                     //if we receive a touch then we move the bar to touched side
+          lcd.drawLine(bar,234,bar+barSize,234,BLACK);                   //erase previous Bar    
+          if(point.x>barCenter){                                         //if we need to move the bar to the right           
+            bar+=8;                                                      //move the bar to the right  8 pixels
+            if((bar+barSize)>318){                                       //if the bar reach the right corner
+              bar=318-barSize;      
+            }              
+            barCenter=bar+(barSize/2);                                   //set new center position of the bar
+          }else{                                                         //move the bar to the left                                   
+            bar-=8;                                                      //move the bar to the left 8 pixels 
+            if(bar<1){                                                   //if the bar reach the left corner
+              bar=1;  
+            }              
+            barCenter=bar+(barSize/2);                                   //set new center position of the bar
+          }
+          lcd.drawLine(bar,234,bar+barSize,234,WHITE);                   //draw the new bar at the new position
+        }
+        wait_ms(1);         
+      }
+      //we update the balls
+      if(ball1Active){                                                   //if we haven't lost the ball1
+        moveBall1(); 
+      }
+      if(ball2Active){                                                   //if we haven't lost the ball2
+        moveBall2();      
+      }
+  }
+  //game over - proceed to show final score
+  lcd.setTextColour(RED);
+  lcd.setTextSize(FONT4);
+  lcd.setTextBackFill(TRANS);    
+  lcd.string(70,60,319,239,"Game Over",&charsPrinted);
+  score[0]=(points/10)+0x30;                                            //convert points to ascii format and store them on the score buffer
+  score[1]=(points%10)+0x30;                                            //convert points to ascii format and store them on the score buffer
+  lcd.setTextColour(YELLOW);
+  lcd.setTextSize(FONT4);
+  lcd.string(100,110,319,239,score,&charsPrinted);
+  lcd.setTextColour(GREEN);
+  lcd.setTextSize(FONT2);  
+  lcd.string(90,160,319,239,"Touch to Exit",&charsPrinted);
+  wait_ms(1000);
+  while(lcd.touchScreen(&point)==0);                                    //wait for a touch to exit
+  lcd.erase();                                                          //erase the screen and exit function
+}
+
+/***************************************************/
+/***************************************************/
+void initializeSmartGPU2(void){      //Initialize SMARTGPU2 Board
+  lcd.reset();                       //physically reset SMARTGPU2
+  lcd.start();                       //initialize the SMARTGPU2 processor
+}
+
+/***************************************************/
+/***************************************************/
+/***************************************************/
+/***************************************************/
+int main() {
+  initializeSmartGPU2();             //Init communication with SmartGPU2 board
+  
+  lcd.baudChange(BAUD7);             //set a fast baud! for fast drawing
+  
+  while(1){                // Loop forever
+    //Draw a cover
+    lcd.drawTriangle(20,20,70,40,40,80,YELLOW,FILL);
+    lcd.drawRectangle(250,30,300,80,MAGENTA,FILL);
+    lcd.drawCircle(50,190,28,CYAN,FILL);
+    lcd.drawTriangle(270,230,240,190,300,170,RED,FILL);
+    lcd.setTextColour(WHITE);
+    lcd.setTextSize(FONT5);    
+    lcd.setTextBackFill(TRANS); 
+    lcd.string(100,70,319,239,"PONG",&charsPrinted);
+    lcd.setTextColour(GREEN);
+    lcd.setTextSize(FONT2);        
+    lcd.string(40,130,310,220,"Touch screen to begin",&charsPrinted);
+    wait_ms(1000);
+    while(lcd.touchScreen(&point)==INVALID); //wait for a touch to begin
+    lcd.erase();
+    //start the game
+    pong();               // Play until game over
+  }
+}