snake game controlled by joystick

Dependencies:   N5110 beep mbed

Committer:
jasonzhou10
Date:
Mon May 11 21:47:51 2015 +0000
Revision:
0:62b17bedfb7c
Snake game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasonzhou10 0:62b17bedfb7c 1 #include "mbed.h"
jasonzhou10 0:62b17bedfb7c 2 #include "N5110.h"
jasonzhou10 0:62b17bedfb7c 3
jasonzhou10 0:62b17bedfb7c 4 /**
jasonzhou10 0:62b17bedfb7c 5 @ snake maximum body columns value can be used
jasonzhou10 0:62b17bedfb7c 6 */
jasonzhou10 0:62b17bedfb7c 7 int snakeColumns[84];
jasonzhou10 0:62b17bedfb7c 8
jasonzhou10 0:62b17bedfb7c 9 /**
jasonzhou10 0:62b17bedfb7c 10 @ snake maximum body columns value can be used
jasonzhou10 0:62b17bedfb7c 11 */
jasonzhou10 0:62b17bedfb7c 12 int snakeRows[48];
jasonzhou10 0:62b17bedfb7c 13
jasonzhou10 0:62b17bedfb7c 14 /**
jasonzhou10 0:62b17bedfb7c 15 @ all pixels were seted
jasonzhou10 0:62b17bedfb7c 16 @ param.84 - x value to add
jasonzhou10 0:62b17bedfb7c 17 @ param.48 - y value to add
jasonzhou10 0:62b17bedfb7c 18 */
jasonzhou10 0:62b17bedfb7c 19 char cells[84][48];//all pix
jasonzhou10 0:62b17bedfb7c 20
jasonzhou10 0:62b17bedfb7c 21 /**
jasonzhou10 0:62b17bedfb7c 22 @ define the structure of snake
jasonzhou10 0:62b17bedfb7c 23 @ param.x - initial x value of snake to add
jasonzhou10 0:62b17bedfb7c 24 @ param.y - initial y value of snake to add
jasonzhou10 0:62b17bedfb7c 25 @ param.headx - x-coordinate of snake head to add
jasonzhou10 0:62b17bedfb7c 26 @ param.heady - y-coordinate of snake head to add
jasonzhou10 0:62b17bedfb7c 27 */
jasonzhou10 0:62b17bedfb7c 28 typedef struct Snake Snake;//structure of snake
jasonzhou10 0:62b17bedfb7c 29 struct Snake {//structure of snake
jasonzhou10 0:62b17bedfb7c 30 int x;//initial x value of snake
jasonzhou10 0:62b17bedfb7c 31 int y;//initial y value of snake
jasonzhou10 0:62b17bedfb7c 32 int headx;//x-coordinate of snake head
jasonzhou10 0:62b17bedfb7c 33 int heady;//y-coordinate of snake tail
jasonzhou10 0:62b17bedfb7c 34 };
jasonzhou10 0:62b17bedfb7c 35
jasonzhou10 0:62b17bedfb7c 36 /**
jasonzhou10 0:62b17bedfb7c 37 @define the name of snake
jasonzhou10 0:62b17bedfb7c 38 */
jasonzhou10 0:62b17bedfb7c 39 Snake snake;//name of Snake
jasonzhou10 0:62b17bedfb7c 40
jasonzhou10 0:62b17bedfb7c 41
jasonzhou10 0:62b17bedfb7c 42 int Food_x=20;//initial x value of Food
jasonzhou10 0:62b17bedfb7c 43 int Food_y=20;//initial y value of Food
jasonzhou10 0:62b17bedfb7c 44
jasonzhou10 0:62b17bedfb7c 45 /**
jasonzhou10 0:62b17bedfb7c 46 @define the direction of snake's movement
jasonzhou10 0:62b17bedfb7c 47 */
jasonzhou10 0:62b17bedfb7c 48 int up;
jasonzhou10 0:62b17bedfb7c 49 int down;
jasonzhou10 0:62b17bedfb7c 50 int left;
jasonzhou10 0:62b17bedfb7c 51 int right;
jasonzhou10 0:62b17bedfb7c 52
jasonzhou10 0:62b17bedfb7c 53 /**
jasonzhou10 0:62b17bedfb7c 54 @initialize the value of LED when LED is not lighted
jasonzhou10 0:62b17bedfb7c 55 */
jasonzhou10 0:62b17bedfb7c 56 int LED = 0;
jasonzhou10 0:62b17bedfb7c 57
jasonzhou10 0:62b17bedfb7c 58
jasonzhou10 0:62b17bedfb7c 59 /**
jasonzhou10 0:62b17bedfb7c 60 @initialize the value of score
jasonzhou10 0:62b17bedfb7c 61 */
jasonzhou10 0:62b17bedfb7c 62 const char score=0;
jasonzhou10 0:62b17bedfb7c 63
jasonzhou10 0:62b17bedfb7c 64 /**
jasonzhou10 0:62b17bedfb7c 65 @ initialize the value of snake length and snake's life and the food appears
jasonzhou10 0:62b17bedfb7c 66 @param.snakeLength - snake length to add
jasonzhou10 0:62b17bedfb7c 67 @param.snakelife - snake is alive to add
jasonzhou10 0:62b17bedfb7c 68 @param.foodyes - food appears to add
jasonzhou10 0:62b17bedfb7c 69 */
jasonzhou10 0:62b17bedfb7c 70 int snakeLength = 6;
jasonzhou10 0:62b17bedfb7c 71 int snakelife = 1;
jasonzhou10 0:62b17bedfb7c 72 int foodyes = 1;
jasonzhou10 0:62b17bedfb7c 73 // change this to alter tolerance of joystick direction
jasonzhou10 0:62b17bedfb7c 74 #define DIRECTION_TOLERANCE 0.05
jasonzhou10 0:62b17bedfb7c 75
jasonzhou10 0:62b17bedfb7c 76 /**
jasonzhou10 0:62b17bedfb7c 77 @ connections for joystick
jasonzhou10 0:62b17bedfb7c 78 */
jasonzhou10 0:62b17bedfb7c 79 AnalogIn xPot(p15);
jasonzhou10 0:62b17bedfb7c 80 AnalogIn yPot(p16);
jasonzhou10 0:62b17bedfb7c 81 DigitalIn button(p17);
jasonzhou10 0:62b17bedfb7c 82 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
jasonzhou10 0:62b17bedfb7c 83
jasonzhou10 0:62b17bedfb7c 84 /**
jasonzhou10 0:62b17bedfb7c 85 @ timer to regularly read the joystick
jasonzhou10 0:62b17bedfb7c 86 */
jasonzhou10 0:62b17bedfb7c 87 Ticker pollJoystick;
jasonzhou10 0:62b17bedfb7c 88 /**
jasonzhou10 0:62b17bedfb7c 89 @ Serial for debug
jasonzhou10 0:62b17bedfb7c 90 */
jasonzhou10 0:62b17bedfb7c 91 Serial serial(USBTX,USBRX);
jasonzhou10 0:62b17bedfb7c 92
jasonzhou10 0:62b17bedfb7c 93 /**
jasonzhou10 0:62b17bedfb7c 94 @ create enumerated type (0,1,2,3 etc. for direction)
jasonzhou10 0:62b17bedfb7c 95 @ could be extended for diagonals etc.
jasonzhou10 0:62b17bedfb7c 96 */
jasonzhou10 0:62b17bedfb7c 97 enum DirectionName {
jasonzhou10 0:62b17bedfb7c 98 UP,
jasonzhou10 0:62b17bedfb7c 99 DOWN,
jasonzhou10 0:62b17bedfb7c 100 LEFT,
jasonzhou10 0:62b17bedfb7c 101 RIGHT,
jasonzhou10 0:62b17bedfb7c 102 CENTRE,
jasonzhou10 0:62b17bedfb7c 103 UNKNOWN
jasonzhou10 0:62b17bedfb7c 104 }
jasonzhou10 0:62b17bedfb7c 105
jasonzhou10 0:62b17bedfb7c 106 /**
jasonzhou10 0:62b17bedfb7c 107 @define the struct for Joystick
jasonzhou10 0:62b17bedfb7c 108 @ param.x - current x value to add
jasonzhou10 0:62b17bedfb7c 109 @ param.y - current y value to add
jasonzhou10 0:62b17bedfb7c 110 @ param.x0 - centred x value to add
jasonzhou10 0:62b17bedfb7c 111 @ param.y0 - centred y value to add
jasonzhou10 0:62b17bedfb7c 112 @ copied from sample code, but do not know why they have error
jasonzhou10 0:62b17bedfb7c 113 */
jasonzhou10 0:62b17bedfb7c 114 typedef struct JoyStick Joystick;
jasonzhou10 0:62b17bedfb7c 115 struct JoyStick {
jasonzhou10 0:62b17bedfb7c 116 float x; // current x value
jasonzhou10 0:62b17bedfb7c 117 float x0; // 'centred' x value
jasonzhou10 0:62b17bedfb7c 118 float y; // current y value
jasonzhou10 0:62b17bedfb7c 119 float y0; // 'centred' y value
jasonzhou10 0:62b17bedfb7c 120 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
jasonzhou10 0:62b17bedfb7c 121 DirectionName direction; // current direction
jasonzhou10 0:62b17bedfb7c 122 };
jasonzhou10 0:62b17bedfb7c 123 /**
jasonzhou10 0:62b17bedfb7c 124 @ create struct variable
jasonzhou10 0:62b17bedfb7c 125 */
jasonzhou10 0:62b17bedfb7c 126 Joystick joystick;
jasonzhou10 0:62b17bedfb7c 127
jasonzhou10 0:62b17bedfb7c 128 int printFlag = 0;
jasonzhou10 0:62b17bedfb7c 129
jasonzhou10 0:62b17bedfb7c 130 /**
jasonzhou10 0:62b17bedfb7c 131 @ function prototypes
jasonzhou10 0:62b17bedfb7c 132 */
jasonzhou10 0:62b17bedfb7c 133 void calibrateJoystick();
jasonzhou10 0:62b17bedfb7c 134 void updateJoystick();
jasonzhou10 0:62b17bedfb7c 135 void light();
jasonzhou10 0:62b17bedfb7c 136 void wholeSnake();
jasonzhou10 0:62b17bedfb7c 137 void drawSnake();
jasonzhou10 0:62b17bedfb7c 138 void drawFood();
jasonzhou10 0:62b17bedfb7c 139 void moveSnake();
jasonzhou10 0:62b17bedfb7c 140 void eatFood();
jasonzhou10 0:62b17bedfb7c 141 void crashCheck();
jasonzhou10 0:62b17bedfb7c 142 void endGame();
jasonzhou10 0:62b17bedfb7c 143
jasonzhou10 0:62b17bedfb7c 144 /**
jasonzhou10 0:62b17bedfb7c 145 @ main code
jasonzhou10 0:62b17bedfb7c 146 @ calibrateJoystick() - get centred value of joystick
jasonzhou10 0:62b17bedfb7c 147 @ read joystick 10 times per second
jasonzhou10 0:62b17bedfb7c 148 @control the joysick
jasonzhou10 0:62b17bedfb7c 149 */
jasonzhou10 0:62b17bedfb7c 150 int main()
jasonzhou10 0:62b17bedfb7c 151 {
jasonzhou10 0:62b17bedfb7c 152 calibrateJoystick(); // get centred values of joystick
jasonzhou10 0:62b17bedfb7c 153 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
jasonzhou10 0:62b17bedfb7c 154
jasonzhou10 0:62b17bedfb7c 155 while(1) {
jasonzhou10 0:62b17bedfb7c 156
jasonzhou10 0:62b17bedfb7c 157 if (printFlag) { // if flag set, clear flag and print joystick values to serial port
jasonzhou10 0:62b17bedfb7c 158 printFlag = 0;
jasonzhou10 0:62b17bedfb7c 159 serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
jasonzhou10 0:62b17bedfb7c 160
jasonzhou10 0:62b17bedfb7c 161 // check joystick direction
jasonzhou10 0:62b17bedfb7c 162 if (joystick.direction == UP)
jasonzhou10 0:62b17bedfb7c 163 serial.printf(" UP\n");
jasonzhou10 0:62b17bedfb7c 164 if (joystick.direction == DOWN)
jasonzhou10 0:62b17bedfb7c 165 serial.printf(" DOWN\n");
jasonzhou10 0:62b17bedfb7c 166 if (joystick.direction == LEFT)
jasonzhou10 0:62b17bedfb7c 167 serial.printf(" LEFT\n");
jasonzhou10 0:62b17bedfb7c 168 if (joystick.direction == RIGHT)
jasonzhou10 0:62b17bedfb7c 169 serial.printf(" RIGHT\n");
jasonzhou10 0:62b17bedfb7c 170 if (joystick.direction == CENTRE)
jasonzhou10 0:62b17bedfb7c 171 serial.printf(" CENTRE\n");
jasonzhou10 0:62b17bedfb7c 172 if (joystick.direction == UNKNOWN)
jasonzhou10 0:62b17bedfb7c 173 serial.printf(" Unsupported direction\n");
jasonzhou10 0:62b17bedfb7c 174
jasonzhou10 0:62b17bedfb7c 175 }
jasonzhou10 0:62b17bedfb7c 176
jasonzhou10 0:62b17bedfb7c 177 if (button ==0) { //button state assume pull-down used, 0 = unpressed
jasonzhou10 0:62b17bedfb7c 178 lcd. printString ("Game Start", 2,2);// print cover
jasonzhou10 0:62b17bedfb7c 179 lcd. printString ("By Troy",3,3);//print cover
jasonzhou10 0:62b17bedfb7c 180 } else {
jasonzhou10 0:62b17bedfb7c 181 lcd.clear();//function of clear screen
jasonzhou10 0:62b17bedfb7c 182 light();//function to light the led
jasonzhou10 0:62b17bedfb7c 183 drawSnake();//draw a snake
jasonzhou10 0:62b17bedfb7c 184 drawFood();//draw food
jasonzhou10 0:62b17bedfb7c 185 moveSnake();
jasonzhou10 0:62b17bedfb7c 186 eatFood();//function of checking the snake eat food
jasonzhou10 0:62b17bedfb7c 187 crashCheck();//check the collision detection
jasonzhou10 0:62b17bedfb7c 188 endGame();
jasonzhou10 0:62b17bedfb7c 189 wait(0.2);//wait 0.2 second repeat the whine loop
jasonzhou10 0:62b17bedfb7c 190
jasonzhou10 0:62b17bedfb7c 191 }
jasonzhou10 0:62b17bedfb7c 192 }
jasonzhou10 0:62b17bedfb7c 193 }
jasonzhou10 0:62b17bedfb7c 194 /**
jasonzhou10 0:62b17bedfb7c 195 @ read default positions of the joystick to calibrate later readings
jasonzhou10 0:62b17bedfb7c 196 @ initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
jasonzhou10 0:62b17bedfb7c 197 @ param.joystick.x0, value of x0 is to add
jasonzhou10 0:62b17bedfb7c 198 @ param.joystick.y0, value of y0 is to add
jasonzhou10 0:62b17bedfb7c 199 */
jasonzhou10 0:62b17bedfb7c 200 void calibrateJoystick()
jasonzhou10 0:62b17bedfb7c 201 {
jasonzhou10 0:62b17bedfb7c 202 button.mode(PullDown);
jasonzhou10 0:62b17bedfb7c 203 // must not move during calibration
jasonzhou10 0:62b17bedfb7c 204 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
jasonzhou10 0:62b17bedfb7c 205 joystick.y0 = yPot;
jasonzhou10 0:62b17bedfb7c 206 }
jasonzhou10 0:62b17bedfb7c 207
jasonzhou10 0:62b17bedfb7c 208 /**
jasonzhou10 0:62b17bedfb7c 209 @ read value from joystick
jasonzhou10 0:62b17bedfb7c 210 */
jasonzhou10 0:62b17bedfb7c 211 void updateJoystick()
jasonzhou10 0:62b17bedfb7c 212 {
jasonzhou10 0:62b17bedfb7c 213 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
jasonzhou10 0:62b17bedfb7c 214 joystick.x = xPot - joystick.x0;
jasonzhou10 0:62b17bedfb7c 215 joystick.y = yPot - joystick.y0;
jasonzhou10 0:62b17bedfb7c 216 // read button state
jasonzhou10 0:62b17bedfb7c 217 joystick.button = button;
jasonzhou10 0:62b17bedfb7c 218
jasonzhou10 0:62b17bedfb7c 219
jasonzhou10 0:62b17bedfb7c 220 /**
jasonzhou10 0:62b17bedfb7c 221 @ calculate direction depending on x,y values
jasonzhou10 0:62b17bedfb7c 222 @ tolerance allows a little lee-way in case joystick not exactly in the stated direction
jasonzhou10 0:62b17bedfb7c 223 */
jasonzhou10 0:62b17bedfb7c 224 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
jasonzhou10 0:62b17bedfb7c 225 joystick.direction = CENTRE;
jasonzhou10 0:62b17bedfb7c 226
jasonzhou10 0:62b17bedfb7c 227 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
jasonzhou10 0:62b17bedfb7c 228 joystick.direction = UP;
jasonzhou10 0:62b17bedfb7c 229 snake.y = snake.y-1;
jasonzhou10 0:62b17bedfb7c 230 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
jasonzhou10 0:62b17bedfb7c 231 joystick.direction = DOWN;
jasonzhou10 0:62b17bedfb7c 232 snake.y = snake.y+1;
jasonzhou10 0:62b17bedfb7c 233 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
jasonzhou10 0:62b17bedfb7c 234 joystick.direction = RIGHT;
jasonzhou10 0:62b17bedfb7c 235 snake.x = snake.x+1;
jasonzhou10 0:62b17bedfb7c 236 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
jasonzhou10 0:62b17bedfb7c 237 joystick.direction = LEFT;
jasonzhou10 0:62b17bedfb7c 238 snake.x = snake.x-1;
jasonzhou10 0:62b17bedfb7c 239 } else {
jasonzhou10 0:62b17bedfb7c 240 joystick.direction = UNKNOWN;
jasonzhou10 0:62b17bedfb7c 241 }
jasonzhou10 0:62b17bedfb7c 242 /**
jasonzhou10 0:62b17bedfb7c 243 @ set flag for printing
jasonzhou10 0:62b17bedfb7c 244 */
jasonzhou10 0:62b17bedfb7c 245 printFlag = 1;
jasonzhou10 0:62b17bedfb7c 246
jasonzhou10 0:62b17bedfb7c 247 lcd.init();//initialize nokia N5110
jasonzhou10 0:62b17bedfb7c 248 lcd.setBrightness(0.5); // put backlight on 50%
jasonzhou10 0:62b17bedfb7c 249 calibrateJoystick(); // get centred values of joystick
jasonzhou10 0:62b17bedfb7c 250 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
jasonzhou10 0:62b17bedfb7c 251 lcd.refresh();//reset the screen
jasonzhou10 0:62b17bedfb7c 252 }
jasonzhou10 0:62b17bedfb7c 253 /**
jasonzhou10 0:62b17bedfb7c 254 define the light function
jasonzhou10 0:62b17bedfb7c 255 */
jasonzhou10 0:62b17bedfb7c 256 void light()
jasonzhou10 0:62b17bedfb7c 257 {
jasonzhou10 0:62b17bedfb7c 258 if (button == 1)
jasonzhou10 0:62b17bedfb7c 259 LED = 1;
jasonzhou10 0:62b17bedfb7c 260 else
jasonzhou10 0:62b17bedfb7c 261 LED = 0;
jasonzhou10 0:62b17bedfb7c 262 }
jasonzhou10 0:62b17bedfb7c 263
jasonzhou10 0:62b17bedfb7c 264 /**
jasonzhou10 0:62b17bedfb7c 265 define the wholeSnake function
jasonzhou10 0:62b17bedfb7c 266 */
jasonzhou10 0:62b17bedfb7c 267 void wholeSnake()
jasonzhou10 0:62b17bedfb7c 268 {
jasonzhou10 0:62b17bedfb7c 269
jasonzhou10 0:62b17bedfb7c 270 snake.x = 5;
jasonzhou10 0:62b17bedfb7c 271 snake.y = 5;
jasonzhou10 0:62b17bedfb7c 272 snake.headx = 11;
jasonzhou10 0:62b17bedfb7c 273 snake.heady = 7;
jasonzhou10 0:62b17bedfb7c 274
jasonzhou10 0:62b17bedfb7c 275 }
jasonzhou10 0:62b17bedfb7c 276
jasonzhou10 0:62b17bedfb7c 277
jasonzhou10 0:62b17bedfb7c 278 /**
jasonzhou10 0:62b17bedfb7c 279 define the eatFood function
jasonzhou10 0:62b17bedfb7c 280 */
jasonzhou10 0:62b17bedfb7c 281 void eatFood()
jasonzhou10 0:62b17bedfb7c 282 {
jasonzhou10 0:62b17bedfb7c 283 while (1) {
jasonzhou10 0:62b17bedfb7c 284
jasonzhou10 0:62b17bedfb7c 285 if(foodyes == 1) {
jasonzhou10 0:62b17bedfb7c 286 srand(time(NULL));
jasonzhou10 0:62b17bedfb7c 287 int Food_x = rand() % 84;
jasonzhou10 0:62b17bedfb7c 288 int Food_y= rand() % 48;
jasonzhou10 0:62b17bedfb7c 289 while(Food_x%84 != 0)
jasonzhou10 0:62b17bedfb7c 290 Food_x++;
jasonzhou10 0:62b17bedfb7c 291 while(Food_y%48 != 0)
jasonzhou10 0:62b17bedfb7c 292 Food_y++;
jasonzhou10 0:62b17bedfb7c 293 }
jasonzhou10 0:62b17bedfb7c 294
jasonzhou10 0:62b17bedfb7c 295 }
jasonzhou10 0:62b17bedfb7c 296
jasonzhou10 0:62b17bedfb7c 297 /**
jasonzhou10 0:62b17bedfb7c 298 define the darwSnake function
jasonzhou10 0:62b17bedfb7c 299 */
jasonzhou10 0:62b17bedfb7c 300 void drawSnake() {
jasonzhou10 0:62b17bedfb7c 301
jasonzhou10 0:62b17bedfb7c 302 lcd.drawRect(snake.x,snake.y,6,2,1);//draw the rectangle with top left position, height, width and fill the rectangle in black
jasonzhou10 0:62b17bedfb7c 303
jasonzhou10 0:62b17bedfb7c 304 }
jasonzhou10 0:62b17bedfb7c 305
jasonzhou10 0:62b17bedfb7c 306 /**
jasonzhou10 0:62b17bedfb7c 307 define the drawFood function
jasonzhou10 0:62b17bedfb7c 308 */
jasonzhou10 0:62b17bedfb7c 309 void drawFood() { //method to define drawFood function
jasonzhou10 0:62b17bedfb7c 310
jasonzhou10 0:62b17bedfb7c 311 lcd.drawRect(Food_x,Food_y,2,2,1);//draw the rectangle with top left position, height, width and fill the rectangle in black
jasonzhou10 0:62b17bedfb7c 312
jasonzhou10 0:62b17bedfb7c 313 }
jasonzhou10 0:62b17bedfb7c 314
jasonzhou10 0:62b17bedfb7c 315 /**
jasonzhou10 0:62b17bedfb7c 316 define the moveSnake function
jasonzhou10 0:62b17bedfb7c 317 */
jasonzhou10 0:62b17bedfb7c 318 void moveSnake() { //method to define moveSnake function
jasonzhou10 0:62b17bedfb7c 319 button == 1;// button of joystick is pressed
jasonzhou10 0:62b17bedfb7c 320
jasonzhou10 0:62b17bedfb7c 321 switch (direction) {//check the direction of the joystick button is pressed
jasonzhou10 0:62b17bedfb7c 322 case up: //when the direction of joystick is up
jasonzhou10 0:62b17bedfb7c 323 snake.y = snake.y-1;//the change of y value fron joystick
jasonzhou10 0:62b17bedfb7c 324 break;
jasonzhou10 0:62b17bedfb7c 325 jump out
jasonzhou10 0:62b17bedfb7c 326
jasonzhou10 0:62b17bedfb7c 327 case down;
jasonzhou10 0:62b17bedfb7c 328 snake.y = snake.y+1;
jasonzhou10 0:62b17bedfb7c 329 break;
jasonzhou10 0:62b17bedfb7c 330
jasonzhou10 0:62b17bedfb7c 331 case left;
jasonzhou10 0:62b17bedfb7c 332 snake.x = snake.x+1;
jasonzhou10 0:62b17bedfb7c 333 break;
jasonzhou10 0:62b17bedfb7c 334
jasonzhou10 0:62b17bedfb7c 335 case right;
jasonzhou10 0:62b17bedfb7c 336 snake.x = snake.x+1;
jasonzhou10 0:62b17bedfb7c 337 break;
jasonzhou10 0:62b17bedfb7c 338 }
jasonzhou10 0:62b17bedfb7c 339 }
jasonzhou10 0:62b17bedfb7c 340
jasonzhou10 0:62b17bedfb7c 341 /**
jasonzhou10 0:62b17bedfb7c 342 define the crashCheck function
jasonzhou10 0:62b17bedfb7c 343 */
jasonzhou10 0:62b17bedfb7c 344 void crashCheck() { //method to define endGame function
jasonzhou10 0:62b17bedfb7c 345 while(1) { //infinite loop
jasonzhou10 0:62b17bedfb7c 346
jasonzhou10 0:62b17bedfb7c 347 // if snake hit itself
jasonzhou10 0:62b17bedfb7c 348 if (snake.headx == snake.x && snake.heady == snake.y) {//coordinate of snake head is equal to snake tail
jasonzhou10 0:62b17bedfb7c 349 snakelife = 0;// snake dead
jasonzhou10 0:62b17bedfb7c 350 endGame();//game end
jasonzhou10 0:62b17bedfb7c 351 }
jasonzhou10 0:62b17bedfb7c 352
jasonzhou10 0:62b17bedfb7c 353
jasonzhou10 0:62b17bedfb7c 354 //if snake hit wall
jasonzhou10 0:62b17bedfb7c 355 if (snake.x > 47 || snake.x < 0 || snake.y < 0 || snake.y > 83) {//the limit of top and bottom side
jasonzhou10 0:62b17bedfb7c 356 //the limit of left and right side
jasonzhou10 0:62b17bedfb7c 357 //check the snake head over the value of four sides
jasonzhou10 0:62b17bedfb7c 358 snakelife = 0;// snake dead
jasonzhou10 0:62b17bedfb7c 359 endGame();//game end
jasonzhou10 0:62b17bedfb7c 360
jasonzhou10 0:62b17bedfb7c 361 }
jasonzhou10 0:62b17bedfb7c 362 }
jasonzhou10 0:62b17bedfb7c 363 }
jasonzhou10 0:62b17bedfb7c 364
jasonzhou10 0:62b17bedfb7c 365 /**
jasonzhou10 0:62b17bedfb7c 366 define the endGame function
jasonzhou10 0:62b17bedfb7c 367 */
jasonzhou10 0:62b17bedfb7c 368 void endGame () { //method to define endGame function
jasonzhou10 0:62b17bedfb7c 369
jasonzhou10 0:62b17bedfb7c 370 lcd.printString ("END!", 29, 0);//the position and word"End" are printed on LCD display
jasonzhou10 0:62b17bedfb7c 371 lcd.printString ("Score: ", 10, 2);//the position and word"Score" are printed on LCD display
jasonzhou10 0:62b17bedfb7c 372 lcd.printString (score, 20, 2);//the position and number are printedon LCD display
jasonzhou10 0:62b17bedfb7c 373 wait (0.2);
jasonzhou10 0:62b17bedfb7c 374 }