Shiji Zhou
/
FinalProgram
snake game controlled by joystick
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "N5110.h" 00003 00004 /** 00005 @ snake maximum body columns value can be used 00006 */ 00007 int snakeColumns[84]; 00008 00009 /** 00010 @ snake maximum body columns value can be used 00011 */ 00012 int snakeRows[48]; 00013 00014 /** 00015 @ all pixels were seted 00016 @ param.84 - x value to add 00017 @ param.48 - y value to add 00018 */ 00019 char cells[84][48];//all pix 00020 00021 /** 00022 @ define the structure of snake 00023 @ param.x - initial x value of snake to add 00024 @ param.y - initial y value of snake to add 00025 @ param.headx - x-coordinate of snake head to add 00026 @ param.heady - y-coordinate of snake head to add 00027 */ 00028 typedef struct Snake Snake;//structure of snake 00029 struct Snake {//structure of snake 00030 int x;//initial x value of snake 00031 int y;//initial y value of snake 00032 int headx;//x-coordinate of snake head 00033 int heady;//y-coordinate of snake tail 00034 }; 00035 00036 /** 00037 @define the name of snake 00038 */ 00039 Snake snake;//name of Snake 00040 00041 00042 int Food_x=20;//initial x value of Food 00043 int Food_y=20;//initial y value of Food 00044 00045 /** 00046 @define the direction of snake's movement 00047 */ 00048 int up; 00049 int down; 00050 int left; 00051 int right; 00052 00053 /** 00054 @initialize the value of LED when LED is not lighted 00055 */ 00056 int LED = 0; 00057 00058 00059 /** 00060 @initialize the value of score 00061 */ 00062 const char score=0; 00063 00064 /** 00065 @ initialize the value of snake length and snake's life and the food appears 00066 @param.snakeLength - snake length to add 00067 @param.snakelife - snake is alive to add 00068 @param.foodyes - food appears to add 00069 */ 00070 int snakeLength = 6; 00071 int snakelife = 1; 00072 int foodyes = 1; 00073 // change this to alter tolerance of joystick direction 00074 #define DIRECTION_TOLERANCE 0.05 00075 00076 /** 00077 @ connections for joystick 00078 */ 00079 AnalogIn xPot(p15); 00080 AnalogIn yPot(p16); 00081 DigitalIn button(p17); 00082 N5110 lcd(p7,p8,p9,p10,p11,p13,p26); 00083 00084 /** 00085 @ timer to regularly read the joystick 00086 */ 00087 Ticker pollJoystick; 00088 /** 00089 @ Serial for debug 00090 */ 00091 Serial serial(USBTX,USBRX); 00092 00093 /** 00094 @ create enumerated type (0,1,2,3 etc. for direction) 00095 @ could be extended for diagonals etc. 00096 */ 00097 enum DirectionName { 00098 UP, 00099 DOWN, 00100 LEFT, 00101 RIGHT, 00102 CENTRE, 00103 UNKNOWN 00104 } 00105 00106 /** 00107 @define the struct for Joystick 00108 @ param.x - current x value to add 00109 @ param.y - current y value to add 00110 @ param.x0 - centred x value to add 00111 @ param.y0 - centred y value to add 00112 @ copied from sample code, but do not know why they have error 00113 */ 00114 typedef struct JoyStick Joystick; 00115 struct JoyStick { 00116 float x; // current x value 00117 float x0; // 'centred' x value 00118 float y; // current y value 00119 float y0; // 'centred' y value 00120 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) 00121 DirectionName direction; // current direction 00122 }; 00123 /** 00124 @ create struct variable 00125 */ 00126 Joystick joystick; 00127 00128 int printFlag = 0; 00129 00130 /** 00131 @ function prototypes 00132 */ 00133 void calibrateJoystick(); 00134 void updateJoystick(); 00135 void light(); 00136 void wholeSnake(); 00137 void drawSnake(); 00138 void drawFood(); 00139 void moveSnake(); 00140 void eatFood(); 00141 void crashCheck(); 00142 void endGame(); 00143 00144 /** 00145 @ main code 00146 @ calibrateJoystick() - get centred value of joystick 00147 @ read joystick 10 times per second 00148 @control the joysick 00149 */ 00150 int main() 00151 { 00152 calibrateJoystick(); // get centred values of joystick 00153 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second 00154 00155 while(1) { 00156 00157 if (printFlag) { // if flag set, clear flag and print joystick values to serial port 00158 printFlag = 0; 00159 serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button); 00160 00161 // check joystick direction 00162 if (joystick.direction == UP) 00163 serial.printf(" UP\n"); 00164 if (joystick.direction == DOWN) 00165 serial.printf(" DOWN\n"); 00166 if (joystick.direction == LEFT) 00167 serial.printf(" LEFT\n"); 00168 if (joystick.direction == RIGHT) 00169 serial.printf(" RIGHT\n"); 00170 if (joystick.direction == CENTRE) 00171 serial.printf(" CENTRE\n"); 00172 if (joystick.direction == UNKNOWN) 00173 serial.printf(" Unsupported direction\n"); 00174 00175 } 00176 00177 if (button ==0) { //button state assume pull-down used, 0 = unpressed 00178 lcd. printString ("Game Start", 2,2);// print cover 00179 lcd. printString ("By Troy",3,3);//print cover 00180 } else { 00181 lcd.clear();//function of clear screen 00182 light();//function to light the led 00183 drawSnake();//draw a snake 00184 drawFood();//draw food 00185 moveSnake(); 00186 eatFood();//function of checking the snake eat food 00187 crashCheck();//check the collision detection 00188 endGame(); 00189 wait(0.2);//wait 0.2 second repeat the whine loop 00190 00191 } 00192 } 00193 } 00194 /** 00195 @ read default positions of the joystick to calibrate later readings 00196 @ initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) 00197 @ param.joystick.x0, value of x0 is to add 00198 @ param.joystick.y0, value of y0 is to add 00199 */ 00200 void calibrateJoystick() 00201 { 00202 button.mode(PullDown); 00203 // must not move during calibration 00204 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) 00205 joystick.y0 = yPot; 00206 } 00207 00208 /** 00209 @ read value from joystick 00210 */ 00211 void updateJoystick() 00212 { 00213 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) 00214 joystick.x = xPot - joystick.x0; 00215 joystick.y = yPot - joystick.y0; 00216 // read button state 00217 joystick.button = button; 00218 00219 00220 /** 00221 @ calculate direction depending on x,y values 00222 @ tolerance allows a little lee-way in case joystick not exactly in the stated direction 00223 */ 00224 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { 00225 joystick.direction = CENTRE; 00226 00227 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { 00228 joystick.direction = UP; 00229 snake.y = snake.y-1; 00230 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { 00231 joystick.direction = DOWN; 00232 snake.y = snake.y+1; 00233 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { 00234 joystick.direction = RIGHT; 00235 snake.x = snake.x+1; 00236 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { 00237 joystick.direction = LEFT; 00238 snake.x = snake.x-1; 00239 } else { 00240 joystick.direction = UNKNOWN; 00241 } 00242 /** 00243 @ set flag for printing 00244 */ 00245 printFlag = 1; 00246 00247 lcd.init();//initialize nokia N5110 00248 lcd.setBrightness(0.5); // put backlight on 50% 00249 calibrateJoystick(); // get centred values of joystick 00250 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second 00251 lcd.refresh();//reset the screen 00252 } 00253 /** 00254 define the light function 00255 */ 00256 void light() 00257 { 00258 if (button == 1) 00259 LED = 1; 00260 else 00261 LED = 0; 00262 } 00263 00264 /** 00265 define the wholeSnake function 00266 */ 00267 void wholeSnake() 00268 { 00269 00270 snake.x = 5; 00271 snake.y = 5; 00272 snake.headx = 11; 00273 snake.heady = 7; 00274 00275 } 00276 00277 00278 /** 00279 define the eatFood function 00280 */ 00281 void eatFood() 00282 { 00283 while (1) { 00284 00285 if(foodyes == 1) { 00286 srand(time(NULL)); 00287 int Food_x = rand() % 84; 00288 int Food_y= rand() % 48; 00289 while(Food_x%84 != 0) 00290 Food_x++; 00291 while(Food_y%48 != 0) 00292 Food_y++; 00293 } 00294 00295 } 00296 00297 /** 00298 define the darwSnake function 00299 */ 00300 void drawSnake() { 00301 00302 lcd.drawRect(snake.x,snake.y,6,2,1);//draw the rectangle with top left position, height, width and fill the rectangle in black 00303 00304 } 00305 00306 /** 00307 define the drawFood function 00308 */ 00309 void drawFood() { //method to define drawFood function 00310 00311 lcd.drawRect(Food_x,Food_y,2,2,1);//draw the rectangle with top left position, height, width and fill the rectangle in black 00312 00313 } 00314 00315 /** 00316 define the moveSnake function 00317 */ 00318 void moveSnake() { //method to define moveSnake function 00319 button == 1;// button of joystick is pressed 00320 00321 switch (direction) {//check the direction of the joystick button is pressed 00322 case up: //when the direction of joystick is up 00323 snake.y = snake.y-1;//the change of y value fron joystick 00324 break; 00325 jump out 00326 00327 case down; 00328 snake.y = snake.y+1; 00329 break; 00330 00331 case left; 00332 snake.x = snake.x+1; 00333 break; 00334 00335 case right; 00336 snake.x = snake.x+1; 00337 break; 00338 } 00339 } 00340 00341 /** 00342 define the crashCheck function 00343 */ 00344 void crashCheck() { //method to define endGame function 00345 while(1) { //infinite loop 00346 00347 // if snake hit itself 00348 if (snake.headx == snake.x && snake.heady == snake.y) {//coordinate of snake head is equal to snake tail 00349 snakelife = 0;// snake dead 00350 endGame();//game end 00351 } 00352 00353 00354 //if snake hit wall 00355 if (snake.x > 47 || snake.x < 0 || snake.y < 0 || snake.y > 83) {//the limit of top and bottom side 00356 //the limit of left and right side 00357 //check the snake head over the value of four sides 00358 snakelife = 0;// snake dead 00359 endGame();//game end 00360 00361 } 00362 } 00363 } 00364 00365 /** 00366 define the endGame function 00367 */ 00368 void endGame () { //method to define endGame function 00369 00370 lcd.printString ("END!", 29, 0);//the position and word"End" are printed on LCD display 00371 lcd.printString ("Score: ", 10, 2);//the position and word"Score" are printed on LCD display 00372 lcd.printString (score, 20, 2);//the position and number are printedon LCD display 00373 wait (0.2); 00374 }
Generated on Fri Aug 26 2022 00:34:21 by 1.7.2