Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@8:b01f638d23b4, 2015-05-07 (annotated)
- Committer:
- el14rd
- Date:
- Thu May 07 16:42:37 2015 +0000
- Revision:
- 8:b01f638d23b4
- Parent:
- 7:69ae0585d11a
- Child:
- 9:c8ea9b4af873
snake version 2.1
;
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:b01f638d23b4 | 10 | //DigitalIn button(p17); |
el14rd | 8:b01f638d23b4 | 11 | //AnalogIn buttonA(p19); |
el14rd | 8:b01f638d23b4 | 12 | InterruptIn button(p17); |
el14rd | 8:b01f638d23b4 | 13 | DigitalIn buttonA(p19); |
el14rd | 1:f682aeb462f1 | 14 | AnalogIn xPot(p15); |
el14rd | 1:f682aeb462f1 | 15 | AnalogIn yPot(p16); |
el14rd | 4:41acda9d68c7 | 16 | PwmOut buzzer(p21); |
el14rd | 4:41acda9d68c7 | 17 | PwmOut blacklight(p26); |
el14rd | 4:41acda9d68c7 | 18 | PwmOut led1(p24); |
el14rd | 4:41acda9d68c7 | 19 | PwmOut led2(p23); |
el14rd | 8:b01f638d23b4 | 20 | BusOut leds(LED4,LED3,LED2,LED1); |
el14rd | 4:41acda9d68c7 | 21 | |
el14rd | 1:f682aeb462f1 | 22 | int i; |
el14rd | 1:f682aeb462f1 | 23 | int j; |
el14rd | 2:d7b17623ba26 | 24 | int X; |
el14rd | 2:d7b17623ba26 | 25 | int Y; |
el14rd | 5:d8a06e7c54fb | 26 | char buffer[14]; |
el14rd | 1:f682aeb462f1 | 27 | |
el14rd | 1:f682aeb462f1 | 28 | int snakeX[100]; |
el14rd | 1:f682aeb462f1 | 29 | int snakeY[100]; |
eencae | 0:026fa541af7a | 30 | |
eencae | 0:026fa541af7a | 31 | // timer to regularly read the joystick |
eencae | 0:026fa541af7a | 32 | Ticker pollJoystick; |
eencae | 0:026fa541af7a | 33 | // Serial for debug |
eencae | 0:026fa541af7a | 34 | Serial serial(USBTX,USBRX); |
eencae | 0:026fa541af7a | 35 | |
eencae | 0:026fa541af7a | 36 | // create enumerated type (0,1,2,3 etc. for direction) |
eencae | 0:026fa541af7a | 37 | // could be extended for diagonals etc. |
eencae | 0:026fa541af7a | 38 | enum DirectionName { |
eencae | 0:026fa541af7a | 39 | UP, |
eencae | 0:026fa541af7a | 40 | DOWN, |
eencae | 0:026fa541af7a | 41 | LEFT, |
eencae | 0:026fa541af7a | 42 | RIGHT, |
eencae | 0:026fa541af7a | 43 | CENTRE, |
eencae | 0:026fa541af7a | 44 | UNKNOWN |
eencae | 0:026fa541af7a | 45 | }; |
eencae | 0:026fa541af7a | 46 | |
eencae | 0:026fa541af7a | 47 | // struct for Joystick |
eencae | 0:026fa541af7a | 48 | typedef struct JoyStick Joystick; |
eencae | 0:026fa541af7a | 49 | struct JoyStick { |
eencae | 0:026fa541af7a | 50 | float x; // current x value |
eencae | 0:026fa541af7a | 51 | float x0; // 'centred' x value |
eencae | 0:026fa541af7a | 52 | float y; // current y value |
eencae | 0:026fa541af7a | 53 | float y0; // 'centred' y value |
eencae | 0:026fa541af7a | 54 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
eencae | 0:026fa541af7a | 55 | DirectionName direction; // current direction |
eencae | 0:026fa541af7a | 56 | }; |
eencae | 0:026fa541af7a | 57 | // create struct variable |
eencae | 0:026fa541af7a | 58 | Joystick joystick; |
eencae | 0:026fa541af7a | 59 | |
el14rd | 8:b01f638d23b4 | 60 | //int buttonA = 0; |
el14rd | 8:b01f638d23b4 | 61 | int pause = 0; |
el14rd | 8:b01f638d23b4 | 62 | int buttonAFlag=0; |
el14rd | 7:69ae0585d11a | 63 | int buttonFlag = 0; |
eencae | 0:026fa541af7a | 64 | int printFlag = 0; |
el14rd | 3:04ff8cd27dd1 | 65 | int nutFlag =1; |
el14rd | 5:d8a06e7c54fb | 66 | int lifeFlag =0; |
el14rd | 8:b01f638d23b4 | 67 | //float frequency=659; |
el14rd | 8:b01f638d23b4 | 68 | float frequency[]= {659,554,659,554,550,494,554,587,494,659,554,440}; |
el14rd | 8:b01f638d23b4 | 69 | float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; |
eencae | 0:026fa541af7a | 70 | // function prototypes |
el14rd | 1:f682aeb462f1 | 71 | //void setPixel(int x, int y); |
el14rd | 1:f682aeb462f1 | 72 | //void clearPixel(int x, int y); |
el14rd | 1:f682aeb462f1 | 73 | |
eencae | 0:026fa541af7a | 74 | |
el14rd | 1:f682aeb462f1 | 75 | typedef struct Nut Nut; |
el14rd | 1:f682aeb462f1 | 76 | struct Nut { |
el14rd | 1:f682aeb462f1 | 77 | int X; |
el14rd | 1:f682aeb462f1 | 78 | int Y; |
el14rd | 1:f682aeb462f1 | 79 | }; |
el14rd | 1:f682aeb462f1 | 80 | Nut nut; |
eencae | 0:026fa541af7a | 81 | |
el14rd | 1:f682aeb462f1 | 82 | typedef struct Snake Snake; |
el14rd | 1:f682aeb462f1 | 83 | struct Snake { |
el14rd | 1:f682aeb462f1 | 84 | int snakeX[100]; |
el14rd | 1:f682aeb462f1 | 85 | int snakeY[100]; |
el14rd | 1:f682aeb462f1 | 86 | int node; |
el14rd | 1:f682aeb462f1 | 87 | DirectionName direction; |
el14rd | 1:f682aeb462f1 | 88 | }; |
el14rd | 1:f682aeb462f1 | 89 | Snake snake; |
eencae | 0:026fa541af7a | 90 | |
el14rd | 1:f682aeb462f1 | 91 | DirectionName snakeDirection; |
el14rd | 1:f682aeb462f1 | 92 | |
el14rd | 2:d7b17623ba26 | 93 | void initNut() |
el14rd | 2:d7b17623ba26 | 94 | { |
el14rd | 3:04ff8cd27dd1 | 95 | nut.X=rand()%84; |
el14rd | 3:04ff8cd27dd1 | 96 | nut.Y=rand()%48; |
el14rd | 3:04ff8cd27dd1 | 97 | //nutFlag=1; |
el14rd | 8:b01f638d23b4 | 98 | } |
el14rd | 8:b01f638d23b4 | 99 | |
el14rd | 2:d7b17623ba26 | 100 | void drawNut() |
el14rd | 2:d7b17623ba26 | 101 | { |
el14rd | 3:04ff8cd27dd1 | 102 | //int X = rand()%84; |
el14rd | 3:04ff8cd27dd1 | 103 | //int Y = rand()%48; |
el14rd | 8:b01f638d23b4 | 104 | if(nutFlag==1) { |
el14rd | 8:b01f638d23b4 | 105 | lcd.drawCircle(nut.X,nut.Y,1,1); |
el14rd | 3:04ff8cd27dd1 | 106 | } |
el14rd | 2:d7b17623ba26 | 107 | } |
eencae | 0:026fa541af7a | 108 | |
el14rd | 1:f682aeb462f1 | 109 | void moveSnake() |
el14rd | 1:f682aeb462f1 | 110 | { |
el14rd | 8:b01f638d23b4 | 111 | |
el14rd | 8:b01f638d23b4 | 112 | if(joystick.direction == CENTRE) { |
el14rd | 4:41acda9d68c7 | 113 | //snake.snakeX[0]+=1; |
el14rd | 4:41acda9d68c7 | 114 | joystick.direction = CENTRE; |
el14rd | 3:04ff8cd27dd1 | 115 | } |
el14rd | 4:41acda9d68c7 | 116 | //else if(joystick.button == button){ |
el14rd | 8:b01f638d23b4 | 117 | // snake.snakeX[i]=snake.snakeX[i]; |
el14rd | 8:b01f638d23b4 | 118 | // snake.snakeY[i]=snake.snakeY[i]; |
el14rd | 8:b01f638d23b4 | 119 | // } |
el14rd | 8:b01f638d23b4 | 120 | else if(joystick.direction == UNKNOWN) { |
el14rd | 8:b01f638d23b4 | 121 | joystick.direction = UNKNOWN; |
el14rd | 8:b01f638d23b4 | 122 | } else if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) { |
el14rd | 2:d7b17623ba26 | 123 | |
el14rd | 4:41acda9d68c7 | 124 | if((joystick.direction == DOWN)&&(snake.direction!=LEFT)) { |
el14rd | 2:d7b17623ba26 | 125 | snake.direction= RIGHT; |
el14rd | 4:41acda9d68c7 | 126 | //snake.snakeX[0]+=1; |
el14rd | 8:b01f638d23b4 | 127 | |
el14rd | 4:41acda9d68c7 | 128 | } else if((joystick.direction == UP)&&(snake.direction!=RIGHT)) { |
el14rd | 2:d7b17623ba26 | 129 | snake.direction=LEFT; |
el14rd | 4:41acda9d68c7 | 130 | //snake.snakeX[0]-=1; |
el14rd | 4:41acda9d68c7 | 131 | } else if((joystick.direction == LEFT)&&(snake.direction!=DOWN)) { |
el14rd | 5:d8a06e7c54fb | 132 | snake.direction=UP; |
el14rd | 4:41acda9d68c7 | 133 | //snake.snakeY[0]-=1; |
el14rd | 4:41acda9d68c7 | 134 | } else if((joystick.direction == RIGHT)&&(snake.direction!=UP)) { |
el14rd | 5:d8a06e7c54fb | 135 | snake.direction=DOWN; |
el14rd | 4:41acda9d68c7 | 136 | //snake.snakeY[0]+=1; |
eencae | 0:026fa541af7a | 137 | } |
eencae | 0:026fa541af7a | 138 | |
eencae | 0:026fa541af7a | 139 | } |
el14rd | 8:b01f638d23b4 | 140 | |
el14rd | 2:d7b17623ba26 | 141 | } |
el14rd | 2:d7b17623ba26 | 142 | |
el14rd | 2:d7b17623ba26 | 143 | void initSnake() |
el14rd | 2:d7b17623ba26 | 144 | { |
el14rd | 8:b01f638d23b4 | 145 | |
el14rd | 5:d8a06e7c54fb | 146 | snake.snakeX[0] = 16; //coordinate of head |
el14rd | 2:d7b17623ba26 | 147 | snake.snakeY[0] = 10; |
el14rd | 5:d8a06e7c54fb | 148 | snake.snakeX[1] = 14; |
el14rd | 2:d7b17623ba26 | 149 | snake.snakeY[1] = 10; |
el14rd | 4:41acda9d68c7 | 150 | snake.snakeX[2] = 12; |
el14rd | 2:d7b17623ba26 | 151 | snake.snakeY[2] = 10; |
el14rd | 5:d8a06e7c54fb | 152 | snake.snakeX[3] = 10; |
el14rd | 2:d7b17623ba26 | 153 | snake.snakeY[3] = 10; |
el14rd | 5:d8a06e7c54fb | 154 | snake.node = 6; //node of snake |
el14rd | 4:41acda9d68c7 | 155 | snake.direction = RIGHT; |
el14rd | 8:b01f638d23b4 | 156 | |
el14rd | 5:d8a06e7c54fb | 157 | } |
el14rd | 8:b01f638d23b4 | 158 | |
el14rd | 8:b01f638d23b4 | 159 | |
el14rd | 1:f682aeb462f1 | 160 | void drawSnake() |
el14rd | 8:b01f638d23b4 | 161 | { |
el14rd | 8:b01f638d23b4 | 162 | |
el14rd | 8:b01f638d23b4 | 163 | for(i=0; i<snake.node-1; i++) { |
el14rd | 5:d8a06e7c54fb | 164 | lcd.drawCircle(snake.snakeX[i],snake.snakeY[i],1,1); |
el14rd | 5:d8a06e7c54fb | 165 | //lcd.setPixel(snake.snakeX[i],snake.snakeY[i]); |
el14rd | 8:b01f638d23b4 | 166 | |
el14rd | 8:b01f638d23b4 | 167 | } |
el14rd | 8:b01f638d23b4 | 168 | |
el14rd | 1:f682aeb462f1 | 169 | } |
el14rd | 1:f682aeb462f1 | 170 | |
el14rd | 1:f682aeb462f1 | 171 | void startGmae() |
el14rd | 1:f682aeb462f1 | 172 | { |
el14rd | 8:b01f638d23b4 | 173 | //nutFlag = 1; |
el14rd | 5:d8a06e7c54fb | 174 | //lifeFlag = 0; |
el14rd | 2:d7b17623ba26 | 175 | //snake.direction=LEFT; |
el14rd | 8:b01f638d23b4 | 176 | |
el14rd | 6:290173d4a22d | 177 | if((snake.snakeX[0]==nut.X)&&(snake.snakeY[0]==nut.Y)) { // if snake got nut ||(snake.snakeX[0]==nut.X-1)&&(snake.snakeY[0]==nut.Y-1)||(snake.snakeY[0]==nut.Y+1)&&(snake.snakeY[0]==nut.Y+1 |
el14rd | 8:b01f638d23b4 | 178 | snake.node+=1; |
el14rd | 8:b01f638d23b4 | 179 | nutFlag=0; // new nut appear |
el14rd | 8:b01f638d23b4 | 180 | buzzer.period(1/frequency[1]); // set PWM period |
el14rd | 8:b01f638d23b4 | 181 | buzzer=0.1; // set duty cycle |
el14rd | 3:04ff8cd27dd1 | 182 | } |
el14rd | 8:b01f638d23b4 | 183 | |
el14rd | 8:b01f638d23b4 | 184 | if((snake.snakeX[0]!=nut.X)||(snake.snakeY[0]!=nut.Y)) { |
el14rd | 8:b01f638d23b4 | 185 | buzzer=0; |
el14rd | 8:b01f638d23b4 | 186 | } |
el14rd | 8:b01f638d23b4 | 187 | |
el14rd | 3:04ff8cd27dd1 | 188 | if(nutFlag==0) { //set nut randomly |
el14rd | 1:f682aeb462f1 | 189 | nut.X = rand()%83; |
el14rd | 1:f682aeb462f1 | 190 | nut.Y = rand()%47; |
el14rd | 3:04ff8cd27dd1 | 191 | nutFlag = 1; |
el14rd | 1:f682aeb462f1 | 192 | } |
el14rd | 2:d7b17623ba26 | 193 | |
el14rd | 8:b01f638d23b4 | 194 | |
el14rd | 8:b01f638d23b4 | 195 | for(i=3; i<snake.node; i++) { //if snake collide itself snake die |
el14rd | 8:b01f638d23b4 | 196 | if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) { |
el14rd | 8:b01f638d23b4 | 197 | lifeFlag=1; |
el14rd | 7:69ae0585d11a | 198 | } |
el14rd | 8:b01f638d23b4 | 199 | } |
el14rd | 8:b01f638d23b4 | 200 | |
el14rd | 8:b01f638d23b4 | 201 | |
el14rd | 8:b01f638d23b4 | 202 | for(i=snake.node-1; i>0; i--) { //move snake |
el14rd | 8:b01f638d23b4 | 203 | if(snake.snakeX[0]>1||snake.snakeX[0]<83||snake.snakeY[0]>1||snake.snakeY[0]<47) { |
el14rd | 8:b01f638d23b4 | 204 | snake.snakeX[i]=snake.snakeX[i-1]; |
el14rd | 8:b01f638d23b4 | 205 | snake.snakeY[i]=snake.snakeY[i-1]; |
el14rd | 8:b01f638d23b4 | 206 | } else { |
el14rd | 8:b01f638d23b4 | 207 | snake.snakeX[i]=snake.snakeX[i]; |
el14rd | 8:b01f638d23b4 | 208 | snake.snakeY[i]=snake.snakeY[i]; |
el14rd | 8:b01f638d23b4 | 209 | } |
el14rd | 8:b01f638d23b4 | 210 | } |
el14rd | 8:b01f638d23b4 | 211 | |
el14rd | 1:f682aeb462f1 | 212 | |
el14rd | 1:f682aeb462f1 | 213 | if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) { |
el14rd | 5:d8a06e7c54fb | 214 | lifeFlag=1; // snake collide the corrider snake die |
el14rd | 1:f682aeb462f1 | 215 | } |
el14rd | 8:b01f638d23b4 | 216 | |
el14rd | 1:f682aeb462f1 | 217 | } |
el14rd | 1:f682aeb462f1 | 218 | |
el14rd | 8:b01f638d23b4 | 219 | void printScore() |
el14rd | 8:b01f638d23b4 | 220 | { |
el14rd | 6:290173d4a22d | 221 | lcd.printString("Your Score is",0,0); |
el14rd | 6:290173d4a22d | 222 | int score=snake.node-6; |
el14rd | 6:290173d4a22d | 223 | int length = sprintf(buffer," SCORE=%2d ",score); |
el14rd | 8:b01f638d23b4 | 224 | lcd.printString(buffer,0,2 ); |
el14rd | 6:290173d4a22d | 225 | lcd.printString("Press to start",0,4); |
el14rd | 8:b01f638d23b4 | 226 | } |
el14rd | 7:69ae0585d11a | 227 | |
el14rd | 8:b01f638d23b4 | 228 | void buttonPressed() |
el14rd | 8:b01f638d23b4 | 229 | { |
el14rd | 8:b01f638d23b4 | 230 | buttonFlag =1; |
el14rd | 7:69ae0585d11a | 231 | } |
el14rd | 7:69ae0585d11a | 232 | |
el14rd | 8:b01f638d23b4 | 233 | void snakeModel() |
el14rd | 8:b01f638d23b4 | 234 | { |
el14rd | 8:b01f638d23b4 | 235 | for(int i=0; i<25; i+=5) { |
el14rd | 8:b01f638d23b4 | 236 | lcd.clear(); |
el14rd | 8:b01f638d23b4 | 237 | lcd.printString("Press to start",1,1); |
el14rd | 8:b01f638d23b4 | 238 | |
el14rd | 8:b01f638d23b4 | 239 | lcd.drawRect(44-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 240 | lcd.drawRect(46-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 241 | lcd.drawRect(48-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 242 | |
el14rd | 8:b01f638d23b4 | 243 | lcd.drawRect(42-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 244 | lcd.drawRect(44-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 245 | lcd.drawRect(46-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 246 | lcd.drawRect(48-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 247 | |
el14rd | 8:b01f638d23b4 | 248 | lcd.drawRect(40-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 249 | lcd.drawRect(42-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 250 | lcd.drawRect(44-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 251 | lcd.drawRect(46-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 252 | lcd.drawRect(48-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 253 | lcd.drawRect(50-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 254 | |
el14rd | 8:b01f638d23b4 | 255 | lcd.drawRect(40-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 256 | lcd.drawRect(42-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 257 | lcd.drawRect(44-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 258 | lcd.drawRect(46-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 259 | lcd.drawRect(48-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 260 | lcd.drawRect(50-i,27,1,1,1);//head |
el14rd | 8:b01f638d23b4 | 261 | |
el14rd | 8:b01f638d23b4 | 262 | lcd.drawRect(38-i,29,1,1,0); //tougue |
el14rd | 8:b01f638d23b4 | 263 | lcd.drawRect(40-i,31,1,1,0); |
el14rd | 8:b01f638d23b4 | 264 | |
el14rd | 8:b01f638d23b4 | 265 | lcd.drawRect(46-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 266 | lcd.drawRect(48-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 267 | lcd.drawRect(50-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 268 | |
el14rd | 8:b01f638d23b4 | 269 | lcd.drawRect(46-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 270 | lcd.drawRect(48-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 271 | lcd.drawRect(50-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 272 | |
el14rd | 8:b01f638d23b4 | 273 | lcd.drawRect(44-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 274 | lcd.drawRect(46-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 275 | lcd.drawRect(48-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 276 | |
el14rd | 8:b01f638d23b4 | 277 | lcd.drawRect(44-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 278 | lcd.drawRect(46-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 279 | lcd.drawRect(48-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 280 | |
el14rd | 8:b01f638d23b4 | 281 | lcd.drawRect(42-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 282 | lcd.drawRect(44-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 283 | lcd.drawRect(46-i,37,1,1,1); |
el14rd | 6:290173d4a22d | 284 | |
el14rd | 8:b01f638d23b4 | 285 | lcd.drawRect(42-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 286 | lcd.drawRect(44-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 287 | lcd.drawRect(46-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 288 | |
el14rd | 8:b01f638d23b4 | 289 | lcd.drawRect(44-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 290 | lcd.drawRect(46-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 291 | lcd.drawRect(48-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 292 | lcd.drawRect(50-i,41,1,1,1);//neck |
el14rd | 8:b01f638d23b4 | 293 | |
el14rd | 8:b01f638d23b4 | 294 | lcd.drawRect(80-i,41,1,1,1);//tail |
el14rd | 8:b01f638d23b4 | 295 | lcd.drawRect(82-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 296 | lcd.drawRect(84-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 297 | |
el14rd | 8:b01f638d23b4 | 298 | lcd.drawRect(82-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 299 | lcd.drawRect(84-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 300 | |
el14rd | 8:b01f638d23b4 | 301 | lcd.drawRect(82-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 302 | |
el14rd | 8:b01f638d23b4 | 303 | lcd.drawRect(80-i,35,1,1,1);//tail |
el14rd | 8:b01f638d23b4 | 304 | |
el14rd | 8:b01f638d23b4 | 305 | lcd.drawRect(46-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 306 | lcd.drawRect(48-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 307 | lcd.drawRect(50-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 308 | lcd.drawRect(52-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 309 | lcd.drawRect(54-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 310 | lcd.drawRect(56-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 311 | lcd.drawRect(58-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 312 | lcd.drawRect(60-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 313 | lcd.drawRect(62-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 314 | lcd.drawRect(64-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 315 | lcd.drawRect(66-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 316 | lcd.drawRect(68-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 317 | lcd.drawRect(70-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 318 | lcd.drawRect(72-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 319 | lcd.drawRect(74-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 320 | lcd.drawRect(76-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 321 | lcd.drawRect(78-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 322 | lcd.drawRect(80-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 323 | lcd.drawRect(82-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 324 | |
el14rd | 8:b01f638d23b4 | 325 | lcd.drawRect(48-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 326 | lcd.drawRect(50-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 327 | lcd.drawRect(52-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 328 | lcd.drawRect(54-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 329 | lcd.drawRect(56-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 330 | lcd.drawRect(58-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 331 | lcd.drawRect(60-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 332 | lcd.drawRect(62-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 333 | lcd.drawRect(64-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 334 | lcd.drawRect(66-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 335 | lcd.drawRect(68-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 336 | lcd.drawRect(70-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 337 | lcd.drawRect(72-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 338 | lcd.drawRect(74-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 339 | lcd.drawRect(76-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 340 | lcd.drawRect(78-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 341 | lcd.drawRect(80-i,45,1,1,1); |
el14rd | 7:69ae0585d11a | 342 | wait(0.5); |
el14rd | 8:b01f638d23b4 | 343 | //lcd.refresh(); |
el14rd | 7:69ae0585d11a | 344 | } |
el14rd | 8:b01f638d23b4 | 345 | |
el14rd | 8:b01f638d23b4 | 346 | } |
el14rd | 1:f682aeb462f1 | 347 | //switch(snake.direction) |
el14rd | 1:f682aeb462f1 | 348 | //{ |
el14rd | 1:f682aeb462f1 | 349 | // case RIGHT:snake.X[0]+=1; |
el14rd | 1:f682aeb462f1 | 350 | // break; |
el14rd | 1:f682aeb462f1 | 351 | // case LEFT:snake.X[0]-=1; |
el14rd | 1:f682aeb462f1 | 352 | // break; |
el14rd | 1:f682aeb462f1 | 353 | // case UP:snake.Y[0]-=1; |
el14rd | 1:f682aeb462f1 | 354 | // break; |
el14rd | 1:f682aeb462f1 | 355 | // case DOWN:snake.Y[0]+=1; |
el14rd | 1:f682aeb462f1 | 356 | // break; |
el14rd | 1:f682aeb462f1 | 357 | // } |
el14rd | 1:f682aeb462f1 | 358 | |
el14rd | 1:f682aeb462f1 | 359 | //end of while(joystick.direction = UNKNOWN) |
el14rd | 1:f682aeb462f1 | 360 | |
el14rd | 1:f682aeb462f1 | 361 | |
eencae | 0:026fa541af7a | 362 | // read default positions of the joystick to calibrate later readings |
eencae | 0:026fa541af7a | 363 | void calibrateJoystick() |
eencae | 0:026fa541af7a | 364 | { |
eencae | 0:026fa541af7a | 365 | button.mode(PullDown); |
eencae | 0:026fa541af7a | 366 | // must not move during calibration |
eencae | 0:026fa541af7a | 367 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
eencae | 0:026fa541af7a | 368 | joystick.y0 = yPot; |
eencae | 0:026fa541af7a | 369 | } |
el14rd | 1:f682aeb462f1 | 370 | |
el14rd | 2:d7b17623ba26 | 371 | |
eencae | 0:026fa541af7a | 372 | void updateJoystick() |
eencae | 0:026fa541af7a | 373 | { |
eencae | 0:026fa541af7a | 374 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
eencae | 0:026fa541af7a | 375 | joystick.x = xPot - joystick.x0; |
eencae | 0:026fa541af7a | 376 | joystick.y = yPot - joystick.y0; |
eencae | 0:026fa541af7a | 377 | // read button state |
eencae | 0:026fa541af7a | 378 | joystick.button = button; |
eencae | 0:026fa541af7a | 379 | |
eencae | 0:026fa541af7a | 380 | // calculate direction depending on x,y values |
eencae | 0:026fa541af7a | 381 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
eencae | 0:026fa541af7a | 382 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 383 | joystick.direction = CENTRE; |
el14rd | 4:41acda9d68c7 | 384 | //LED OFF |
eencae | 0:026fa541af7a | 385 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 386 | joystick.direction = UP; |
el14rd | 4:41acda9d68c7 | 387 | //LIGHT LED1 |
el14rd | 1:f682aeb462f1 | 388 | //snake.direction = UP; |
eencae | 0:026fa541af7a | 389 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 390 | joystick.direction = DOWN; |
el14rd | 4:41acda9d68c7 | 391 | //LIGHT LED2 |
eencae | 0:026fa541af7a | 392 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 393 | joystick.direction = RIGHT; |
el14rd | 4:41acda9d68c7 | 394 | //LIGHT LED3 |
eencae | 0:026fa541af7a | 395 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 396 | joystick.direction = LEFT; |
eencae | 0:026fa541af7a | 397 | } else { |
eencae | 0:026fa541af7a | 398 | joystick.direction = UNKNOWN; |
eencae | 0:026fa541af7a | 399 | } |
eencae | 0:026fa541af7a | 400 | // set flag for printing |
eencae | 0:026fa541af7a | 401 | printFlag = 1; |
el14rd | 1:f682aeb462f1 | 402 | } |
el14rd | 1:f682aeb462f1 | 403 | |
el14rd | 1:f682aeb462f1 | 404 | void updateSnake() |
el14rd | 1:f682aeb462f1 | 405 | { |
el14rd | 1:f682aeb462f1 | 406 | // get direction of joystick and update snake |
el14rd | 4:41acda9d68c7 | 407 | if (snake.direction == LEFT) { |
el14rd | 7:69ae0585d11a | 408 | snake.snakeX[0]-=1; |
el14rd | 4:41acda9d68c7 | 409 | //LED1 |
el14rd | 4:41acda9d68c7 | 410 | if(snake.snakeX[0]<1) { |
el14rd | 4:41acda9d68c7 | 411 | snake.snakeX[0]=1; |
el14rd | 8:b01f638d23b4 | 412 | |
el14rd | 1:f682aeb462f1 | 413 | }//will stop at the top edge |
el14rd | 1:f682aeb462f1 | 414 | |
el14rd | 1:f682aeb462f1 | 415 | } |
el14rd | 4:41acda9d68c7 | 416 | if (snake.direction == RIGHT) { |
el14rd | 7:69ae0585d11a | 417 | snake.snakeX[0]+=1; |
el14rd | 4:41acda9d68c7 | 418 | //LED2 |
el14rd | 4:41acda9d68c7 | 419 | if(snake.snakeX[0]>83) { |
el14rd | 4:41acda9d68c7 | 420 | snake.snakeX[0]=83; |
el14rd | 1:f682aeb462f1 | 421 | }//will stop at the bottom edge |
el14rd | 1:f682aeb462f1 | 422 | } |
el14rd | 4:41acda9d68c7 | 423 | if (snake.direction == UP) { |
el14rd | 7:69ae0585d11a | 424 | snake.snakeY[0]-=1; |
el14rd | 4:41acda9d68c7 | 425 | if(snake.snakeY[0]>47) { |
el14rd | 4:41acda9d68c7 | 426 | snake.snakeY[0]=47; |
el14rd | 1:f682aeb462f1 | 427 | }//will stop at the left edge |
el14rd | 1:f682aeb462f1 | 428 | |
el14rd | 1:f682aeb462f1 | 429 | } |
el14rd | 4:41acda9d68c7 | 430 | if (snake.direction == DOWN) { |
el14rd | 7:69ae0585d11a | 431 | snake.snakeY[0]+=1; |
el14rd | 4:41acda9d68c7 | 432 | if(snake.snakeY[0]<1) { |
el14rd | 4:41acda9d68c7 | 433 | snake.snakeY[0]=1; |
el14rd | 1:f682aeb462f1 | 434 | }//will stop at the right edge |
el14rd | 1:f682aeb462f1 | 435 | |
el14rd | 1:f682aeb462f1 | 436 | } |
el14rd | 8:b01f638d23b4 | 437 | if(snake.direction == CENTRE) { |
el14rd | 4:41acda9d68c7 | 438 | snake.snakeX[0]+=1; |
el14rd | 8:b01f638d23b4 | 439 | } |
el14rd | 8:b01f638d23b4 | 440 | if(snake.direction == UNKNOWN) { |
el14rd | 5:d8a06e7c54fb | 441 | snake.snakeX[0]+=1; |
el14rd | 8:b01f638d23b4 | 442 | } |
el14rd | 1:f682aeb462f1 | 443 | } |
el14rd | 1:f682aeb462f1 | 444 | |
el14rd | 1:f682aeb462f1 | 445 | |
el14rd | 2:d7b17623ba26 | 446 | |
el14rd | 1:f682aeb462f1 | 447 | //main code |
el14rd | 1:f682aeb462f1 | 448 | int main() |
el14rd | 1:f682aeb462f1 | 449 | { |
el14rd | 1:f682aeb462f1 | 450 | lcd.init(); |
el14rd | 1:f682aeb462f1 | 451 | lcd.setBrightness(0.5); // put LED backlight on 50% |
el14rd | 1:f682aeb462f1 | 452 | calibrateJoystick(); // get centred values of joystick |
el14rd | 1:f682aeb462f1 | 453 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el14rd | 1:f682aeb462f1 | 454 | lcd.refresh(); |
el14rd | 2:d7b17623ba26 | 455 | initNut(); |
el14rd | 2:d7b17623ba26 | 456 | initSnake(); |
el14rd | 8:b01f638d23b4 | 457 | button.rise(&buttonPressed); |
el14rd | 8:b01f638d23b4 | 458 | buttonA.mode(PullDown); |
el14rd | 8:b01f638d23b4 | 459 | //pauseGame(); |
el14rd | 4:41acda9d68c7 | 460 | //printScore(); |
el14rd | 1:f682aeb462f1 | 461 | |
el14rd | 1:f682aeb462f1 | 462 | //infinite while loop |
el14rd | 1:f682aeb462f1 | 463 | while(1) { |
el14rd | 8:b01f638d23b4 | 464 | |
el14rd | 8:b01f638d23b4 | 465 | if(buttonFlag==0) { //joystick button unpressed |
el14rd | 8:b01f638d23b4 | 466 | lcd.printString("Press to start",1,1); |
el14rd | 8:b01f638d23b4 | 467 | snakeModel(); |
el14rd | 8:b01f638d23b4 | 468 | } else { |
el14rd | 8:b01f638d23b4 | 469 | |
el14rd | 8:b01f638d23b4 | 470 | leds = 0; |
el14rd | 8:b01f638d23b4 | 471 | |
el14rd | 8:b01f638d23b4 | 472 | if(buttonA) { // if button pressed, toggle pause |
el14rd | 8:b01f638d23b4 | 473 | wait_ms(50); |
el14rd | 8:b01f638d23b4 | 474 | pause = !pause; |
el14rd | 8:b01f638d23b4 | 475 | leds = 15; |
el14rd | 8:b01f638d23b4 | 476 | } |
el14rd | 8:b01f638d23b4 | 477 | |
el14rd | 8:b01f638d23b4 | 478 | while(pause) { |
el14rd | 8:b01f638d23b4 | 479 | leds = 3; |
el14rd | 8:b01f638d23b4 | 480 | if(buttonA) { // if button pressed, toggle pause |
el14rd | 8:b01f638d23b4 | 481 | wait_ms(50); |
el14rd | 8:b01f638d23b4 | 482 | pause = 0; |
el14rd | 8:b01f638d23b4 | 483 | leds = 1; |
el14rd | 8:b01f638d23b4 | 484 | |
el14rd | 8:b01f638d23b4 | 485 | } |
el14rd | 8:b01f638d23b4 | 486 | } |
el14rd | 8:b01f638d23b4 | 487 | } |
el14rd | 8:b01f638d23b4 | 488 | |
el14rd | 8:b01f638d23b4 | 489 | |
el14rd | 1:f682aeb462f1 | 490 | lcd.clear(); |
el14rd | 5:d8a06e7c54fb | 491 | lifeFlag=0; |
el14rd | 1:f682aeb462f1 | 492 | updateSnake(); |
el14rd | 1:f682aeb462f1 | 493 | drawSnake(); |
el14rd | 2:d7b17623ba26 | 494 | drawNut(); |
el14rd | 1:f682aeb462f1 | 495 | moveSnake(); |
el14rd | 1:f682aeb462f1 | 496 | startGmae(); |
el14rd | 8:b01f638d23b4 | 497 | lcd.refresh(); |
el14rd | 8:b01f638d23b4 | 498 | wait(0.08); |
el14rd | 8:b01f638d23b4 | 499 | //pauseGame(); |
el14rd | 8:b01f638d23b4 | 500 | //if (buttonFlag==1){ |
el14rd | 8:b01f638d23b4 | 501 | //while(1){ |
el14rd | 8:b01f638d23b4 | 502 | // buttonFlag =0; |
el14rd | 8:b01f638d23b4 | 503 | //if(buttonFlag ==1) |
el14rd | 8:b01f638d23b4 | 504 | // break; |
el14rd | 8:b01f638d23b4 | 505 | //} |
el14rd | 8:b01f638d23b4 | 506 | // } |
el14rd | 8:b01f638d23b4 | 507 | if(lifeFlag==1) { |
el14rd | 8:b01f638d23b4 | 508 | lcd.clear(); |
el14rd | 8:b01f638d23b4 | 509 | lcd.refresh(); |
el14rd | 8:b01f638d23b4 | 510 | printScore(); |
el14rd | 8:b01f638d23b4 | 511 | for(int i=0; i<=11; i++) { |
el14rd | 8:b01f638d23b4 | 512 | buzzer.period(1/(frequency[i])); // set PWM period |
el14rd | 8:b01f638d23b4 | 513 | buzzer=0.1; // set duty cycle |
el14rd | 8:b01f638d23b4 | 514 | wait(0.5*beat[i]); |
el14rd | 8:b01f638d23b4 | 515 | } |
el14rd | 8:b01f638d23b4 | 516 | } |
el14rd | 8:b01f638d23b4 | 517 | |
el14rd | 8:b01f638d23b4 | 518 | |
el14rd | 8:b01f638d23b4 | 519 | if (buttonFlag==1) { |
el14rd | 8:b01f638d23b4 | 520 | lifeFlag =0; |
el14rd | 8:b01f638d23b4 | 521 | } |
el14rd | 8:b01f638d23b4 | 522 | |
el14rd | 8:b01f638d23b4 | 523 | |
el14rd | 8:b01f638d23b4 | 524 | } |
el14rd | 8:b01f638d23b4 | 525 | |
el14rd | 8:b01f638d23b4 | 526 | |
el14rd | 1:f682aeb462f1 | 527 | } |
el14rd | 1:f682aeb462f1 | 528 |