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