ELEC2645 (2015/16) / Mbed 2 deprecated SnakeProjectRev1

Dependencies:   Joystick N5110 SDFileSystem beep fsmMenu mbed

Fork of SnakeProjectRev1 by Meurig Phillips

Committer:
meurigp
Date:
Fri Apr 22 13:03:15 2016 +0000
Revision:
7:f7f2abab16ef
Parent:
6:9a5706a27240
Child:
8:9b767dd1e549
prior to implementing ticker for main game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
meurigp 0:fcb5a32bc2fc 1 /* Joystick
meurigp 0:fcb5a32bc2fc 2
meurigp 0:fcb5a32bc2fc 3 Example code of how to read a joystick
meurigp 0:fcb5a32bc2fc 4
meurigp 0:fcb5a32bc2fc 5 https://www.sparkfun.com/products/9032
meurigp 0:fcb5a32bc2fc 6
meurigp 0:fcb5a32bc2fc 7 Craig A. Evans
meurigp 0:fcb5a32bc2fc 8 7 March 2015
meurigp 0:fcb5a32bc2fc 9 */
meurigp 0:fcb5a32bc2fc 10
meurigp 0:fcb5a32bc2fc 11 #include "mbed.h"
meurigp 0:fcb5a32bc2fc 12 #include "N5110.h"
meurigp 0:fcb5a32bc2fc 13
meurigp 0:fcb5a32bc2fc 14 // change this to alter tolerance of joystick direction
meurigp 0:fcb5a32bc2fc 15 #define DIRECTION_TOLERANCE 0.05
meurigp 0:fcb5a32bc2fc 16
meurigp 0:fcb5a32bc2fc 17 // VCC, SCE, RST, D/C, MOSI, SCLK, LED
meurigp 0:fcb5a32bc2fc 18 N5110 lcd (PTD3, PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
meurigp 0:fcb5a32bc2fc 19 // Can also power (VCC) directly from VOUT (3.3 V) -
meurigp 0:fcb5a32bc2fc 20 // Can give better performance due to current limitation from GPIO pin
meurigp 0:fcb5a32bc2fc 21
meurigp 0:fcb5a32bc2fc 22 // connections for joystick
meurigp 0:fcb5a32bc2fc 23 DigitalIn button(PTB18);
meurigp 7:f7f2abab16ef 24 AnalogIn xPot(PTB2);
meurigp 7:f7f2abab16ef 25 AnalogIn yPot(PTB3);
meurigp 0:fcb5a32bc2fc 26
meurigp 7:f7f2abab16ef 27 //PwmOut greenLed(PTC2);
meurigp 5:257b4816ac6a 28
meurigp 0:fcb5a32bc2fc 29 // timer to regularly read the joystick
meurigp 0:fcb5a32bc2fc 30 Ticker pollJoystick;
meurigp 0:fcb5a32bc2fc 31 // Serial for debug
meurigp 0:fcb5a32bc2fc 32 Serial serial(USBTX,USBRX);
meurigp 2:663b9aadf00c 33
meurigp 2:663b9aadf00c 34
meurigp 0:fcb5a32bc2fc 35
meurigp 0:fcb5a32bc2fc 36 // create enumerated type (0,1,2,3 etc. for direction)
meurigp 0:fcb5a32bc2fc 37 // could be extended for diagonals etc.
meurigp 0:fcb5a32bc2fc 38 enum DirectionName {
meurigp 0:fcb5a32bc2fc 39 UP,
meurigp 0:fcb5a32bc2fc 40 DOWN,
meurigp 0:fcb5a32bc2fc 41 LEFT,
meurigp 0:fcb5a32bc2fc 42 RIGHT,
meurigp 0:fcb5a32bc2fc 43 CENTRE,
meurigp 0:fcb5a32bc2fc 44 UNKNOWN
meurigp 0:fcb5a32bc2fc 45 };
meurigp 0:fcb5a32bc2fc 46
meurigp 0:fcb5a32bc2fc 47 /// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading))
meurigp 0:fcb5a32bc2fc 48 enum CurrentDirection {
meurigp 0:fcb5a32bc2fc 49 up,
meurigp 0:fcb5a32bc2fc 50 down,
meurigp 0:fcb5a32bc2fc 51 left,
meurigp 0:fcb5a32bc2fc 52 right,
meurigp 0:fcb5a32bc2fc 53 centre,
meurigp 0:fcb5a32bc2fc 54 };
meurigp 0:fcb5a32bc2fc 55 CurrentDirection currentDirection = centre; /// intialise direction at beginning
meurigp 0:fcb5a32bc2fc 56
meurigp 0:fcb5a32bc2fc 57 // struct for Joystick
meurigp 0:fcb5a32bc2fc 58 typedef struct JoyStick Joystick;
meurigp 0:fcb5a32bc2fc 59 struct JoyStick {
meurigp 0:fcb5a32bc2fc 60 float x; // current x value
meurigp 0:fcb5a32bc2fc 61 float x0; // 'centred' x value
meurigp 0:fcb5a32bc2fc 62 float y; // current y value
meurigp 0:fcb5a32bc2fc 63 float y0; // 'centred' y value
meurigp 0:fcb5a32bc2fc 64 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
meurigp 0:fcb5a32bc2fc 65 DirectionName direction; // current direction
meurigp 0:fcb5a32bc2fc 66 };
meurigp 0:fcb5a32bc2fc 67 // create struct variable
meurigp 0:fcb5a32bc2fc 68 Joystick joystick;
meurigp 0:fcb5a32bc2fc 69
meurigp 0:fcb5a32bc2fc 70 int printFlag = 0;
meurigp 0:fcb5a32bc2fc 71
meurigp 2:663b9aadf00c 72 int pixels[84][48];
meurigp 2:663b9aadf00c 73
meurigp 2:663b9aadf00c 74 int randomX = rand() % 83 + 1; // randomX in the range 1 to 81
meurigp 2:663b9aadf00c 75 int randomY = rand() % 47 + 1; // randomY in the range 1 to 47
meurigp 2:663b9aadf00c 76 int randomXoddEven = randomX%2; // find out whether odd or even
meurigp 2:663b9aadf00c 77 int randomYoddEven = randomY%2;
meurigp 2:663b9aadf00c 78
meurigp 3:0e3179c452c5 79 int snakeTailX[100];
meurigp 3:0e3179c452c5 80 int snakeTailY[100];
meurigp 3:0e3179c452c5 81 int snakeTailLength;
meurigp 3:0e3179c452c5 82
meurigp 5:257b4816ac6a 83 int score = 0;
meurigp 5:257b4816ac6a 84 int fruitValue = 10;
meurigp 5:257b4816ac6a 85
meurigp 4:3ceebacef5f1 86 int i = 41; // snake head origin x
meurigp 4:3ceebacef5f1 87 int j = 23; // snake head origin y
meurigp 5:257b4816ac6a 88 int prev_i = 41;
meurigp 5:257b4816ac6a 89 int prev_j = 23;
meurigp 5:257b4816ac6a 90 int prev2_i = 41;
meurigp 5:257b4816ac6a 91 int prev2_j = 23;
meurigp 4:3ceebacef5f1 92
meurigp 7:f7f2abab16ef 93
meurigp 6:9a5706a27240 94 bool gamePlaying = false;
meurigp 6:9a5706a27240 95
meurigp 0:fcb5a32bc2fc 96 // function prototypes
meurigp 0:fcb5a32bc2fc 97 void calibrateJoystick();
meurigp 0:fcb5a32bc2fc 98 void updateJoystick();
meurigp 3:0e3179c452c5 99 void joystickDirection();
meurigp 2:663b9aadf00c 100 void generateFood();
meurigp 5:257b4816ac6a 101 void newFruitXY();
meurigp 4:3ceebacef5f1 102 void moveSnake();
meurigp 5:257b4816ac6a 103 void greenLedFlash();
meurigp 5:257b4816ac6a 104 void hardWall();
meurigp 6:9a5706a27240 105 void specialMap();
meurigp 6:9a5706a27240 106 void wrapAround();
meurigp 5:257b4816ac6a 107 void scoreCalculation();
meurigp 3:0e3179c452c5 108
meurigp 6:9a5706a27240 109
meurigp 0:fcb5a32bc2fc 110
meurigp 0:fcb5a32bc2fc 111 int main()
meurigp 0:fcb5a32bc2fc 112 {
meurigp 2:663b9aadf00c 113
meurigp 0:fcb5a32bc2fc 114 calibrateJoystick(); // get centred values of joystick
meurigp 7:f7f2abab16ef 115 pollJoystick.attach(&updateJoystick,0.5/10.0); // read joystick 10 times per second
meurigp 2:663b9aadf00c 116
meurigp 2:663b9aadf00c 117 srand(time(NULL));
meurigp 2:663b9aadf00c 118
meurigp 0:fcb5a32bc2fc 119 lcd.init();
meurigp 2:663b9aadf00c 120 lcd.drawRect(i,j,1,1,1); // default snake position
meurigp 6:9a5706a27240 121 //hardWall();
meurigp 2:663b9aadf00c 122 generateFood();
meurigp 6:9a5706a27240 123 gamePlaying = true;
meurigp 7:f7f2abab16ef 124 // greenLed = 0;
meurigp 0:fcb5a32bc2fc 125
meurigp 0:fcb5a32bc2fc 126 while(1) {
meurigp 0:fcb5a32bc2fc 127
meurigp 0:fcb5a32bc2fc 128 if (printFlag) { // if flag set, clear flag and print joystick values to serial port
meurigp 0:fcb5a32bc2fc 129 printFlag = 0;
meurigp 3:0e3179c452c5 130
meurigp 6:9a5706a27240 131 if (gamePlaying == true) {
meurigp 4:3ceebacef5f1 132 moveSnake();
meurigp 6:9a5706a27240 133 }
meurigp 6:9a5706a27240 134 else if (gamePlaying == false) {
meurigp 6:9a5706a27240 135 }
meurigp 6:9a5706a27240 136 }
meurigp 4:3ceebacef5f1 137 sleep(); // put the MCU to sleep until an interrupt wakes it up
meurigp 0:fcb5a32bc2fc 138 }
meurigp 0:fcb5a32bc2fc 139 }
meurigp 0:fcb5a32bc2fc 140
meurigp 4:3ceebacef5f1 141
meurigp 4:3ceebacef5f1 142 void calibrateJoystick() // read default positions of the joystick to calibrate later readings
meurigp 0:fcb5a32bc2fc 143 {
meurigp 0:fcb5a32bc2fc 144 button.mode(PullDown);
meurigp 0:fcb5a32bc2fc 145 // must not move during calibration
meurigp 0:fcb5a32bc2fc 146 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
meurigp 0:fcb5a32bc2fc 147 joystick.y0 = yPot;
meurigp 0:fcb5a32bc2fc 148 }
meurigp 4:3ceebacef5f1 149
meurigp 0:fcb5a32bc2fc 150 void updateJoystick()
meurigp 0:fcb5a32bc2fc 151 {
meurigp 0:fcb5a32bc2fc 152 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
meurigp 0:fcb5a32bc2fc 153 joystick.x = xPot - joystick.x0;
meurigp 0:fcb5a32bc2fc 154 joystick.y = yPot - joystick.y0;
meurigp 0:fcb5a32bc2fc 155 // read button state
meurigp 0:fcb5a32bc2fc 156 joystick.button = button;
meurigp 0:fcb5a32bc2fc 157
meurigp 0:fcb5a32bc2fc 158 // calculate direction depending on x,y values
meurigp 0:fcb5a32bc2fc 159 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
meurigp 0:fcb5a32bc2fc 160 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
meurigp 0:fcb5a32bc2fc 161 joystick.direction = CENTRE;
meurigp 0:fcb5a32bc2fc 162 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
meurigp 0:fcb5a32bc2fc 163 joystick.direction = UP;
meurigp 0:fcb5a32bc2fc 164 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
meurigp 0:fcb5a32bc2fc 165 joystick.direction = DOWN;
meurigp 0:fcb5a32bc2fc 166 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
meurigp 7:f7f2abab16ef 167 joystick.direction = LEFT;
meurigp 0:fcb5a32bc2fc 168 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
meurigp 7:f7f2abab16ef 169 joystick.direction = RIGHT;
meurigp 0:fcb5a32bc2fc 170 } else {
meurigp 0:fcb5a32bc2fc 171 joystick.direction = UNKNOWN;
meurigp 0:fcb5a32bc2fc 172 }
meurigp 0:fcb5a32bc2fc 173
meurigp 0:fcb5a32bc2fc 174 // set flag for printing
meurigp 0:fcb5a32bc2fc 175 printFlag = 1;
meurigp 0:fcb5a32bc2fc 176 }
meurigp 1:97ac723959f2 177
meurigp 2:663b9aadf00c 178 void generateFood()
meurigp 1:97ac723959f2 179 {
meurigp 2:663b9aadf00c 180 while (randomXoddEven ==0 || randomYoddEven ==0 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on
meurigp 2:663b9aadf00c 181
meurigp 5:257b4816ac6a 182 randomX = rand() % 83 + 1; // randomX in the range 1 to 83
meurigp 2:663b9aadf00c 183 randomY = rand() % 47 + 1; // randomY in the range 1 to 47
meurigp 2:663b9aadf00c 184
meurigp 3:0e3179c452c5 185 // serial.printf("X = %i\n",randomX); // debug
meurigp 3:0e3179c452c5 186 // serial.printf("Y = %i\n\n",randomY);
meurigp 2:663b9aadf00c 187
meurigp 2:663b9aadf00c 188 randomXoddEven = randomX%2; // find out whether odd or even
meurigp 2:663b9aadf00c 189 randomYoddEven = randomY%2;
meurigp 2:663b9aadf00c 190 }
meurigp 2:663b9aadf00c 191
meurigp 2:663b9aadf00c 192 lcd.drawRect(randomX,randomY,1,1,1);
meurigp 1:97ac723959f2 193
meurigp 1:97ac723959f2 194 }
meurigp 3:0e3179c452c5 195
meurigp 5:257b4816ac6a 196 void newFruitXY() // new fruit coordinate values are given before it is passed to the generateFood function
meurigp 3:0e3179c452c5 197 {
meurigp 4:3ceebacef5f1 198
meurigp 3:0e3179c452c5 199 randomX = rand() % 83 + 1; // randomX in the range 1 to 81
meurigp 3:0e3179c452c5 200 randomY = rand() % 47 + 1; // randomY in the range 1 to 47
meurigp 3:0e3179c452c5 201 randomXoddEven = randomX%2; // find out whether odd or even
meurigp 3:0e3179c452c5 202 randomYoddEven = randomY%2;
meurigp 1:97ac723959f2 203
meurigp 3:0e3179c452c5 204 }
meurigp 3:0e3179c452c5 205
meurigp 4:3ceebacef5f1 206 void joystickDirection() { // stcik direction for debug
meurigp 3:0e3179c452c5 207
meurigp 3:0e3179c452c5 208 serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
meurigp 1:97ac723959f2 209
meurigp 3:0e3179c452c5 210 // check joystick direction
meurigp 3:0e3179c452c5 211 if (joystick.direction == UP)
meurigp 3:0e3179c452c5 212 serial.printf(" UP\n");
meurigp 3:0e3179c452c5 213 if (joystick.direction == DOWN)
meurigp 3:0e3179c452c5 214 serial.printf(" DOWN\n");
meurigp 3:0e3179c452c5 215 if (joystick.direction == LEFT)
meurigp 3:0e3179c452c5 216 serial.printf(" LEFT\n");
meurigp 3:0e3179c452c5 217 if (joystick.direction == RIGHT)
meurigp 3:0e3179c452c5 218 serial.printf(" RIGHT\n");
meurigp 3:0e3179c452c5 219 if (joystick.direction == CENTRE)
meurigp 3:0e3179c452c5 220 serial.printf(" CENTRE\n");
meurigp 3:0e3179c452c5 221 if (joystick.direction == UNKNOWN)
meurigp 3:0e3179c452c5 222 serial.printf(" Unsupported direction\n");
meurigp 3:0e3179c452c5 223
meurigp 3:0e3179c452c5 224 }
meurigp 4:3ceebacef5f1 225
meurigp 4:3ceebacef5f1 226 void moveSnake() {
meurigp 4:3ceebacef5f1 227
meurigp 4:3ceebacef5f1 228 if (joystick.direction == LEFT) {
meurigp 4:3ceebacef5f1 229 if (currentDirection != right) { // change the currentDirection according to joystick input, providing
meurigp 4:3ceebacef5f1 230 currentDirection = left; // it's not the opposite direction to the current direction
meurigp 4:3ceebacef5f1 231 }
meurigp 4:3ceebacef5f1 232 }
meurigp 4:3ceebacef5f1 233 else if (joystick.direction == RIGHT) {
meurigp 4:3ceebacef5f1 234 if (currentDirection != left) { // change the currentDirection according to joystick input, providing
meurigp 4:3ceebacef5f1 235 currentDirection = right; // it's not the opposite direction to the current direction
meurigp 4:3ceebacef5f1 236 }
meurigp 4:3ceebacef5f1 237 }
meurigp 4:3ceebacef5f1 238 else if (joystick.direction == UP) {
meurigp 4:3ceebacef5f1 239 if (currentDirection != down) { // change the currentDirection according to joystick input, providing
meurigp 4:3ceebacef5f1 240 currentDirection = up; // it's not the opposite direction to the current direction
meurigp 4:3ceebacef5f1 241 }
meurigp 4:3ceebacef5f1 242 }
meurigp 4:3ceebacef5f1 243 else if (joystick.direction == DOWN) {
meurigp 4:3ceebacef5f1 244 if (currentDirection != up) { // change the currentDirection according to joystick input, providing
meurigp 4:3ceebacef5f1 245 currentDirection = down; // it's not the opposite direction to the current direction
meurigp 4:3ceebacef5f1 246 }
meurigp 4:3ceebacef5f1 247 }
meurigp 4:3ceebacef5f1 248
meurigp 4:3ceebacef5f1 249 lcd.clear();
meurigp 4:3ceebacef5f1 250 prev2_i = prev_i; // previous coordinates of the previous snake head coordinates
meurigp 4:3ceebacef5f1 251 prev2_j = prev_j;
meurigp 4:3ceebacef5f1 252 prev_i = i; // previous coordinates of the snake head
meurigp 4:3ceebacef5f1 253 prev_j = j;
meurigp 4:3ceebacef5f1 254
meurigp 4:3ceebacef5f1 255 if (currentDirection == left) { // shift snake head coordinates according to current direction
meurigp 4:3ceebacef5f1 256 i -= 2; }
meurigp 4:3ceebacef5f1 257 else if (currentDirection == right) {
meurigp 4:3ceebacef5f1 258 i += 2; }
meurigp 4:3ceebacef5f1 259 else if (currentDirection == up) {
meurigp 4:3ceebacef5f1 260 j -= 2; }
meurigp 4:3ceebacef5f1 261 else if (currentDirection == down) {
meurigp 4:3ceebacef5f1 262 j += 2; }
meurigp 4:3ceebacef5f1 263
meurigp 6:9a5706a27240 264
meurigp 6:9a5706a27240 265 //lcd.drawRect(i,j,1,1,1); // snake head
meurigp 6:9a5706a27240 266 lcd.setPixel(i,j);
meurigp 6:9a5706a27240 267 lcd.setPixel(i,j+1);
meurigp 6:9a5706a27240 268 lcd.setPixel(i+1,j+1);
meurigp 6:9a5706a27240 269 lcd.setPixel(i+1,j);
meurigp 6:9a5706a27240 270 serial.printf("j = %i\n",j);
meurigp 6:9a5706a27240 271 serial.printf("j+1 = %i\n\n",j+1);
meurigp 6:9a5706a27240 272 /* serial.printf("i+1,j+1 = %i,%i\n",i+1,j+1);
meurigp 6:9a5706a27240 273 serial.printf("i+1,j = %i,%i\n",i+1,j); */
meurigp 6:9a5706a27240 274
meurigp 6:9a5706a27240 275 //lcd.drawRect(prev_i,prev_j,1,1,1); // seg 1
meurigp 6:9a5706a27240 276 lcd.setPixel(prev_i,prev_j);
meurigp 6:9a5706a27240 277 lcd.setPixel(prev_i,prev_j+1);
meurigp 6:9a5706a27240 278 lcd.setPixel(prev_i+1,prev_j+1);
meurigp 6:9a5706a27240 279 lcd.setPixel(prev_i+1,prev_j);
meurigp 6:9a5706a27240 280 /* serial.printf("prev_i,prev_j = %i,%i\n",prev_i,prev_j);
meurigp 6:9a5706a27240 281 serial.printf("prev_i,prev_j+1 = %i,%i\n",prev_i,prev_j+1);
meurigp 6:9a5706a27240 282 serial.printf("prev_i+1,prev_j+1 = %i,%i\n",prev_i+1,prev_j+1);
meurigp 6:9a5706a27240 283 serial.printf("prev_i+1,prev_j = %i,%i\n",prev_i+1,prev_j); */
meurigp 6:9a5706a27240 284
meurigp 6:9a5706a27240 285 //lcd.drawRect(prev2_i,prev2_j,1,1,1); // seg 2
meurigp 6:9a5706a27240 286 lcd.setPixel(prev2_i,prev2_j);
meurigp 6:9a5706a27240 287 lcd.setPixel(prev2_i,prev2_j+1);
meurigp 6:9a5706a27240 288 lcd.setPixel(prev2_i+1,prev2_j+1);
meurigp 6:9a5706a27240 289 lcd.setPixel(prev2_i+1,prev2_j);
meurigp 6:9a5706a27240 290 /* serial.printf("prev2_i,prev2_j = %i,%i\n",prev2_i,prev2_j);
meurigp 6:9a5706a27240 291 serial.printf("prev2_i,prev2_j+1 = %i,%i\n",prev2_i,prev2_j+1);
meurigp 6:9a5706a27240 292 serial.printf("prev2_i+1,prev2_j+1 = %i,%i\n",prev2_i+1,prev2_j+1);
meurigp 6:9a5706a27240 293 serial.printf("prev2_i+1,prev2_j = %i,%i\n\n",prev2_i+1,prev2_j); */
meurigp 6:9a5706a27240 294
meurigp 6:9a5706a27240 295 lcd.refresh();
meurigp 6:9a5706a27240 296
meurigp 4:3ceebacef5f1 297 lcd.drawRect(randomX,randomY,1,1,1); // food
meurigp 7:f7f2abab16ef 298 hardWall();
meurigp 6:9a5706a27240 299 //specialMap();
meurigp 7:f7f2abab16ef 300 //wrapAround();
meurigp 4:3ceebacef5f1 301
meurigp 4:3ceebacef5f1 302 if (i == randomX && j == randomY) { // if fruit is eaten
meurigp 7:f7f2abab16ef 303 //greenLedFlash();
meurigp 5:257b4816ac6a 304 scoreCalculation();
meurigp 4:3ceebacef5f1 305 snakeTailLength++;
meurigp 5:257b4816ac6a 306 newFruitXY();
meurigp 4:3ceebacef5f1 307 generateFood();
meurigp 4:3ceebacef5f1 308 }
meurigp 7:f7f2abab16ef 309 wait(0.5);
meurigp 4:3ceebacef5f1 310
meurigp 5:257b4816ac6a 311 }
meurigp 5:257b4816ac6a 312
meurigp 7:f7f2abab16ef 313 /*void greenLedFlash() {
meurigp 5:257b4816ac6a 314
meurigp 5:257b4816ac6a 315 for(float p = 1.0f; p > 0.0f; p -= 0.1f) { // green led rapidly decreases from full brightness to off
meurigp 5:257b4816ac6a 316 greenLed = p;
meurigp 5:257b4816ac6a 317 wait(0.07);
meurigp 5:257b4816ac6a 318 }
meurigp 5:257b4816ac6a 319 greenLed = 0;
meurigp 5:257b4816ac6a 320
meurigp 7:f7f2abab16ef 321 } */
meurigp 5:257b4816ac6a 322
meurigp 5:257b4816ac6a 323 void hardWall() {
meurigp 5:257b4816ac6a 324
meurigp 5:257b4816ac6a 325 lcd.drawRect(0,0,83,47,0);
meurigp 6:9a5706a27240 326 lcd.refresh();
meurigp 6:9a5706a27240 327 if (i == 0 || i+1 == 0 ||
meurigp 6:9a5706a27240 328 i == 83 || i+1 == 83 || // if any of the 4 pixels within the snake head touch the border
meurigp 6:9a5706a27240 329 j == 0 || j+1 == 0 ||
meurigp 6:9a5706a27240 330 j == 47 || j+1 == 47) {
meurigp 6:9a5706a27240 331
meurigp 6:9a5706a27240 332 gamePlaying = false;
meurigp 6:9a5706a27240 333 lcd.clear();
meurigp 6:9a5706a27240 334 lcd.printString("Game Over",15,0); // width(6) * n(9) = 54, 84-54 = 30, 30/2 = 15
meurigp 6:9a5706a27240 335
meurigp 6:9a5706a27240 336 char buffer[14];
meurigp 6:9a5706a27240 337 int length = sprintf(buffer,"Score: %i",score); // display score of round
meurigp 6:9a5706a27240 338 if (length <= 14) { // if string will fit on display
meurigp 6:9a5706a27240 339 lcd.printString(buffer,0,1); } // display on screen
meurigp 6:9a5706a27240 340
meurigp 6:9a5706a27240 341 }
meurigp 5:257b4816ac6a 342
meurigp 5:257b4816ac6a 343 }
meurigp 5:257b4816ac6a 344
meurigp 6:9a5706a27240 345 void specialMap() {
meurigp 6:9a5706a27240 346
meurigp 6:9a5706a27240 347 for (int i=0; i<21; i++) { // top/bottom left x line
meurigp 6:9a5706a27240 348 lcd.setPixel(i,0);
meurigp 6:9a5706a27240 349 lcd.setPixel(i,47);
meurigp 6:9a5706a27240 350 lcd.refresh();
meurigp 6:9a5706a27240 351 }
meurigp 6:9a5706a27240 352 for (int i=63; i<84; i++) { // top/bottom right x line
meurigp 6:9a5706a27240 353 lcd.setPixel(i,0);
meurigp 6:9a5706a27240 354 lcd.setPixel(i,47);
meurigp 6:9a5706a27240 355 lcd.refresh();
meurigp 6:9a5706a27240 356 }
meurigp 6:9a5706a27240 357 for (int i=37; i<47; i++) { // top/bottom middle x line
meurigp 6:9a5706a27240 358 lcd.setPixel(i,0);
meurigp 6:9a5706a27240 359 lcd.setPixel(i,47);
meurigp 6:9a5706a27240 360 lcd.refresh();
meurigp 6:9a5706a27240 361 }
meurigp 6:9a5706a27240 362 for (int j=0; j<17; j++) { // left/right top y line
meurigp 6:9a5706a27240 363 lcd.setPixel(0,j);
meurigp 6:9a5706a27240 364 lcd.setPixel(83,j);
meurigp 6:9a5706a27240 365 lcd.refresh();
meurigp 6:9a5706a27240 366 }
meurigp 6:9a5706a27240 367 for (int j=29; j<48; j++) { // left/right bottom y line
meurigp 6:9a5706a27240 368 lcd.setPixel(0,j);
meurigp 6:9a5706a27240 369 lcd.setPixel(83,j);
meurigp 6:9a5706a27240 370 lcd.refresh();
meurigp 6:9a5706a27240 371 }
meurigp 6:9a5706a27240 372 for (int i=41; i<43; i++) { // vertical cross line
meurigp 6:9a5706a27240 373 for (int j=14; j<34; j++) {
meurigp 6:9a5706a27240 374 lcd.setPixel(i,j);
meurigp 6:9a5706a27240 375 lcd.refresh();
meurigp 6:9a5706a27240 376 }
meurigp 6:9a5706a27240 377 }
meurigp 6:9a5706a27240 378 for (int i=32; i<52; i++) { // horizontal cross line
meurigp 6:9a5706a27240 379 for (int j=23; j<25; j++) {
meurigp 6:9a5706a27240 380 lcd.setPixel(i,j);
meurigp 6:9a5706a27240 381 lcd.refresh();
meurigp 6:9a5706a27240 382 }
meurigp 6:9a5706a27240 383 }
meurigp 6:9a5706a27240 384
meurigp 6:9a5706a27240 385 }
meurigp 6:9a5706a27240 386
meurigp 7:f7f2abab16ef 387 /*void wrapAround() {
meurigp 6:9a5706a27240 388
meurigp 6:9a5706a27240 389 if (i == -1) {
meurigp 6:9a5706a27240 390 i = 83;
meurigp 6:9a5706a27240 391 }
meurigp 6:9a5706a27240 392 if (i == 84) {
meurigp 6:9a5706a27240 393 i = 0;
meurigp 6:9a5706a27240 394 }
meurigp 6:9a5706a27240 395 if (j == -1) {
meurigp 6:9a5706a27240 396 j = 47;
meurigp 6:9a5706a27240 397 }
meurigp 6:9a5706a27240 398 if (j+1 == 48) {
meurigp 7:f7f2abab16ef 399 j+1 = 0;
meurigp 6:9a5706a27240 400 }
meurigp 6:9a5706a27240 401
meurigp 6:9a5706a27240 402 lcd.setPixel(i,j);
meurigp 6:9a5706a27240 403 lcd.setPixel(i,j+1);
meurigp 6:9a5706a27240 404 lcd.setPixel(i+1,j+1);
meurigp 6:9a5706a27240 405 lcd.setPixel(i+1,j);
meurigp 6:9a5706a27240 406 lcd.setPixel(prev_i,prev_j);
meurigp 6:9a5706a27240 407 lcd.setPixel(prev_i,prev_j+1);
meurigp 6:9a5706a27240 408 lcd.setPixel(prev_i+1,prev_j+1);
meurigp 6:9a5706a27240 409 lcd.setPixel(prev_i+1,prev_j);
meurigp 6:9a5706a27240 410 lcd.setPixel(prev2_i,prev2_j);
meurigp 6:9a5706a27240 411 lcd.setPixel(prev2_i,prev2_j+1);
meurigp 6:9a5706a27240 412 lcd.setPixel(prev2_i+1,prev2_j+1);
meurigp 6:9a5706a27240 413 lcd.setPixel(prev2_i+1,prev2_j);
meurigp 6:9a5706a27240 414
meurigp 6:9a5706a27240 415 lcd.refresh();
meurigp 7:f7f2abab16ef 416 } */
meurigp 6:9a5706a27240 417
meurigp 6:9a5706a27240 418
meurigp 5:257b4816ac6a 419 void scoreCalculation() {
meurigp 5:257b4816ac6a 420
meurigp 5:257b4816ac6a 421 score += fruitValue; // each time fruit is eaten score is calculated and fruit value will increase by 1
meurigp 5:257b4816ac6a 422
meurigp 5:257b4816ac6a 423 fruitValue++;
meurigp 5:257b4816ac6a 424
meurigp 5:257b4816ac6a 425 serial.printf("score = %i\n",score);
meurigp 5:257b4816ac6a 426 serial.printf("fruitValue = %i\n\n",fruitValue);
meurigp 5:257b4816ac6a 427
meurigp 5:257b4816ac6a 428 }