Snake Game

Dependencies:   N5110 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "N5110.h"
00003 #define DIRECTION_TOLERANCE 0.05// change this to alter tolerance of joystick direction
00004 
00005 DigitalIn button(PTB11);// connections for joystick
00006 AnalogIn xPot(PTB2);
00007 AnalogIn yPot(PTB3);
00008 Ticker pollJoystick;// timer to regularly read the joystick
00009 // Serial for debug
00010 Serial serial(USBTX,USBRX);// Serial for debug
00011 // create enumerated type (0,1,2,3 etc. for direction)
00012 // could be extended for diagonals etc.
00013 enum DirectionName {
00014     UP,
00015     DOWN,
00016     LEFT,
00017     RIGHT,
00018     CENTRE,
00019     UNKNOWN
00020 };
00021 typedef struct JoyStick Joystick;// struct for Joystick
00022 struct JoyStick {
00023     float x;    // current x value
00024     float x0;   // 'centred' x value
00025     float y;    // current y value
00026     float y0;   // 'centred' y value
00027     int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
00028     DirectionName direction;  // current direction
00029 };
00030 Joystick joystick;// create struct variable
00031 int printFlag = 0;
00032 
00033 //        VCC,    SCE,    RST,    D/C,    MOSI,   SCLK,   LED
00034 N5110 lcd( PTE26,     PTA0,   PTC4,   PTD0,   PTD2,   PTD1,   PTC3);
00035 DigitalIn buttonA(PTB18);
00036 int ni = 84;
00037 int x = 0;
00038 int x1 = 0;
00039 int x2 = 0;
00040 int x3 = 0;
00041 int x4 = 0;
00042 int x5 = 0;
00043 int x6 = 0;
00044 int x7 = 0;
00045 int x8 = 0;
00046 int x9 = 0;
00047 int x10 = 0;
00048 int x11 = 0;
00049 int x12 = 0;
00050 int x13 = 0;
00051 int x14 = 0;
00052 int x15 = 0;
00053 int x16 = 0;
00054 int x17 = 0;
00055 int x18 = 0;
00056 int x19 = 0;
00057 int x20 = 0;
00058 int x21 = 0;
00059 int x22 = 0;
00060 int x23 = 0;
00061 int x24 = 0;
00062 int y = 0;
00063 int y1 = 1;
00064 int y2 = 2;
00065 int y3 = 3;
00066 int y4 = 4;
00067 int y5 = 5;
00068 int y6 = 6;
00069 int y7 = 7;
00070 int y8 = 8;
00071 int y9 = 9;
00072 int y10 = 10;
00073 int y11 = 11;
00074 int y12 = 12;
00075 int y13 = 13;
00076 int y14 = 14;
00077 int y15 = 15;
00078 int y16 = 16;
00079 int y17 = 17;
00080 int y18 = 18;
00081 int y19 = 19;
00082 int y20 = 20;
00083 int y21 = 21;
00084 int y22 = 22;
00085 int y23 = 23;
00086 int y24 = 24;
00087 int change = 1;
00088 int n[84][48];
00089 int array[48][84];
00090 int nj = 48;
00091 int cells[84][48];
00092 int Score = 0;
00093 int randX;
00094 int randY;
00095 char str[64];
00096 int pause=0;
00097 
00098 void Menu();  //Start screen
00099 void Welcomescreen(); //Prints Welcome to snake on screen
00100 void clearCells(); //Clears Screen
00101 void checkerBoard(); //produces checkers before game begins
00102 void DrawArray(); //Sets the pixel depending on whether 0 or higher than 1
00103 void ClearArray();//Checks over each individual pixel and if its 0 it will clear
00104 void GenerateFood();//Generates food at random places
00105 void MakeSnake(); //sets the pixels for the snake
00106 void SnakeGame(); // Game Rules
00107 void MoveSnake(); //moving the snake using the joy stick
00108 void calibrateJoystick();
00109 void updateJoystick();
00110 void SnakeGrowing();
00111 void GameOver();//Game ends
00112 void drawLine();
00113 void calibrateJoystick();
00114 void updateJoystick();
00115 
00116 int main()
00117 {
00118     buttonA.mode(PullDown);
00119     calibrateJoystick();  // get centred values of joystick
00120     pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
00121     calibrateJoystick();
00122     updateJoystick();
00123     lcd.init();
00124     Welcomescreen();
00125     lcd.clear();
00126     GenerateFood();
00127     DrawArray();
00128     Menu();
00129     while(1) {
00130 
00131         MakeSnake();
00132         ClearArray();
00133         DrawArray();
00134     }
00135 }
00136 //menu screen
00137 void Menu()
00138 {
00139     // first need to initialise display
00140 
00141     lcd.clear();
00142     lcd.printString("Press Button",6,1);
00143     lcd.printString("To",35,3);
00144     lcd.printString("START",27,5);
00145     wait(0.3);
00146 
00147     while(1) {
00148         if (buttonA==1) {
00149             lcd.clear();
00150             checkerBoard();
00151             wait(0.5);
00152             lcd.clear();
00153 
00154 
00155             while(1) {
00156 
00157                 wait(0.005);
00158                 SnakeGame();
00159                 if (buttonA==1) {
00160                     pause=0;
00161                     Menu();
00162                 }
00163 
00164                 if (button==1) {
00165                     pause=0;
00166                     lcd.clear();
00167                     sprintf(str,"%d",Score);
00168                     lcd.printString(str,37,2);
00169                     wait(1);
00170                 }
00171             }
00172         }
00173     }
00174 }
00175 
00176 void Welcomescreen()   // Display welcome screen
00177 {
00178     lcd.printString("Welcome",20,1); // display Welcome
00179     wait(0.1);
00180     lcd.printString("To",35,3); // display To
00181     wait(0.1);
00182     lcd.printString("SNAKE",27,5);  // display Snake
00183     wait(1);
00184 }
00185 
00186 void checkerBoard()
00187 {
00188 //
00189     for (int i = 0; i < ni ; i+=2) {
00190         for (int j = 0; j < nj ; j+=2) {
00191             lcd.setPixel(i,j);
00192         }
00193     }
00194     lcd.refresh();
00195 }
00196 
00197 void DrawArray()
00198 {
00199     for(int x=0; x<84; x++) {
00200         for (int y=0; y<48; y++) {
00201             if(array[y][x]>=1) {
00202                 lcd.setPixel(x,y);
00203             } else {
00204                 lcd.clearPixel(x,y);
00205             }
00206         }
00207     }
00208     lcd.refresh();
00209 }
00210 
00211 void ClearArray()
00212 {
00213     for(int x=0; x<84; x++) {
00214         for(int y=0; y<48; y++) {
00215             if (array[y][x]==0) {
00216                 lcd.clearPixel(x,y);
00217             } else {
00218                 lcd.setPixel(x,y);
00219             }
00220         }
00221     }
00222     lcd.refresh();
00223 }
00224 
00225 void calibrateJoystick()// read default positions of the joystick to calibrate later readings
00226 {
00227     button.mode(PullDown);  // must not move during calibration
00228     joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
00229     joystick.y0 = yPot;
00230 }
00231 void updateJoystick()
00232 {
00233     joystick.x = xPot - joystick.x0;// read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
00234     joystick.y = yPot - joystick.y0;// read button state
00235     joystick.button = button; // calculate direction depending on x,y values
00236     if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { // tolerance allows a little lee-way in case joystick not exactly in the stated direction
00237         joystick.direction = CENTRE;
00238     } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00239         joystick.direction = LEFT;
00240     } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00241         joystick.direction = RIGHT;
00242     } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00243         joystick.direction = DOWN;
00244     } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00245         joystick.direction = UP;
00246     } else {
00247         joystick.direction = UNKNOWN;
00248     }
00249     printFlag = 1;// set flag for printing
00250 }
00251 
00252 void GenerateFood()
00253 {
00254     randX = rand() %83; //make random number between 0 - 84
00255     randY = rand() %47; //make random number between 0 - 48
00256     while(array[randY][randX]) {
00257         randX= rand() %83;
00258         randY= rand() %47;
00259     }
00260     array[randY][randX]= 5;
00261 }
00262 void MakeSnake()
00263 {
00264     array[y+36][x+5]= 1;
00265     array[y1+36][x1+5]= 1;
00266     array[y2+36][x2+5]= 1;
00267     array[y3+36][x3+5]= 1;
00268     array[y4+36][x4+5]= 1;
00269     array[y5+36][x5+5]= 0;
00270     array[y6+36][x6+5]= 0;
00271     array[y7+36][x7+5]= 0;
00272     array[y8+36][x7+5]= 0;
00273     array[y9+36][x9+5]= 0;
00274     array[y10+36][x10+5]= 0;
00275     array[y11+36][x11+5]= 0;
00276     array[y12+36][x12+5]= 0;
00277     array[y13+36][x13+5]= 0;
00278     array[y14+36][x14+5]= 0;
00279     array[y15+36][x15+5]= 0;
00280     array[y16+36][x16+5]= 0;
00281     array[y17+36][x17+5]= 0;
00282     array[y18+36][x18+5]= 0;
00283     array[y19+36][x19+5]= 0;
00284     array[y20+36][x20+5]= 0;
00285     array[y21+36][x21+5]= 0;
00286     array[y22+36][x22+5]= 0;
00287     array[y23+36][x23+5]= 0;
00288     array[y24+36][x24+5]= 0;
00289 }
00290 
00291 void SnakeGame()
00292 {
00293     MakeSnake();
00294     ClearArray();
00295     DrawArray();
00296     SnakeGrowing();
00297     if(pause==0) {
00298         MoveSnake();
00299     }
00300     if(y+36==randY && x+5==randX) {
00301         GenerateFood();
00302 
00303         Score= Score+10;
00304     }
00305 
00306 }
00307 void MoveSnake()
00308 {
00309     array[y+36][x+5]= 0;
00310 
00311     array[y1+36][x1+5]= 0;
00312 
00313     array[y2+36][x2+5]= 0;
00314 
00315     array[y3+36][x3+5]= 0;
00316 
00317     array[y4+36][x4+5]= 0;
00318 
00319     array[y5+36][x5+5]= 0;
00320 
00321     array[y6+36][x6+5]= 0;
00322 
00323     array[y7+36][x7+5]= 0;
00324 
00325     array[y8+36][x8+5]= 0;
00326 
00327     array[y9+36][x9+5]= 0;
00328 
00329     array[y10+36][x10+5]= 0;
00330 
00331     array[y11+36][x11+5]= 0;
00332 
00333     array[y12+36][x12+5]= 0;
00334 
00335     array[y13+36][x13+5]= 0;
00336 
00337     array[y14+36][x14+5]= 0;
00338 
00339     array[y15+36][x15+5]= 0;
00340 
00341     array[y16+36][x16+5]= 0;
00342 
00343     array[y17+36][x17+5]= 0;
00344 
00345     array[y18+36][x18+5]= 0;
00346 
00347     array[y19+36][x19+5]= 0;
00348 
00349     array[y20+36][x20+5]= 0;
00350 
00351     array[y21+36][x21+5]= 0;
00352 
00353     array[y22+36][x22+5]= 0;
00354 
00355     array[y23+36][x23+5]= 0;
00356 
00357     array[y24+36][x24+5]= 0;
00358 
00359     x24=x23;
00360     y24=y23;
00361     x23=x22;
00362     y23=y22;
00363     x22=x21;
00364     y22=y21;
00365     x21=x20;
00366     y21=y20;
00367     x20=x19;
00368     y20=y19;
00369     x19=x18;
00370     y19=y18;
00371     x18=x17;
00372     y18=y17;
00373     x17=x16;
00374     y17=y16;
00375     x16=x15;
00376     y16=y15;
00377     x15=x14;
00378     y15=y14;
00379     x14=x13;
00380     y14=y13;
00381     x13=x12;
00382     y13=y12;
00383     x12=x11;
00384     y12=y11;
00385     x11=x10;
00386     y11=y10;
00387     x10=x9;
00388     y10=y9;
00389     x9=x8;
00390     y9=y8;
00391     x8=x7;
00392     y8=y7;
00393     x7=x6;
00394     y7=y6;
00395     x6=x5;
00396     y6=y5;
00397     x5=x4;
00398     y5=y4;
00399     x4=x3;
00400     y4=y3;
00401     x3=x2;
00402     y3=y2;
00403     x2=x1;
00404     y2=y1;
00405     x1 = x;
00406     y1 = y;
00407     if (joystick.direction == CENTRE) {
00408         //lcd.printString("CENTRE",20,1); // display welcome
00409         wait(0.05);
00410     } else if (joystick.direction == UP) {
00411         //lcd.printString("UP",20,1); // display welcome
00412         wait(0.05);
00413         change = 1;
00414     } else if (joystick.direction == DOWN) {
00415         //lcd.printString("DOWN",20,1); // display welcome
00416         wait(0.05);
00417         change = 2;
00418     } else if (joystick.direction == LEFT) {
00419         //lcd.printString("LEFT",20,1); // display welcome
00420         wait(0.05);
00421         change = 3;
00422     } else if (joystick.direction == RIGHT) {
00423         //lcd.printString("RIGHT",20,1); // display welcome
00424         wait(0.05);
00425         change = 4;
00426     }
00427     if (change == 1) {
00428         y--;
00429     } else if (change == 2) {
00430         y++;
00431     } else if (change == 3) {
00432         x--;
00433     } else if (change == 4) {
00434         x++;
00435     }
00436 
00437 }
00438 void SnakeGrowing()
00439 {
00440 
00441 
00442     if(Score>=10) {
00443         array[y+36][x+5]= 1;
00444 
00445         array[y1+36][x1+5]= 1;
00446 
00447         array[y2+36][x2+5]= 1;
00448 
00449         array[y3+36][x3+5]= 1;
00450 
00451         array[y4+36][x4+5]= 1;
00452 
00453         array[y5+36][x5+5]= 1;
00454 
00455         array[y6+36][x6+5]= 1;
00456 
00457     }
00458 
00459 
00460     if(Score>=20) {
00461         array[y+36][x+5]= 1;
00462 
00463         array[y1+36][x1+5]= 1;
00464 
00465         array[y2+36][x2+5]= 1;
00466 
00467         array[y3+36][x3+5]= 1;
00468 
00469         array[y4+36][x4+5]= 1;
00470 
00471         array[y5+36][x5+5]= 1;
00472 
00473         array[y6+36][x6+5]= 1;
00474 
00475         array[y7+36][x7+5]= 1;
00476 
00477         array[y8+36][x8+5]= 1;
00478     }
00479     if(Score>=30) {
00480         array[y+36][x+5]= 1;
00481 
00482         array[y1+36][x1+5]= 1;
00483 
00484         array[y2+36][x2+5]= 1;
00485 
00486         array[y3+36][x3+5]= 1;
00487 
00488         array[y4+36][x4+5]= 1;
00489 
00490         array[y5+36][x5+5]= 1;
00491 
00492         array[y6+36][x6+5]= 1;
00493 
00494         array[y7+36][x7+5]= 1;
00495 
00496         array[y8+36][x8+5]= 1;
00497 
00498         array[y9+36][x9+5]= 1;
00499 
00500         array[y10+36][x10+5]= 1;
00501 
00502     }
00503     if(Score>=40) {
00504         array[y+36][x+5]= 1;
00505 
00506         array[y1+36][x1+5]= 1;
00507 
00508         array[y2+36][x2+5]= 1;
00509 
00510         array[y3+36][x3+5]= 1;
00511 
00512         array[y4+36][x4+5]= 1;
00513 
00514         array[y5+36][x5+5]= 1;
00515 
00516         array[y6+36][x6+5]= 1;
00517 
00518         array[y7+36][x7+5]= 1;
00519 
00520         array[y8+36][x8+5]= 1;
00521 
00522         array[y9+36][x9+5]= 1;
00523 
00524         array[y10+36][x10+5]= 1;
00525 
00526         array[y11+36][x11+5]= 1;
00527 
00528         array[y12+36][x12+5]= 1;
00529 
00530     }
00531     if(Score>=50) {
00532         array[y+36][x+5]= 1;
00533 
00534         array[y1+36][x1+5]= 1;
00535 
00536         array[y2+36][x2+5]= 1;
00537 
00538         array[y3+36][x3+5]= 1;
00539 
00540         array[y4+36][x4+5]= 1;
00541 
00542         array[y5+36][x5+5]= 1;
00543 
00544         array[y6+36][x6+5]= 1;
00545 
00546         array[y7+36][x7+5]= 1;
00547 
00548         array[y8+36][x8+5]= 1;
00549 
00550         array[y9+36][x9+5]= 1;
00551 
00552         array[y10+36][x10+5]= 1;
00553 
00554         array[y11+36][x11+5]= 1;
00555 
00556         array[y12+36][x12+5]= 1;
00557 
00558         array[y13+36][x13+5]= 1;
00559 
00560         array[y14+36][x14+5]= 1;
00561 
00562     }
00563     if(Score>=60) {
00564         array[y+36][x+5]= 1;
00565 
00566         array[y1+36][x1+5]= 1;
00567 
00568         array[y2+36][x2+5]= 1;
00569 
00570         array[y3+36][x3+5]= 1;
00571 
00572         array[y4+36][x4+5]= 1;
00573 
00574         array[y5+36][x5+5]= 1;
00575 
00576         array[y6+36][x6+5]= 1;
00577 
00578         array[y7+36][x7+5]= 1;
00579 
00580         array[y8+36][x8+5]= 1;
00581 
00582         array[y9+36][x9+5]= 1;
00583 
00584         array[y10+36][x10+5]= 1;
00585 
00586         array[y11+36][x11+5]= 1;
00587 
00588         array[y12+36][x12+5]= 1;
00589 
00590         array[y13+36][x13+5]= 1;
00591 
00592         array[y14+36][x14+5]= 1;
00593 
00594         array[y15+36][x15+5]= 1;
00595 
00596         array[y16+36][x16+5]= 1;
00597 
00598     }
00599     if(Score>=70) {
00600         array[y+36][x+5]= 1;
00601 
00602         array[y1+36][x1+5]= 1;
00603 
00604         array[y2+36][x2+5]= 1;
00605 
00606         array[y3+36][x3+5]= 1;
00607 
00608         array[y4+36][x4+5]= 1;
00609 
00610         array[y5+36][x5+5]= 1;
00611 
00612         array[y6+36][x6+5]= 1;
00613 
00614         array[y7+36][x7+5]= 1;
00615 
00616         array[y8+36][x8+5]= 1;
00617 
00618         array[y9+36][x9+5]= 1;
00619 
00620         array[y10+36][x10+5]= 1;
00621 
00622         array[y11+36][x11+5]= 1;
00623 
00624         array[y12+36][x12+5]= 1;
00625 
00626         array[y13+36][x13+5]= 1;
00627 
00628         array[y14+36][x14+5]= 1;
00629 
00630         array[y15+36][x15+5]= 1;
00631 
00632         array[y16+36][x16+5]= 1;
00633 
00634         array[y17+36][x17+5]= 1;
00635 
00636         array[y18+36][x18+5]= 1;
00637 
00638     }
00639     if(Score>=80) {
00640         array[y+36][x+5]= 1;
00641 
00642         array[y1+36][x1+5]= 1;
00643 
00644         array[y2+36][x2+5]= 1;
00645 
00646         array[y3+36][x3+5]= 1;
00647 
00648         array[y4+36][x4+5]= 1;
00649 
00650         array[y5+36][x5+5]= 1;
00651 
00652         array[y6+36][x6+5]= 1;
00653 
00654         array[y7+36][x7+5]= 1;
00655 
00656         array[y8+36][x8+5]= 1;
00657 
00658         array[y9+36][x9+5]= 1;
00659 
00660         array[y10+36][x10+5]= 1;
00661 
00662         array[y11+36][x11+5]= 1;
00663 
00664         array[y12+36][x12+5]= 1;
00665 
00666         array[y13+36][x13+5]= 1;
00667 
00668         array[y14+36][x14+5]= 1;
00669 
00670         array[y15+36][x15+5]= 1;
00671 
00672         array[y16+36][x16+5]= 1;
00673 
00674         array[y17+36][x17+5]= 1;
00675 
00676         array[y18+36][x18+5]= 1;
00677 
00678         array[y19+36][x19+5]= 1;
00679 
00680         array[y20+36][x20+5]= 1;
00681 
00682     }
00683     if(Score>=90) {
00684         array[y+36][x+5]= 1;
00685 
00686         array[y1+36][x1+5]= 1;
00687 
00688         array[y2+36][x2+5]= 1;
00689 
00690         array[y3+36][x3+5]= 1;
00691 
00692         array[y4+36][x4+5]= 1;
00693 
00694         array[y5+36][x5+5]= 1;
00695 
00696         array[y6+36][x6+5]= 1;
00697 
00698         array[y7+36][x7+5]= 1;
00699 
00700         array[y8+36][x8+5]= 1;
00701 
00702         array[y9+36][x9+5]= 1;
00703 
00704         array[y10+36][x10+5]= 1;
00705 
00706         array[y11+36][x11+5]= 1;
00707 
00708         array[y12+36][x12+5]= 1;
00709 
00710         array[y13+36][x13+5]= 1;
00711 
00712         array[y14+36][x14+5]= 1;
00713 
00714         array[y15+36][x15+5]= 1;
00715 
00716         array[y16+36][x16+5]= 1;
00717 
00718         array[y17+36][x17+5]= 1;
00719 
00720         array[y18+36][x18+5]= 1;
00721 
00722         array[y19+36][x19+5]= 1;
00723 
00724         array[y20+36][x20+5]= 1;
00725 
00726         array[y21+36][x21+5]= 1;
00727 
00728         array[y22+36][x22+5]= 1;
00729 
00730     }
00731     if(Score>=100) {
00732         array[y+36][x+5]= 1;
00733 
00734         array[y1+36][x1+5]= 1;
00735 
00736         array[y2+36][x2+5]= 1;
00737 
00738         array[y3+36][x3+5]= 1;
00739 
00740         array[y4+36][x4+5]= 1;
00741 
00742         array[y5+36][x5+5]= 1;
00743 
00744         array[y6+36][x6+5]= 1;
00745 
00746         array[y7+36][x7+5]= 1;
00747 
00748         array[y8+36][x8+5]= 1;
00749 
00750         array[y9+36][x9+5]= 1;
00751 
00752         array[y10+36][x10+5]= 1;
00753 
00754         array[y11+36][x11+5]= 1;
00755 
00756         array[y12+36][x12+5]= 1;
00757 
00758         array[y13+36][x13+5]= 1;
00759 
00760         array[y14+36][x14+5]= 1;
00761 
00762         array[y15+36][x15+5]= 1;
00763 
00764         array[y16+36][x16+5]= 1;
00765 
00766         array[y17+36][x17+5]= 1;
00767 
00768         array[y18+36][x18+5]= 1;
00769 
00770         array[y19+36][x19+5]= 1;
00771 
00772         array[y20+36][x20+5]= 1;
00773 
00774         array[y21+36][x21+5]= 1;
00775 
00776         array[y22+36][x22+5]= 1;
00777 
00778         array[y23+36][x23+5]= 1;
00779 
00780         array[y24+36][x24+5]= 1;
00781 
00782     }
00783     DrawArray();
00784 
00785 
00786 
00787 }
00788 
00789 
00790 /*void GameOver()
00791 {
00792 
00793     if (button==1) {
00794 
00795         lcd.clear();
00796 
00797         lcd.printString("Welcome",20,1); // display welcome
00798         //wait(0.1);
00799         lcd.printString("To",35,3); // display To the game
00800         //wait(0.1);
00801         lcd.printString("SNAKE",27,5);  // display of life
00802         //wait(1);
00803     }
00804 }
00805 */