ELEC2645 (2015/16) / Mbed 2 deprecated fsmSnake

Dependencies:   N5110 mbed

Fork of fsmSnake by dean griffin

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "fsmSnakemain.h "
00003 
00004 int main()
00005 {
00006     lcd.init();
00007     wait(2); //wait for power to settle
00008     lcd.refresh();
00009     calibrateJoystick();  // get centred values of joystick
00010     pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
00011     checkJoystick.attach(&updateJoystick,0.1);
00012     moveSnake.attach(&nextMoveSnake,0.3);
00013     gameTime.attach(&gameTimer_ISR,0.3);
00014     menuTicker.attach(&menu_isr,0.3); 
00015     //button.fall(&button_isr);
00016     //gameSetup();
00017 
00018     menu(); // run the menu (while loop is contained in here)
00019   
00020 }
00021 
00022 void menu()
00023 {
00024     int y = 3; // set initial bank as 3 for the middle text
00025 
00026     while (1) { // while 1
00027         lcd.printString ("Menu",15,0); // print text
00028         lcd.printString ("Classic", 15,2);
00029         lcd.printString ("Snake 2",15,3);
00030         lcd.printString ("Maze",15,4);
00031         checkDirection();
00032         if (g_timer_flag) { // ticker 
00033             g_timer_flag = 0; // reset ticker
00034             lcd.clear(); // clear
00035 
00036             // if down, move cursor down
00037             if (joystick.direction == RIGHT) {
00038                 y++; // increase bank 
00039                 if ( y>4) { // wrap around bank (only works one way)
00040                     y = 2; 
00041                 }
00042             }
00043             // if selected, run the code highlighted...
00044             if (button) { // interrupt occured
00045 
00046                 if (y == 2) { // if first option
00047                     // run classic
00048                     classic();
00049                 } else if (y == 3) { // if second option
00050                     // run snake 2
00051                     snake2();
00052                 } else if ( y == 4) { // if third option
00053                     // run maze
00054                     maze();
00055                 }
00056             }
00057             lcd.printChar('<',75,y); // print the char to show position 
00058             lcd.refresh(); // refresh screen 
00059         }
00060         sleep(); // sleep until ticker interrupt
00061     }
00062 }
00063 // SW2 event-triggered interrupt
00064 void button_isr()
00065 {
00066     g_button_flag = 1;   // set flag in ISR
00067 }
00068 
00069 // read default positions of the joystick to calibrate later readings
00070 void calibrateJoystick()
00071 {
00072     button.mode(PullDown);
00073     // must not move during calibration
00074     joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
00075     joystick.y0 = yPot;
00076 }
00077 void updateJoystick()
00078 {
00079     // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
00080     joystick.x = xPot - joystick.x0;
00081     joystick.y = yPot - joystick.y0;
00082     // read button state
00083     joystick.button = button;
00084 
00085     // calculate direction depending on x,y values
00086     // tolerance allows a little lee-way in case joystick not exactly in the stated direction
00087     if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00088         joystick.direction = CENTRE;
00089     } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00090         joystick.direction = UP;
00091     } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00092         joystick.direction = DOWN;
00093     } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00094         joystick.direction = RIGHT;
00095     } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00096         joystick.direction = LEFT;
00097     } else {
00098         joystick.direction = UNKNOWN;
00099     }
00100 
00101     // set flag for printing
00102     printFlag = 1;
00103 }
00104 
00105 void menu_isr()
00106 {
00107     g_timer_flag = 1;
00108 }
00109 
00110 void checkDirection()
00111 {
00112         pc.baud(115200);
00113         pc.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
00114 
00115         // check joystick direction
00116         if (joystick.direction == UP)
00117             pc.printf(" UP\n");
00118         if (joystick.direction == DOWN)
00119             pc.printf(" DOWN\n");
00120         if (joystick.direction == LEFT)
00121             pc.printf(" LEFT\n");
00122         if (joystick.direction == RIGHT)
00123             pc.printf(" RIGHT\n");
00124         if (joystick.direction == CENTRE)
00125             pc.printf(" CENTRE\n");
00126         if (joystick.direction == UNKNOWN)
00127             pc.printf(" Unsupported direction\n");
00128 
00129 }
00130 
00131 
00132 void nextMoveSnake () // function to control snake with joystick
00133 {
00134     lcd.clearPixel(sx,sy);
00135 
00136     if (snakeDirection == up) { // if direction of joystick is up move head y axis down element of array to go up
00137         sy--;
00138 
00139     } else if  (snakeDirection == down) { //if direction of joystick is down move head y axis up element of array to go down
00140         sy++;
00141 
00142     }
00143 
00144     else if (snakeDirection == left) { //if direction of joystick is left move head x axis down one  element of array to go left
00145         sx++;
00146 
00147     }
00148 
00149     else if (snakeDirection == right) { // if direction of joystick is rigth, move head x axis up one element to go right
00150         sx--;
00151 
00152     }
00153 }
00154 
00155 void newDirection ()
00156 {
00157     if  (joystick.direction == UP) {
00158         snakeDirection = up;
00159 
00160     } else if  (joystick.direction == DOWN) {
00161         snakeDirection = down;
00162 
00163 
00164     }
00165 
00166     else if (joystick.direction == LEFT) {
00167         snakeDirection = left;
00168 
00169 
00170     }
00171 
00172     else if (joystick.direction == RIGHT) {
00173         snakeDirection = right;
00174     } else {
00175         snakeDirection = snakeDirection;
00176     }
00177 }
00178 
00179 
00180 void snakeTail()
00181 {
00182     int prevX = tailX[0];
00183     int prevY  = tailY[0];
00184     int prev2X, prev2Y;
00185 
00186     for( int i = 1; i < tailL; i++) {
00187         prev2X = tailX[i];
00188         prev2Y = tailY[i];
00189         tailX[i] = prevX;
00190         tailY[i] = prevY;
00191         prevX = prev2X;
00192         prevY = prev2Y;
00193 
00194         lcd.setPixel (tailX[i],tailY[i]);
00195     }
00196 }
00197 
00198 
00199 void newSnake ()
00200 {
00201     lcd.setPixel (sx,sy);
00202     lcd.refresh();
00203 }
00204 
00205 void generateFood () // function to gerneate food
00206 {
00207 
00208     if (sx == fx && sy == sx) {
00209 
00210         lcd.clearPixel(fx,fy); // remove previous food from screen
00211         printFood();
00212         score ++; // add 1 to score
00213         tailL ++; // length of snake increases
00214         lcd.refresh();
00215     }
00216 }
00217 
00218 void classic ()
00219 {
00220   
00221     if (g_gameTimer_flag == 1) {
00222         g_gameTimer_flag = 0;
00223 
00224         while(!gameOver) {
00225              gameSetup();
00226              hardWall();
00227             newDirection();
00228             nextMoveSnake();
00229             newSnake();
00230             generateFood();
00231 
00232         }
00233         sleep();
00234     }
00235 
00236 }
00237 void snake2()
00238 {
00239 
00240          
00241     while (!gameOver) {
00242 
00243         if (g_gameTimer_flag == 1) {
00244             g_gameTimer_flag = 0;
00245 
00246            
00247 
00248 
00249              gameSetup();
00250          wrapAround();
00251             snakeTail();
00252             nextMoveSnake();
00253             newSnake();
00254             generateFood();
00255 
00256         }
00257         sleep();
00258     }
00259 
00260 }
00261 
00262 void maze ()   // function to set up maze game
00263 {
00264    
00265 
00266     while (!gameOver) {
00267             checkDirection();
00268         if (g_gameTimer_flag == 1) {
00269             g_gameTimer_flag = 0;
00270              gameSetup();
00271             wrapAround();
00272             snakeTail();
00273 
00274 
00275 
00276             nextMoveSnake();
00277             newSnake();
00278             generateFood();
00279 
00280         }
00281         sleep();
00282     }
00283 }
00284 
00285 void hardWall()
00286 {
00287     lcd.drawRect(0,0,84,48,0);    // sets hardwall
00288     if ( sx > 84 || sx < 0 || sy  > 48 || sy < 0) { // if snake touches wall, snake dies
00289         game_over();
00290     }
00291 
00292 }
00293 void wrapAround () // function to let snake wrap around screen
00294 
00295 {
00296 
00297     if (sx > 84) { // if snake goes off the right of the screen
00298         sx = 0; // set back to the left side
00299     }
00300 
00301     if (sx < 0) { // if snake goes off the left of the screen
00302         sx = 84; //set back the right of the screen
00303     }
00304 
00305     if (sy < 0) { // if snake off the top of the screen
00306         sy = 48; // set snake to bottom
00307     }
00308 
00309     if (sy > 48) { // if snake goes off the bottom of the screen
00310         sy = 0; // set back to the top of the screen
00311     }
00312 
00313 
00314 }
00315 
00316 
00317 
00318 void printFood()
00319 {
00320     lcd.setPixel(fx,fy); // print food to game area
00321     lcd.clear(); // refresh screen
00322 
00323 }
00324 
00325 void gameSetup()
00326 {
00327     calibrateJoystick();  // get centred values of joystick
00328     pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
00329     checkJoystick.attach(&updateJoystick,0.1);
00330     moveSnake.attach(&nextMoveSnake,0.3);
00331     lcd.init();
00332     lcd.clear();
00333     lcd.normalMode();
00334     lcd.setBrightness(0.5);
00335     lcd.setPixel(sx,sy); // set food at random
00336     printFood();
00337 }
00338 
00339 void game_over()
00340 {
00341     lcd.printString("Game Over",15,2); // print game over to screen
00342 
00343 }
00344 
00345 void joystickTimer_isr()
00346 {
00347     g_joystickTimer_flag = 1;   // set flag in ISR
00348 
00349 }
00350 
00351 
00352 void gameTimer_ISR()
00353 {
00354     g_gameTimer_flag = 1;   // set flag in ISR
00355 }