Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #include "TSISensor.h" 00004 00005 TextLCD lcd(PTD1,PTD3,PTD2,PTD0,PTD5,PTA13); // rs, e, d4-d7 00006 // number of seconds to wait between each frame movement 00007 float frameTime = 1; 00008 // number of frames since speed increase 00009 int steps = 0; 00010 // number of frames in total 00011 int score = 0; 00012 // size of course 00013 int courseSize = 48; 00014 00015 int main() 00016 { 00017 // set up course 00018 char course[courseSize][2]; 00019 00020 // blank course to start with 00021 for(int i = 0; i < courseSize; i++) { 00022 course[i][0] = ' '; 00023 course[i][1] = ' '; 00024 } 00025 00026 // obstacles 00027 course[3][0] = '0'; 00028 course[6][1] = '0'; 00029 course[9][1] = '0'; 00030 course[12][0] = '0'; 00031 course[15][1] = '0'; 00032 course[16][1] = '0'; 00033 course[19][0] = '0'; 00034 course[21][1] = '0'; 00035 course[23][0] = '0'; 00036 course[24][0] = '0'; 00037 course[29][1] = '0'; 00038 course[33][0] = '0'; 00039 course[35][0] = '0'; 00040 course[37][0] = '0'; 00041 course[40][1] = '0'; 00042 course[43][0] = '0'; 00043 course[46][1] = '0'; 00044 00045 // set up LED output 00046 PwmOut led(LED_GREEN); 00047 // set up touch sensor to control game 00048 TSISensor tsi; 00049 00050 int gameover = 0; 00051 00052 // game loop 00053 while(1) { 00054 00055 // print current course on lcd 00056 for(int i = 15; i >= 0; i--) { 00057 lcd.locate(i,0); 00058 lcd.printf("%c",course[i][0]); 00059 lcd.locate(i,1); 00060 lcd.printf("%c",course[i][1]); 00061 } 00062 00063 00064 // create next frame of course 00065 // course loops round infinitely 00066 00067 // these are rows that will be pushed off top of screen 00068 // save them so they can be put at bottom once finished 00069 char toprow1 = course[courseSize-1][0]; 00070 char toprow2 = course[courseSize-1][1]; 00071 00072 // shift each row up 1 00073 for(int i = courseSize-1; i >= 0; i--) { 00074 00075 course[i][0] = course[i-1][0]; 00076 course[i][1] = course[i-1][1]; 00077 00078 } 00079 00080 // set bottom rows as the top rows that have been pushed off 00081 course[0][0] = toprow1; 00082 course[0][1] = toprow2; 00083 00084 // game control 00085 // what to do if controller is on right 00086 00087 if (tsi.readPercentage() >= 0.5) { // if on one side of touchpad 00088 // go to column 00089 lcd.locate(15,1); 00090 // print game character 00091 lcd.printf("%c",'<'); 00092 00093 // check whether there is collision 00094 if(course[15][1] == '0') 00095 { 00096 // clear screen 00097 lcd.cls(); 00098 // let user know game is over 00099 lcd.printf("Game Over!!\n Score %d", score); 00100 gameover = 1; 00101 } 00102 } 00103 00104 // when controller is on left 00105 else { 00106 // go to column 00107 lcd.locate(15,0); 00108 // print game character 00109 lcd.printf("%c",'<'); 00110 00111 // check whether there is collision 00112 if(course[15][0] == '0') 00113 { 00114 // clear screen 00115 lcd.cls(); 00116 // let user know game is over 00117 lcd.printf("Game Over!!\n Score %d", score); 00118 gameover = 1; 00119 } 00120 } 00121 00122 // delay between each frame 00123 wait(frameTime); 00124 // add to steps 00125 steps++; 00126 // add to score 00127 score++; 00128 // check whether enough steps completed to speed game up 00129 if(steps > 5) { 00130 frameTime -= 0.05; 00131 // reset number of steps 00132 steps = 0; 00133 } 00134 00135 if(gameover == 1) 00136 { 00137 wait(10); 00138 gameover = 0; 00139 score = 0; 00140 frameTime = 1; 00141 } 00142 } 00143 00144 }
Generated on Mon Jul 18 2022 01:21:24 by
1.7.2