Pong Video Game Demo - MBED + SmartGPU2 board

Dependencies:   SMARTGPU2 mbed

Committer:
emmanuelchio
Date:
Thu Apr 17 23:52:36 2014 +0000
Revision:
1:3dc267ad8b64
Parent:
0:9ef82d07ae18
SmartGPU2 PongGame_SG2 demo- Please select(uncomment) your smartGPU2 board under SMARTGPU2.h file before compiling!!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emmanuelchio 1:3dc267ad8b64 1 /**************************************************************************************/
emmanuelchio 0:9ef82d07ae18 2 /**************************************************************************************/
emmanuelchio 0:9ef82d07ae18 3 /*SMARTGPU2 intelligent embedded graphics processor unit
emmanuelchio 1:3dc267ad8b64 4 those examples are for using the SMARTGPU2 with the mbed microcontoller, just connect tx,rx,and reset
emmanuelchio 0:9ef82d07ae18 5 Board:
emmanuelchio 1:3dc267ad8b64 6 http://www.vizictechnologies.com/
emmanuelchio 0:9ef82d07ae18 7
emmanuelchio 0:9ef82d07ae18 8 www.vizictechnologies.com
emmanuelchio 1:3dc267ad8b64 9 Vizic Technologies copyright 2014 */
emmanuelchio 1:3dc267ad8b64 10 /**************************************************************************************/
emmanuelchio 0:9ef82d07ae18 11 /**************************************************************************************/
emmanuelchio 0:9ef82d07ae18 12
emmanuelchio 0:9ef82d07ae18 13 #include "mbed.h"
emmanuelchio 0:9ef82d07ae18 14 #include "SMARTGPU2.h"
emmanuelchio 0:9ef82d07ae18 15
emmanuelchio 0:9ef82d07ae18 16 SMARTGPU2 lcd(TXPIN,RXPIN,RESETPIN); //create our object called "lcd"
emmanuelchio 0:9ef82d07ae18 17
emmanuelchio 0:9ef82d07ae18 18 // defines for balls
emmanuelchio 0:9ef82d07ae18 19 #define radiusBall1 15 //ball1 size
emmanuelchio 0:9ef82d07ae18 20 #define colourBall1 GREEN //ball1 colour
emmanuelchio 0:9ef82d07ae18 21 #define radiusBall2 8 //ball2 size
emmanuelchio 0:9ef82d07ae18 22 #define colourBall2 YELLOW //ball2 colour
emmanuelchio 0:9ef82d07ae18 23
emmanuelchio 0:9ef82d07ae18 24 POINT point;
emmanuelchio 0:9ef82d07ae18 25 NUMBEROFBYTES charsPrinted;
emmanuelchio 0:9ef82d07ae18 26
emmanuelchio 0:9ef82d07ae18 27 //variables used by move ball methods
emmanuelchio 0:9ef82d07ae18 28 int speedBall1=2; //ball1 moving speed - amount of pixels that ball move each time
emmanuelchio 0:9ef82d07ae18 29 int speedBall2=3; //ball2 moving speed - amount of pixels that ball move each time
emmanuelchio 0:9ef82d07ae18 30 int dirx1=1; //xball1 initial positive direction
emmanuelchio 0:9ef82d07ae18 31 int diry1=1; //yball1 initial positive direction
emmanuelchio 0:9ef82d07ae18 32 int xBall1=300; //x initial position of ball1
emmanuelchio 0:9ef82d07ae18 33 int yBall1; //y position of ball1
emmanuelchio 0:9ef82d07ae18 34 int dirx2=-1; //xball2 initial negative direction
emmanuelchio 0:9ef82d07ae18 35 int diry2=1; //yball2 initial positive direction
emmanuelchio 0:9ef82d07ae18 36 int xBall2=30; //x initial position of ball2
emmanuelchio 0:9ef82d07ae18 37 int yBall2; //y position of ball2
emmanuelchio 0:9ef82d07ae18 38
emmanuelchio 0:9ef82d07ae18 39 //variables used by Pong method
emmanuelchio 0:9ef82d07ae18 40 char score[7]={0,0,' ','P','T','S',0x00}; //array to save score
emmanuelchio 0:9ef82d07ae18 41 char points;
emmanuelchio 0:9ef82d07ae18 42 char gameOver; //game over flag
emmanuelchio 0:9ef82d07ae18 43 char speedCounter; //Counter that saves the speed
emmanuelchio 0:9ef82d07ae18 44 char ball1Active; //acrive ball flag
emmanuelchio 0:9ef82d07ae18 45 char ball2Active; //active ball flag
emmanuelchio 0:9ef82d07ae18 46 int barSize = 60; //size of bar in pixels
emmanuelchio 0:9ef82d07ae18 47 int bar=50; //initial x position of the bar
emmanuelchio 0:9ef82d07ae18 48 int barCenter = (barSize/2)+bar; //we need to know the center of the bar
emmanuelchio 0:9ef82d07ae18 49
emmanuelchio 0:9ef82d07ae18 50 /***************************************************/
emmanuelchio 0:9ef82d07ae18 51 //function that move or update the actual position of the ball1
emmanuelchio 0:9ef82d07ae18 52 void moveBall1(){
emmanuelchio 0:9ef82d07ae18 53 lcd.drawCircle(xBall1,yBall1,radiusBall1,BLACK,UNFILL); // Erase previous ball position
emmanuelchio 0:9ef82d07ae18 54 xBall1+=(dirx1*speedBall1); // Calculate new x coordinate for ball1
emmanuelchio 0:9ef82d07ae18 55 yBall1+=(diry1*speedBall1); // Calculate new y coordinate for ball1
emmanuelchio 0:9ef82d07ae18 56 lcd.drawCircle(xBall1,yBall1,radiusBall1,colourBall1,UNFILL); // Draw new ball position
emmanuelchio 0:9ef82d07ae18 57 if((xBall1+speedBall1+radiusBall1)>318 | (xBall1-speedBall1-radiusBall1)<=1){ // if ball reaches the left or right corner, we invert moving direction
emmanuelchio 0:9ef82d07ae18 58 dirx1= dirx1*(-1);
emmanuelchio 0:9ef82d07ae18 59 }
emmanuelchio 0:9ef82d07ae18 60 if((yBall1+speedBall1+radiusBall1)>233 | (yBall1-speedBall1-radiusBall1)<=21){ // if ball reaches the top or bottom corner, we invert moving direction
emmanuelchio 0:9ef82d07ae18 61 if((yBall1-speedBall1-radiusBall1)<=21){ // Bounce on top, only invert moving direction
emmanuelchio 0:9ef82d07ae18 62 diry1= diry1*(-1); // We invert the moving direction by multiplying by -1
emmanuelchio 0:9ef82d07ae18 63 }else{ // Bounce on bottom, check if inside the bar
emmanuelchio 0:9ef82d07ae18 64 if((xBall1+speedBall1+radiusBall1)>bar & (xBall1-speedBall1-radiusBall1)<(bar+barSize)){ //if bounce inside the bar
emmanuelchio 0:9ef82d07ae18 65 diry1= diry1*(-1); // We invert the moving direction by multiplying by -1
emmanuelchio 0:9ef82d07ae18 66 points++; // Increase player current points
emmanuelchio 0:9ef82d07ae18 67 speedCounter++; // Increase the speed counter
emmanuelchio 0:9ef82d07ae18 68 if(speedCounter>9){ // If we reach 10 counts we increase the ball1 bouncing speed
emmanuelchio 0:9ef82d07ae18 69 speedBall1++;
emmanuelchio 0:9ef82d07ae18 70 speedCounter=0; // Reset the counter
emmanuelchio 0:9ef82d07ae18 71 }
emmanuelchio 0:9ef82d07ae18 72 }else{ // Bounce outside the bar
emmanuelchio 0:9ef82d07ae18 73 ball1Active=0; // Clear ball1 active flag
emmanuelchio 0:9ef82d07ae18 74 lcd.drawCircle(xBall1,yBall1,radiusBall1,BLACK,UNFILL);// Delete this ball because bounce outside of the bar
emmanuelchio 0:9ef82d07ae18 75 if(ball1Active==0 & ball2Active==0){ // if we have lost both balls
emmanuelchio 0:9ef82d07ae18 76 gameOver=1; // Set game over flag
emmanuelchio 0:9ef82d07ae18 77 }
emmanuelchio 0:9ef82d07ae18 78 }
emmanuelchio 0:9ef82d07ae18 79 }
emmanuelchio 0:9ef82d07ae18 80 }
emmanuelchio 0:9ef82d07ae18 81 }
emmanuelchio 0:9ef82d07ae18 82
emmanuelchio 0:9ef82d07ae18 83 /***************************************************/
emmanuelchio 0:9ef82d07ae18 84 //function that move or update the actual position of the ball2
emmanuelchio 0:9ef82d07ae18 85 void moveBall2(){
emmanuelchio 0:9ef82d07ae18 86 lcd.drawCircle(xBall2,yBall2,radiusBall2,BLACK,FILL); // Erase previous ball position
emmanuelchio 0:9ef82d07ae18 87 xBall2+=(dirx2*speedBall2); // Calculate new x coordinate for ball2
emmanuelchio 0:9ef82d07ae18 88 yBall2+=(diry2*speedBall2); // Calculate new y coordinate for ball2
emmanuelchio 0:9ef82d07ae18 89 lcd.drawCircle(xBall2,yBall2,radiusBall2,colourBall2,FILL); // Draw new ball position
emmanuelchio 0:9ef82d07ae18 90 if((xBall2+speedBall2+radiusBall2)>318 | (xBall2-speedBall2-radiusBall2)<=1){ // if ball reaches the left or right corner, we invert moving direction
emmanuelchio 0:9ef82d07ae18 91 dirx2= dirx2*(-1);
emmanuelchio 0:9ef82d07ae18 92 }
emmanuelchio 0:9ef82d07ae18 93 if((yBall2+speedBall2+radiusBall2)>233 | (yBall2-speedBall2-radiusBall2)<=21){ // if ball reaches the top or bottom corner, we invert moving direction
emmanuelchio 0:9ef82d07ae18 94 if((yBall2-speedBall2-radiusBall2)<=21){ // Bounce on top, only invert moving direction
emmanuelchio 0:9ef82d07ae18 95 diry2= diry2*(-1);
emmanuelchio 0:9ef82d07ae18 96 }else{ // Bounce on bottom, check if inside the bar
emmanuelchio 0:9ef82d07ae18 97 if((xBall2+speedBall2+radiusBall2)>bar & (xBall2-speedBall2-radiusBall2)<(bar+barSize)){ //if bounce inside the bar
emmanuelchio 0:9ef82d07ae18 98 diry2= diry2*(-1); // We invert the moving direction by multiplying by -1
emmanuelchio 0:9ef82d07ae18 99 points++; // Increase player current points
emmanuelchio 0:9ef82d07ae18 100 speedCounter++; // Increase the speed counter
emmanuelchio 0:9ef82d07ae18 101 if(speedCounter>9){ // If we reach 10 counts we increase the ball1 bouncing speed
emmanuelchio 0:9ef82d07ae18 102 speedBall2++;
emmanuelchio 0:9ef82d07ae18 103 speedCounter=0; // Reset the counter
emmanuelchio 0:9ef82d07ae18 104 }
emmanuelchio 0:9ef82d07ae18 105 }else{ // Bounce outside the bar
emmanuelchio 0:9ef82d07ae18 106 ball2Active=0; // Clear ball1 active flag
emmanuelchio 0:9ef82d07ae18 107 lcd.drawCircle(xBall2,yBall2,radiusBall2,BLACK,FILL); // Delete this ball because bounce outside of the bar
emmanuelchio 0:9ef82d07ae18 108 if(ball1Active==0 & ball2Active==0){ // if we have lost both balls
emmanuelchio 0:9ef82d07ae18 109 gameOver=1; // Set game over flag
emmanuelchio 0:9ef82d07ae18 110 }
emmanuelchio 0:9ef82d07ae18 111 }
emmanuelchio 0:9ef82d07ae18 112 }
emmanuelchio 0:9ef82d07ae18 113 }
emmanuelchio 0:9ef82d07ae18 114 }
emmanuelchio 0:9ef82d07ae18 115
emmanuelchio 0:9ef82d07ae18 116 /***************************************************/
emmanuelchio 0:9ef82d07ae18 117 /***************************************************/
emmanuelchio 0:9ef82d07ae18 118 //The game methodology
emmanuelchio 0:9ef82d07ae18 119 void pong(){
emmanuelchio 0:9ef82d07ae18 120 unsigned char i;
emmanuelchio 0:9ef82d07ae18 121 char score[7]={0,0,' ','P','T','S',0x00};
emmanuelchio 0:9ef82d07ae18 122 char buffer[4]="00 ";
emmanuelchio 0:9ef82d07ae18 123
emmanuelchio 0:9ef82d07ae18 124 //reset all parameters
emmanuelchio 0:9ef82d07ae18 125 gameOver=0; //reset flag
emmanuelchio 0:9ef82d07ae18 126 points=0; //reset points
emmanuelchio 0:9ef82d07ae18 127 speedCounter=0; //reset speed
emmanuelchio 0:9ef82d07ae18 128 ball1Active=1; //acrive ball flag
emmanuelchio 0:9ef82d07ae18 129 ball2Active=1; //active ball flag
emmanuelchio 0:9ef82d07ae18 130 yBall1=25+radiusBall1; //y initial position of ball1
emmanuelchio 0:9ef82d07ae18 131 yBall2=25+radiusBall2; //y initial position of ball2
emmanuelchio 0:9ef82d07ae18 132
emmanuelchio 0:9ef82d07ae18 133 lcd.drawRectangle(0,20,319,239,MAGENTA,UNFILL); //draw corners
emmanuelchio 0:9ef82d07ae18 134 lcd.drawLine(bar,234,bar+barSize,234,WHITE); //draw Bar
emmanuelchio 0:9ef82d07ae18 135
emmanuelchio 0:9ef82d07ae18 136 while(gameOver==0){ //while game over flag is zero
emmanuelchio 0:9ef82d07ae18 137 buffer[0]=(points/10)+0x30, buffer[1]=(points%10)+0x30; //fill buffer that counts
emmanuelchio 0:9ef82d07ae18 138 lcd.setTextColour(RED);
emmanuelchio 0:9ef82d07ae18 139 lcd.setTextSize(FONT1);
emmanuelchio 0:9ef82d07ae18 140 lcd.setTextBackFill(FILLED);
emmanuelchio 0:9ef82d07ae18 141 lcd.string(2,2,30,35,buffer,&charsPrinted); //display current points
emmanuelchio 0:9ef82d07ae18 142 for(i=0;i<10;i++){ //check 30 times if the player touches the screen
emmanuelchio 0:9ef82d07ae18 143 if(lcd.touchScreen(&point)){ //if we receive a touch then we move the bar to touched side
emmanuelchio 0:9ef82d07ae18 144 lcd.drawLine(bar,234,bar+barSize,234,BLACK); //erase previous Bar
emmanuelchio 0:9ef82d07ae18 145 if(point.x>barCenter){ //if we need to move the bar to the right
emmanuelchio 0:9ef82d07ae18 146 bar+=8; //move the bar to the right 8 pixels
emmanuelchio 0:9ef82d07ae18 147 if((bar+barSize)>318){ //if the bar reach the right corner
emmanuelchio 0:9ef82d07ae18 148 bar=318-barSize;
emmanuelchio 0:9ef82d07ae18 149 }
emmanuelchio 0:9ef82d07ae18 150 barCenter=bar+(barSize/2); //set new center position of the bar
emmanuelchio 0:9ef82d07ae18 151 }else{ //move the bar to the left
emmanuelchio 0:9ef82d07ae18 152 bar-=8; //move the bar to the left 8 pixels
emmanuelchio 0:9ef82d07ae18 153 if(bar<1){ //if the bar reach the left corner
emmanuelchio 0:9ef82d07ae18 154 bar=1;
emmanuelchio 0:9ef82d07ae18 155 }
emmanuelchio 0:9ef82d07ae18 156 barCenter=bar+(barSize/2); //set new center position of the bar
emmanuelchio 0:9ef82d07ae18 157 }
emmanuelchio 0:9ef82d07ae18 158 lcd.drawLine(bar,234,bar+barSize,234,WHITE); //draw the new bar at the new position
emmanuelchio 0:9ef82d07ae18 159 }
emmanuelchio 0:9ef82d07ae18 160 wait_ms(1);
emmanuelchio 0:9ef82d07ae18 161 }
emmanuelchio 0:9ef82d07ae18 162 //we update the balls
emmanuelchio 0:9ef82d07ae18 163 if(ball1Active){ //if we haven't lost the ball1
emmanuelchio 0:9ef82d07ae18 164 moveBall1();
emmanuelchio 0:9ef82d07ae18 165 }
emmanuelchio 0:9ef82d07ae18 166 if(ball2Active){ //if we haven't lost the ball2
emmanuelchio 0:9ef82d07ae18 167 moveBall2();
emmanuelchio 0:9ef82d07ae18 168 }
emmanuelchio 0:9ef82d07ae18 169 }
emmanuelchio 0:9ef82d07ae18 170 //game over - proceed to show final score
emmanuelchio 0:9ef82d07ae18 171 lcd.setTextColour(RED);
emmanuelchio 0:9ef82d07ae18 172 lcd.setTextSize(FONT4);
emmanuelchio 0:9ef82d07ae18 173 lcd.setTextBackFill(TRANS);
emmanuelchio 0:9ef82d07ae18 174 lcd.string(70,60,319,239,"Game Over",&charsPrinted);
emmanuelchio 0:9ef82d07ae18 175 score[0]=(points/10)+0x30; //convert points to ascii format and store them on the score buffer
emmanuelchio 0:9ef82d07ae18 176 score[1]=(points%10)+0x30; //convert points to ascii format and store them on the score buffer
emmanuelchio 0:9ef82d07ae18 177 lcd.setTextColour(YELLOW);
emmanuelchio 0:9ef82d07ae18 178 lcd.setTextSize(FONT4);
emmanuelchio 0:9ef82d07ae18 179 lcd.string(100,110,319,239,score,&charsPrinted);
emmanuelchio 0:9ef82d07ae18 180 lcd.setTextColour(GREEN);
emmanuelchio 0:9ef82d07ae18 181 lcd.setTextSize(FONT2);
emmanuelchio 0:9ef82d07ae18 182 lcd.string(90,160,319,239,"Touch to Exit",&charsPrinted);
emmanuelchio 0:9ef82d07ae18 183 wait_ms(1000);
emmanuelchio 0:9ef82d07ae18 184 while(lcd.touchScreen(&point)==0); //wait for a touch to exit
emmanuelchio 0:9ef82d07ae18 185 lcd.erase(); //erase the screen and exit function
emmanuelchio 0:9ef82d07ae18 186 }
emmanuelchio 0:9ef82d07ae18 187
emmanuelchio 0:9ef82d07ae18 188 /***************************************************/
emmanuelchio 0:9ef82d07ae18 189 /***************************************************/
emmanuelchio 0:9ef82d07ae18 190 void initializeSmartGPU2(void){ //Initialize SMARTGPU2 Board
emmanuelchio 0:9ef82d07ae18 191 lcd.reset(); //physically reset SMARTGPU2
emmanuelchio 0:9ef82d07ae18 192 lcd.start(); //initialize the SMARTGPU2 processor
emmanuelchio 0:9ef82d07ae18 193 }
emmanuelchio 0:9ef82d07ae18 194
emmanuelchio 0:9ef82d07ae18 195 /***************************************************/
emmanuelchio 0:9ef82d07ae18 196 /***************************************************/
emmanuelchio 0:9ef82d07ae18 197 /***************************************************/
emmanuelchio 0:9ef82d07ae18 198 /***************************************************/
emmanuelchio 0:9ef82d07ae18 199 int main() {
emmanuelchio 0:9ef82d07ae18 200 initializeSmartGPU2(); //Init communication with SmartGPU2 board
emmanuelchio 0:9ef82d07ae18 201
emmanuelchio 0:9ef82d07ae18 202 lcd.baudChange(BAUD7); //set a fast baud! for fast drawing
emmanuelchio 0:9ef82d07ae18 203
emmanuelchio 0:9ef82d07ae18 204 while(1){ // Loop forever
emmanuelchio 0:9ef82d07ae18 205 //Draw a cover
emmanuelchio 0:9ef82d07ae18 206 lcd.drawTriangle(20,20,70,40,40,80,YELLOW,FILL);
emmanuelchio 0:9ef82d07ae18 207 lcd.drawRectangle(250,30,300,80,MAGENTA,FILL);
emmanuelchio 0:9ef82d07ae18 208 lcd.drawCircle(50,190,28,CYAN,FILL);
emmanuelchio 0:9ef82d07ae18 209 lcd.drawTriangle(270,230,240,190,300,170,RED,FILL);
emmanuelchio 0:9ef82d07ae18 210 lcd.setTextColour(WHITE);
emmanuelchio 0:9ef82d07ae18 211 lcd.setTextSize(FONT5);
emmanuelchio 0:9ef82d07ae18 212 lcd.setTextBackFill(TRANS);
emmanuelchio 0:9ef82d07ae18 213 lcd.string(100,70,319,239,"PONG",&charsPrinted);
emmanuelchio 0:9ef82d07ae18 214 lcd.setTextColour(GREEN);
emmanuelchio 0:9ef82d07ae18 215 lcd.setTextSize(FONT2);
emmanuelchio 0:9ef82d07ae18 216 lcd.string(40,130,310,220,"Touch screen to begin",&charsPrinted);
emmanuelchio 0:9ef82d07ae18 217 wait_ms(1000);
emmanuelchio 0:9ef82d07ae18 218 while(lcd.touchScreen(&point)==INVALID); //wait for a touch to begin
emmanuelchio 0:9ef82d07ae18 219 lcd.erase();
emmanuelchio 0:9ef82d07ae18 220 //start the game
emmanuelchio 0:9ef82d07ae18 221 pong(); // Play until game over
emmanuelchio 0:9ef82d07ae18 222 }
emmanuelchio 0:9ef82d07ae18 223 }