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