Martin Engelcke / Mbed 2 deprecated CarGame

Dependencies:   TSI TextLCD mbed

Fork of CarGame by Dan James

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* CarGame has the aim of trying to avoid obstacles on two lanes for as long as possbile.
00002 The speed increases as the game progresses and when you die you are able to see your score. */
00003 
00004 #include "mbed.h"
00005 #include "TextLCD.h"
00006 #include "TSISensor.h"
00007 
00008 // initialise LCD display
00009 TextLCD lcd(PTA1,PTA2,PTD4,PTA12,PTA4,PTA5); // rs, e, d4-d7
00010 
00011 // number of seconds to wait between each frame movement
00012 float frameTime = 1;
00013 
00014 // number of frames since speed increase
00015 int steps = 0;
00016 
00017 // number of frames in total
00018 int score = 0;
00019 
00020 // size of course
00021 int courseSize = 48;
00022 
00023 int main() 
00024 {
00025     // set up course
00026     char course[courseSize][2];
00027     
00028     // blank course to start with
00029     for(int i = 0; i < courseSize; i++)
00030     {
00031         course[i][0] = ' ';
00032         course[i][1] = ' ';
00033     }
00034     
00035     // obstacle course
00036     course[3][0] = '0';
00037     course[6][1] = '0';
00038     course[9][1] = '0';
00039     course[12][0] = '0';
00040     course[15][1] = '0';
00041     course[16][1] = '0';
00042     course[19][0] = '0';
00043     course[21][1] = '0';
00044     course[23][0] = '0';
00045     course[24][0] = '0';
00046     course[29][1] = '0';
00047     course[33][0] = '0';
00048     course[35][0] = '0';
00049     course[37][0] = '0';
00050     course[40][1] = '0';
00051     course[43][0] = '0';
00052     course[46][1] = '0';
00053     
00054     // set up LED output
00055     PwmOut led(LED_GREEN);
00056     // set up touch sensor to control game
00057     TSISensor tsi;
00058     
00059     int gameover = 0; // define a 'gameover' flag
00060     
00061     // game loop
00062     while(1)
00063     {    
00064         
00065         // print current course on lcd
00066         for(int i = 15; i >= 0; i--)
00067         {
00068             lcd.locate(i,0);
00069             lcd.printf("%c",course[i][0]);
00070             lcd.locate(i,1);
00071             lcd.printf("%c",course[i][1]);
00072         }       
00073         
00074         // create next frame of course
00075         // course loops round infinitely
00076         
00077         // these are rows that will be pushed off top of screen
00078         // save them so they can be put at bottom once finished
00079         char toprow1 =  course[courseSize-1][0];
00080         char toprow2 =  course[courseSize-1][1];
00081         
00082         // shift each row up 1
00083         for(int i = courseSize-1; i >= 0; i--)
00084         {
00085             course[i][0] = course[i-1][0];
00086             course[i][1] = course[i-1][1];
00087         }
00088         
00089         // set bottom rows as the top rows that have been pushed off
00090         course[0][0] = toprow1;
00091         course[0][1] = toprow2;
00092         
00093         // game control
00094         // what to do if controller is on right
00095         
00096         if (tsi.readPercentage() >= 0.5)
00097         { // if on one side of touchpad
00098             // go to column
00099             lcd.locate(15,1);
00100             // print game character
00101             lcd.printf("%c",'<');
00102             
00103             // check whether there is collision
00104             if(course[15][1] == '0')
00105             {
00106                 // clear screen
00107                 lcd.cls();
00108                 // let user know game is over
00109                 lcd.printf("Game Over!!\n Score %d", score);
00110                 gameover = 1;
00111             }          
00112         }   
00113     
00114         // when controller is on left
00115         else
00116         {
00117             // go to column
00118             lcd.locate(15,0);
00119             // print game character
00120             lcd.printf("%c",'<');
00121             
00122             // check whether there is collision
00123             if(course[15][0] == '0')
00124             {
00125                 // clear screen
00126                 lcd.cls();
00127                 // let user know game is over
00128                 lcd.printf("Game Over!!\n Score %d", score);
00129                 gameover = 1;
00130             }
00131         }
00132     
00133         // delay between each frame    
00134         wait(frameTime);
00135         // add to steps 
00136         steps++;     
00137         // add to score
00138         score++;
00139     
00140         // check whether enough steps completed to speed game up
00141         if(steps > 5) {   
00142         frameTime -= 0.05;
00143         // reset number of steps
00144         steps = 0;
00145         }
00146     
00147         // return all variables to initial values when game is lost
00148         if(gameover == 1)
00149         {
00150         wait(10);
00151         gameover = 0;
00152         score = 0;
00153         frameTime = 1;
00154         }
00155     }
00156 }