Hunter Wardlaw / Mbed 2 deprecated Target_Practice

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed wave_player

Fork of USBKeyboard_HelloWorld by Samuel Mokrani

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "uLCD_4DGL.h"
00004 #include "wave_player.h"
00005 #include "stdio.h"
00006 
00007 DigitalIn right(p13);
00008 DigitalIn left(p11);
00009 DigitalIn up(p15);
00010 DigitalIn down(p12);
00011 
00012 DigitalIn top(p17);
00013 DigitalIn center(p20);
00014 DigitalIn bottom(p19);
00015 DigitalIn last(p16);
00016 
00017 SDFileSystem sd(p5, p6, p7, p8, "sd");
00018 uLCD_4DGL uLCD(p28,p27,p30);
00019 //Analog Out Jack
00020 AnalogOut DACout(p18);
00021 //On Board Speaker
00022 //PwmOut PWMout(p23);
00023 wave_player waver(&DACout);
00024 Timer t;
00025 
00026 struct target {
00027         bool dead;
00028         int myX, myY;
00029         bool wasVisible;
00030 };
00031 float currScore;
00032 float scores[9];
00033 
00034 
00035 int main(void) {
00036     //Start timer for seed value
00037     t.start();
00038     uLCD.cls();
00039     uLCD.baudrate(3000000);
00040     bool newGame;
00041     int xloc, yloc, xold, yold, kills;
00042     target myTargets[10];
00043     
00044     
00045     for(int i = 0; i < 10; i++){
00046         scores[i] = 999.99;    
00047     }
00048     FILE *wfile;
00049     
00050     
00051     
00052     // Exit main menu on button press
00053     uLCD.media_init();
00054     uLCD.set_sector_address(0x003B, 0x5001);
00055     uLCD.display_image(0,0);
00056     
00057     wfile = fopen("/sd/intro.wav","r");
00058     waver.play(wfile);
00059     fclose(wfile);
00060     
00061     uLCD.locate(0,10);
00062     uLCD.printf("Button 1 to Start!");
00063     while(top){
00064         //reset high score list
00065         if(!last) {
00066             for(int i = 0; i < 10; i++){
00067                 scores[i] = 999.99; 
00068                 uLCD.locate(0,0);
00069                 uLCD.printf("High scores reset");   
00070             }        
00071         } 
00072     }
00073     t.stop();
00074     
00075     
00076     
00077     //Main game loop
00078     while(true) {
00079     newGame = false;
00080     
00081     //Ready-set-go style countdown
00082     uLCD.cls();
00083     uLCD.locate(4,4);
00084     uLCD.printf("Get Ready!");
00085     wait(1); 
00086     uLCD.cls();
00087     uLCD.locate(8,4);
00088     uLCD.printf("3");
00089     wfile = fopen("/sd/beep.wav","r");
00090     waver.play(wfile);
00091     fclose(wfile);
00092     uLCD.cls();
00093     uLCD.locate(8,4);
00094     uLCD.printf("2");
00095     wfile = fopen("/sd/beep.wav","r");
00096     waver.play(wfile);
00097     fclose(wfile);
00098     uLCD.cls();
00099     uLCD.locate(8,4);
00100     uLCD.printf("1");
00101     wfile = fopen("/sd/beep.wav","r");
00102     waver.play(wfile);
00103     fclose(wfile);
00104     uLCD.cls();
00105     uLCD.locate(7,4);
00106     uLCD.printf("GO!");
00107     wfile = fopen("/sd/beephigh.wav","r");
00108     waver.play(wfile);
00109     fclose(wfile);
00110     uLCD.cls();
00111     
00112     
00113     xloc = 64;
00114     yloc = 64;
00115     xold = 0;
00116     yold = 0;
00117     kills = 0;
00118     
00119     
00120     //Create targets, use time as seed
00121     srand(t.read());
00122     
00123     for (int i = 0; i < 10; i++) {
00124         myTargets[i].myX = rand() % 128;
00125         myTargets[i].myY = rand() % 128;
00126         myTargets[i].dead = false;
00127         myTargets[i].wasVisible = false;
00128     }
00129     
00130     //Start score timer for game
00131     t.reset();
00132     t.start();
00133     while (!newGame) {
00134         //Redraw scope if moved
00135         if (xloc != xold || yloc != yold) {
00136             uLCD.filled_circle(xold, yold, 14, BLACK);
00137             uLCD.filled_circle(xloc, yloc, 14, GREEN);
00138             uLCD.filled_circle(xloc, yloc, 2, WHITE);
00139             xold = xloc;
00140             yold = yloc;
00141         }
00142         
00143         //Check if targets are visible
00144         for (int i = 0; i < 10; i++) {
00145             if (myTargets[i].dead == true) {
00146                 continue;
00147             }
00148                 
00149             if (abs(myTargets[i].myX - xloc) < 16 && abs(myTargets[i].myY - yloc) < 16) {
00150                 uLCD.filled_circle(myTargets[i].myX, myTargets[i].myY, 10, RED);
00151                 myTargets[i].wasVisible = true;
00152             }
00153             else if (myTargets[i].wasVisible == true) {
00154                 myTargets[i].wasVisible = false;
00155                 uLCD.filled_circle(myTargets[i].myX, myTargets[i].myY, 10, BLACK);
00156                 uLCD.filled_circle(xloc, yloc, 14, GREEN);
00157                 uLCD.filled_circle(xloc, yloc, 2, WHITE);
00158             }
00159         }
00160         
00161         
00162         //Check if shot hit
00163         if (!top) {
00164             wfile = fopen("/sd/newsilencer.wav","r");
00165             waver.play(wfile);
00166             fclose(wfile);
00167             for (int i = 0; i < 10; i++) {
00168                 if (myTargets[i].dead == true)
00169                 continue;
00170                 if (abs(myTargets[i].myX - xloc) < 8 && abs(myTargets[i].myY - yloc) < 8) {
00171                     uLCD.filled_circle(myTargets[i].myX, myTargets[i].myY, 10, BLACK);
00172                     uLCD.filled_circle(xloc, yloc, 14, GREEN);
00173                     uLCD.filled_circle(xloc, yloc, 2, WHITE);
00174                     myTargets[i].dead = true;
00175                     kills++;
00176                     uLCD.filled_circle(xloc, yloc, 2, 0xffa500);//orange
00177                     
00178                 
00179                    
00180             }
00181     }
00182         }  
00183                  
00184         if (!up) {
00185             yloc -= 6;
00186             if (yloc < 6)
00187                 yloc = 6;
00188         }
00189         if (!down){
00190             yloc += 6;
00191             if (yloc > 121)
00192                 yloc = 121;
00193         }
00194         if (!left) {
00195             xloc -= 6;
00196             if (xloc < 6)
00197                 xloc = 6;
00198         }
00199         if (!right) {
00200             xloc += 6;
00201             if (xloc > 121)
00202                 xloc = 121;
00203         }
00204         uLCD.locate(0,0);
00205         uLCD.printf("Time: %0.2f", t.read());
00206         wait(.025);
00207         //Win Screen
00208         if (kills >= 10) {
00209             t.stop();
00210             currScore = t.read() - .4;
00211             uLCD.cls();
00212 
00213             if(currScore < scores[0]){
00214                 uLCD.locate(0,1);
00215                 uLCD.printf("NEW HIGH SCORE!!!");    
00216             }
00217             for(int i = 0; i < 9; i++){ 
00218                 if(currScore < scores[i]) {
00219                     for(int j = 7; j >= i; j--){
00220                      scores[j+1] = scores[j];
00221                     }  
00222                     scores[i] = currScore;
00223                     break;
00224                 }
00225                     
00226             }
00227             
00228             uLCD.locate(0,3);
00229             uLCD.printf("High Scores\n");
00230             uLCD.printf("-----------\n");
00231             for(int i = 0; i < 9; i++){
00232                 uLCD.printf("%d. %6.2f\n", i+1, scores[i]);    
00233             }
00234             //display score
00235             uLCD.locate(0,0);
00236             uLCD.printf("Final Score: %0.2f\n",currScore);
00237             wfile = fopen("/sd/youwinshort.wav","r");
00238             waver.play(wfile);
00239             fclose(wfile);
00240             uLCD.filled_rectangle(0, 0, 128, 16, BLACK);
00241             uLCD.locate(0,0);
00242             uLCD.printf("New Game?");
00243             while (true) {
00244                 //Break and reset on button press
00245                 if (!top) {
00246                     newGame = true;
00247                     break;
00248                 }
00249                
00250             }
00251         }
00252     }
00253     }
00254 }