renhao dai / Mbed 2 deprecated Joystick_snake_game

Dependencies:   N5110 mbed

Committer:
el14rd
Date:
Tue Apr 28 12:27:00 2015 +0000
Revision:
1:f682aeb462f1
Parent:
0:026fa541af7a
Child:
2:d7b17623ba26
snake Version 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 0:026fa541af7a 1 #include "mbed.h"
el14rd 1:f682aeb462f1 2 #include "N5110.h"
eencae 0:026fa541af7a 3 #define DIRECTION_TOLERANCE 0.05
eencae 0:026fa541af7a 4
el14rd 1:f682aeb462f1 5
el14rd 1:f682aeb462f1 6 //pins in
el14rd 1:f682aeb462f1 7 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
el14rd 1:f682aeb462f1 8
eencae 0:026fa541af7a 9 // connections for joystick
el14rd 1:f682aeb462f1 10 DigitalIn button(p19);
el14rd 1:f682aeb462f1 11 AnalogIn xPot(p15);
el14rd 1:f682aeb462f1 12 AnalogIn yPot(p16);
el14rd 1:f682aeb462f1 13
el14rd 1:f682aeb462f1 14 int i;
el14rd 1:f682aeb462f1 15 int j;
el14rd 1:f682aeb462f1 16
el14rd 1:f682aeb462f1 17 int snakeX[100];
el14rd 1:f682aeb462f1 18 int snakeY[100];
eencae 0:026fa541af7a 19
eencae 0:026fa541af7a 20 // timer to regularly read the joystick
eencae 0:026fa541af7a 21 Ticker pollJoystick;
eencae 0:026fa541af7a 22 // Serial for debug
eencae 0:026fa541af7a 23 Serial serial(USBTX,USBRX);
eencae 0:026fa541af7a 24
eencae 0:026fa541af7a 25 // create enumerated type (0,1,2,3 etc. for direction)
eencae 0:026fa541af7a 26 // could be extended for diagonals etc.
eencae 0:026fa541af7a 27 enum DirectionName {
eencae 0:026fa541af7a 28 UP,
eencae 0:026fa541af7a 29 DOWN,
eencae 0:026fa541af7a 30 LEFT,
eencae 0:026fa541af7a 31 RIGHT,
eencae 0:026fa541af7a 32 CENTRE,
eencae 0:026fa541af7a 33 UNKNOWN
eencae 0:026fa541af7a 34 };
eencae 0:026fa541af7a 35
eencae 0:026fa541af7a 36 // struct for Joystick
eencae 0:026fa541af7a 37 typedef struct JoyStick Joystick;
eencae 0:026fa541af7a 38 struct JoyStick {
eencae 0:026fa541af7a 39 float x; // current x value
eencae 0:026fa541af7a 40 float x0; // 'centred' x value
eencae 0:026fa541af7a 41 float y; // current y value
eencae 0:026fa541af7a 42 float y0; // 'centred' y value
eencae 0:026fa541af7a 43 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
eencae 0:026fa541af7a 44 DirectionName direction; // current direction
eencae 0:026fa541af7a 45 };
eencae 0:026fa541af7a 46 // create struct variable
eencae 0:026fa541af7a 47 Joystick joystick;
eencae 0:026fa541af7a 48
eencae 0:026fa541af7a 49 int printFlag = 0;
eencae 0:026fa541af7a 50
eencae 0:026fa541af7a 51 // function prototypes
el14rd 1:f682aeb462f1 52 //void setPixel(int x, int y);
el14rd 1:f682aeb462f1 53 //void clearPixel(int x, int y);
el14rd 1:f682aeb462f1 54
eencae 0:026fa541af7a 55
el14rd 1:f682aeb462f1 56 typedef struct Nut Nut;
el14rd 1:f682aeb462f1 57 struct Nut {
el14rd 1:f682aeb462f1 58 int X;
el14rd 1:f682aeb462f1 59 int Y;
el14rd 1:f682aeb462f1 60 int yes;
el14rd 1:f682aeb462f1 61 };
el14rd 1:f682aeb462f1 62 Nut nut;
eencae 0:026fa541af7a 63
el14rd 1:f682aeb462f1 64 typedef struct Snake Snake;
el14rd 1:f682aeb462f1 65 struct Snake {
el14rd 1:f682aeb462f1 66 int snakeX[100];
el14rd 1:f682aeb462f1 67 int snakeY[100];
el14rd 1:f682aeb462f1 68 int node;
el14rd 1:f682aeb462f1 69 int life;
el14rd 1:f682aeb462f1 70 DirectionName direction;
el14rd 1:f682aeb462f1 71 };
el14rd 1:f682aeb462f1 72 Snake snake;
eencae 0:026fa541af7a 73
el14rd 1:f682aeb462f1 74 DirectionName snakeDirection;
el14rd 1:f682aeb462f1 75
el14rd 1:f682aeb462f1 76 //void drawNut()
el14rd 1:f682aeb462f1 77 //{
el14rd 1:f682aeb462f1 78 // int X = rand()%84;
el14rd 1:f682aeb462f1 79 // int Y = rand()%48;
el14rd 1:f682aeb462f1 80 // lcd.drawCircle(X,Y,1,1);
el14rd 1:f682aeb462f1 81 //}
eencae 0:026fa541af7a 82
el14rd 1:f682aeb462f1 83 void moveSnake()
el14rd 1:f682aeb462f1 84 {
el14rd 1:f682aeb462f1 85
el14rd 1:f682aeb462f1 86 if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) {
el14rd 1:f682aeb462f1 87 if(joystick.direction == RIGHT&&snake.direction!=LEFT) {
el14rd 1:f682aeb462f1 88 snake.snakeX[0]-=1;
el14rd 1:f682aeb462f1 89
el14rd 1:f682aeb462f1 90 } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) {
el14rd 1:f682aeb462f1 91 snake.snakeX[0]+=1;
el14rd 1:f682aeb462f1 92 } else if(joystick.direction == UP&&snake.direction!=DOWN) {
el14rd 1:f682aeb462f1 93 snake.snakeY[0]-=1;
el14rd 1:f682aeb462f1 94 } else if(joystick.direction == DOWN&&snake.direction!=UP) {
el14rd 1:f682aeb462f1 95 snake.snakeY[0]+=1;
eencae 0:026fa541af7a 96 }
eencae 0:026fa541af7a 97
eencae 0:026fa541af7a 98 }
eencae 0:026fa541af7a 99 }
eencae 0:026fa541af7a 100
el14rd 1:f682aeb462f1 101 void drawSnake()
el14rd 1:f682aeb462f1 102 {
el14rd 1:f682aeb462f1 103
el14rd 1:f682aeb462f1 104 snake.snakeX[0] = 10; //coordinate of head
el14rd 1:f682aeb462f1 105 snake.snakeY[0] = 10;
el14rd 1:f682aeb462f1 106 snake.snakeX[1] = 11;
el14rd 1:f682aeb462f1 107 snake.snakeY[1] = 10;
el14rd 1:f682aeb462f1 108 snake.snakeX[2] = 12;
el14rd 1:f682aeb462f1 109 snake.snakeY[2] = 10;
el14rd 1:f682aeb462f1 110 snake.snakeX[3] = 13;
el14rd 1:f682aeb462f1 111 snake.snakeY[3] = 10;
el14rd 1:f682aeb462f1 112 snake.node = 5; //node of snake
el14rd 1:f682aeb462f1 113
el14rd 1:f682aeb462f1 114 for(i=0; i<snake.node-1; i++) {
el14rd 1:f682aeb462f1 115
el14rd 1:f682aeb462f1 116 lcd.setPixel(snake.snakeX[i],snake.snakeY[i]);
el14rd 1:f682aeb462f1 117 }
el14rd 1:f682aeb462f1 118 }
el14rd 1:f682aeb462f1 119
el14rd 1:f682aeb462f1 120 /*
el14rd 1:f682aeb462f1 121
el14rd 1:f682aeb462f1 122 */
el14rd 1:f682aeb462f1 123
el14rd 1:f682aeb462f1 124 void startGmae()
el14rd 1:f682aeb462f1 125 {
el14rd 1:f682aeb462f1 126 nut.yes = 1;
el14rd 1:f682aeb462f1 127 snake.life = 0;
el14rd 1:f682aeb462f1 128 snake.direction=LEFT;
el14rd 1:f682aeb462f1 129
el14rd 1:f682aeb462f1 130 //if(joystick.direction == UNKNOWN)
el14rd 1:f682aeb462f1 131 //{
el14rd 1:f682aeb462f1 132
el14rd 1:f682aeb462f1 133 if(nut.yes==1) { //set nut randomly
el14rd 1:f682aeb462f1 134 nut.X = rand()%83;
el14rd 1:f682aeb462f1 135 nut.Y = rand()%47;
el14rd 1:f682aeb462f1 136 nut.yes = 0;
el14rd 1:f682aeb462f1 137 lcd.drawCircle(nut.X,nut.Y,1,1);
el14rd 1:f682aeb462f1 138 }
el14rd 1:f682aeb462f1 139
el14rd 1:f682aeb462f1 140
el14rd 1:f682aeb462f1 141 for(i=3; i<snake.node; i++) { //if snake collide itself snake die
el14rd 1:f682aeb462f1 142 if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) {
el14rd 1:f682aeb462f1 143 snake.life=1;
el14rd 1:f682aeb462f1 144 // break;
el14rd 1:f682aeb462f1 145 }
el14rd 1:f682aeb462f1 146 }
el14rd 1:f682aeb462f1 147
el14rd 1:f682aeb462f1 148 for(i=snake.node-1; i>0; i--) { //move snake
el14rd 1:f682aeb462f1 149 snake.snakeX[i]=snake.snakeX[i-1];
el14rd 1:f682aeb462f1 150 snake.snakeY[i]=snake.snakeY[i-1];
el14rd 1:f682aeb462f1 151 }
el14rd 1:f682aeb462f1 152
el14rd 1:f682aeb462f1 153 if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) {
el14rd 1:f682aeb462f1 154 snake.life=1; // snake collide the corrider snake die
el14rd 1:f682aeb462f1 155 //break;
el14rd 1:f682aeb462f1 156 }
el14rd 1:f682aeb462f1 157
el14rd 1:f682aeb462f1 158
el14rd 1:f682aeb462f1 159 if(snake.life==1) {
el14rd 1:f682aeb462f1 160 //break;
el14rd 1:f682aeb462f1 161 }
el14rd 1:f682aeb462f1 162 if(snake.snakeX[0]==nut.X && snake.snakeY[0]==nut.Y) { // if snake got nut
el14rd 1:f682aeb462f1 163 snake.node++;
el14rd 1:f682aeb462f1 164 nut.yes=1; // new nut appear
el14rd 1:f682aeb462f1 165 }
el14rd 1:f682aeb462f1 166 //}
el14rd 1:f682aeb462f1 167
el14rd 1:f682aeb462f1 168 }
el14rd 1:f682aeb462f1 169
el14rd 1:f682aeb462f1 170
el14rd 1:f682aeb462f1 171 //while(joystick.direction == UNKNOWN) {
el14rd 1:f682aeb462f1 172
el14rd 1:f682aeb462f1 173
el14rd 1:f682aeb462f1 174 //switch(snake.direction)
el14rd 1:f682aeb462f1 175 //{
el14rd 1:f682aeb462f1 176 // case RIGHT:snake.X[0]+=1;
el14rd 1:f682aeb462f1 177 // break;    
el14rd 1:f682aeb462f1 178 // case LEFT:snake.X[0]-=1;
el14rd 1:f682aeb462f1 179 // break;     
el14rd 1:f682aeb462f1 180 // case UP:snake.Y[0]-=1;
el14rd 1:f682aeb462f1 181 // break;
el14rd 1:f682aeb462f1 182 // case DOWN:snake.Y[0]+=1;
el14rd 1:f682aeb462f1 183 // break;
el14rd 1:f682aeb462f1 184 // }
el14rd 1:f682aeb462f1 185
el14rd 1:f682aeb462f1 186 //end of while(joystick.direction = UNKNOWN)
el14rd 1:f682aeb462f1 187
el14rd 1:f682aeb462f1 188
el14rd 1:f682aeb462f1 189 // if(joystick.direction == UP&&snake.direction!=DOWN) {
el14rd 1:f682aeb462f1 190 // snake.direction=UP;
el14rd 1:f682aeb462f1 191 // } else if(joystick.direction == RIGHT&&snake.direction!=LEFT) {
el14rd 1:f682aeb462f1 192 // snake.direction= RIGHT;
el14rd 1:f682aeb462f1 193 // } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) {
el14rd 1:f682aeb462f1 194 // snake.direction=LEFT;
el14rd 1:f682aeb462f1 195 // } else(joystick.direction == DOWN&&snake.direction!=UP) {
el14rd 1:f682aeb462f1 196 // snake.direction=DOWN;
el14rd 1:f682aeb462f1 197 // }
el14rd 1:f682aeb462f1 198
el14rd 1:f682aeb462f1 199
el14rd 1:f682aeb462f1 200
eencae 0:026fa541af7a 201 // read default positions of the joystick to calibrate later readings
eencae 0:026fa541af7a 202 void calibrateJoystick()
eencae 0:026fa541af7a 203 {
eencae 0:026fa541af7a 204 button.mode(PullDown);
eencae 0:026fa541af7a 205 // must not move during calibration
eencae 0:026fa541af7a 206 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
eencae 0:026fa541af7a 207 joystick.y0 = yPot;
eencae 0:026fa541af7a 208 }
el14rd 1:f682aeb462f1 209
eencae 0:026fa541af7a 210 void updateJoystick()
eencae 0:026fa541af7a 211 {
eencae 0:026fa541af7a 212 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
eencae 0:026fa541af7a 213 joystick.x = xPot - joystick.x0;
eencae 0:026fa541af7a 214 joystick.y = yPot - joystick.y0;
eencae 0:026fa541af7a 215 // read button state
eencae 0:026fa541af7a 216 joystick.button = button;
eencae 0:026fa541af7a 217
eencae 0:026fa541af7a 218 // calculate direction depending on x,y values
eencae 0:026fa541af7a 219 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
eencae 0:026fa541af7a 220 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 221 joystick.direction = CENTRE;
eencae 0:026fa541af7a 222 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 223 joystick.direction = UP;
el14rd 1:f682aeb462f1 224 //snake.direction = UP;
eencae 0:026fa541af7a 225 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 226 joystick.direction = DOWN;
eencae 0:026fa541af7a 227 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 228 joystick.direction = RIGHT;
eencae 0:026fa541af7a 229 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 230 joystick.direction = LEFT;
eencae 0:026fa541af7a 231 } else {
eencae 0:026fa541af7a 232 joystick.direction = UNKNOWN;
eencae 0:026fa541af7a 233 }
eencae 0:026fa541af7a 234 // set flag for printing
eencae 0:026fa541af7a 235 printFlag = 1;
el14rd 1:f682aeb462f1 236 }
el14rd 1:f682aeb462f1 237
el14rd 1:f682aeb462f1 238 void updateSnake()
el14rd 1:f682aeb462f1 239 {
el14rd 1:f682aeb462f1 240 // get direction of joystick and update snake
el14rd 1:f682aeb462f1 241 if (joystick.direction == UP) {
el14rd 1:f682aeb462f1 242 snake.snakeY[0]--;
el14rd 1:f682aeb462f1 243 if(snake.snakeY[0]<1) {
el14rd 1:f682aeb462f1 244 snake.snakeY[0]=1;
el14rd 1:f682aeb462f1 245 }//will stop at the top edge
el14rd 1:f682aeb462f1 246
el14rd 1:f682aeb462f1 247 }
el14rd 1:f682aeb462f1 248 if (joystick.direction == DOWN) {
el14rd 1:f682aeb462f1 249 snake.snakeY[0]++;
el14rd 1:f682aeb462f1 250 if(snake.snakeY[0]>43) {
el14rd 1:f682aeb462f1 251 snake.snakeY[0]=43;
el14rd 1:f682aeb462f1 252 }//will stop at the bottom edge
el14rd 1:f682aeb462f1 253 }
el14rd 1:f682aeb462f1 254 if (joystick.direction == LEFT) {
el14rd 1:f682aeb462f1 255 snake.snakeX[0]--;
el14rd 1:f682aeb462f1 256 if(snake.snakeX[0]<1) {
el14rd 1:f682aeb462f1 257 snake.snakeX[0]=1;
el14rd 1:f682aeb462f1 258 }//will stop at the left edge
el14rd 1:f682aeb462f1 259
el14rd 1:f682aeb462f1 260 }
el14rd 1:f682aeb462f1 261 if (joystick.direction == RIGHT) {
el14rd 1:f682aeb462f1 262 snake.snakeX[0]++;
el14rd 1:f682aeb462f1 263 if(snake.snakeX[0]>87) {
el14rd 1:f682aeb462f1 264 snake.snakeX[0]=87;
el14rd 1:f682aeb462f1 265 }//will stop at the right edge
el14rd 1:f682aeb462f1 266
el14rd 1:f682aeb462f1 267 }
el14rd 1:f682aeb462f1 268
el14rd 1:f682aeb462f1 269 }
el14rd 1:f682aeb462f1 270
el14rd 1:f682aeb462f1 271
el14rd 1:f682aeb462f1 272 //main code
el14rd 1:f682aeb462f1 273 int main()
el14rd 1:f682aeb462f1 274 {
el14rd 1:f682aeb462f1 275 lcd.init();
el14rd 1:f682aeb462f1 276 lcd.setBrightness(0.5); // put LED backlight on 50%
el14rd 1:f682aeb462f1 277 lcd.printString("press to start",0,0);
el14rd 1:f682aeb462f1 278
el14rd 1:f682aeb462f1 279 calibrateJoystick(); // get centred values of joystick
el14rd 1:f682aeb462f1 280 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
el14rd 1:f682aeb462f1 281 lcd.refresh();
el14rd 1:f682aeb462f1 282
el14rd 1:f682aeb462f1 283 //for(i=20,i<83,i++){
el14rd 1:f682aeb462f1 284 // i=i;
el14rd 1:f682aeb462f1 285 // j=j;
el14rd 1:f682aeb462f1 286 // wait(0.5);
el14rd 1:f682aeb462f1 287 // }
el14rd 1:f682aeb462f1 288
el14rd 1:f682aeb462f1 289 //infinite while loop
el14rd 1:f682aeb462f1 290 while(1) {
el14rd 1:f682aeb462f1 291 lcd.clear();
el14rd 1:f682aeb462f1 292 updateSnake();
el14rd 1:f682aeb462f1 293 drawSnake();
el14rd 1:f682aeb462f1 294 // drawNut();
el14rd 1:f682aeb462f1 295 moveSnake();
el14rd 1:f682aeb462f1 296 startGmae();
el14rd 1:f682aeb462f1 297 lcd.refresh();
el14rd 1:f682aeb462f1 298 //wait(0.5);
el14rd 1:f682aeb462f1 299 //if(snake.life==1) {
el14rd 1:f682aeb462f1 300 // lcd.printString("lost",24,42);
el14rd 1:f682aeb462f1 301 // break;
el14rd 1:f682aeb462f1 302 }
el14rd 1:f682aeb462f1 303
el14rd 1:f682aeb462f1 304 }
el14rd 1:f682aeb462f1 305