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.
Dependencies: Joystick N5110 SDFileSystem beep fsmMenu mbed
Fork of SnakeProjectRev1 by
main.cpp@20:f634b1060981, 2016-05-05 (annotated)
- Committer:
- meurigp
- Date:
- Thu May 05 13:31:19 2016 +0000
- Revision:
- 20:f634b1060981
- Parent:
- 19:8907a82ebe09
- Child:
- 21:e03461ea23e9
working game! special map yet to be implemented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
meurigp | 15:a5590211888c | 1 | /** |
meurigp | 15:a5590211888c | 2 | @file main.cpp |
meurigp | 10:7820b46476ea | 3 | |
meurigp | 15:a5590211888c | 4 | @brief Program implementation of Snake Game |
meurigp | 15:a5590211888c | 5 | |
meurigp | 15:a5590211888c | 6 | */ |
meurigp | 0:fcb5a32bc2fc | 7 | |
meurigp | 0:fcb5a32bc2fc | 8 | #include "mbed.h" |
meurigp | 11:f8478bc749e0 | 9 | #include "main.h" |
meurigp | 0:fcb5a32bc2fc | 10 | #include "N5110.h" |
meurigp | 9:6d7074258c63 | 11 | #include "beep.h" |
meurigp | 9:6d7074258c63 | 12 | #include "SDFileSystem.h" |
meurigp | 10:7820b46476ea | 13 | #include "Joystick.h" |
meurigp | 15:a5590211888c | 14 | #include "fsmMenu.h" |
meurigp | 0:fcb5a32bc2fc | 15 | |
meurigp | 0:fcb5a32bc2fc | 16 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
meurigp | 0:fcb5a32bc2fc | 17 | N5110 lcd (PTD3, PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
meurigp | 0:fcb5a32bc2fc | 18 | // Can also power (VCC) directly from VOUT (3.3 V) - |
meurigp | 9:6d7074258c63 | 19 | // Can give better performance due to current limitation from GPIO pin |
meurigp | 9:6d7074258c63 | 20 | |
meurigp | 9:6d7074258c63 | 21 | // Connections to SD card holder on K64F (SPI interface) |
meurigp | 9:6d7074258c63 | 22 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
meurigp | 0:fcb5a32bc2fc | 23 | |
meurigp | 12:825a402d230f | 24 | /// Serial for debug |
meurigp | 12:825a402d230f | 25 | Serial serial(USBTX,USBRX); |
meurigp | 0:fcb5a32bc2fc | 26 | Ticker pollJoystick; |
meurigp | 14:56e355c5cfc9 | 27 | Ticker gameTicker; |
meurigp | 14:56e355c5cfc9 | 28 | |
meurigp | 16:68b9460d4c76 | 29 | |
meurigp | 0:fcb5a32bc2fc | 30 | |
meurigp | 9:6d7074258c63 | 31 | FILE *fp; // this is our file pointer |
meurigp | 6:9a5706a27240 | 32 | |
meurigp | 14:56e355c5cfc9 | 33 | void timer_isr(); |
meurigp | 16:68b9460d4c76 | 34 | void rb_isr(); |
meurigp | 16:68b9460d4c76 | 35 | void lb_isr(); |
meurigp | 9:6d7074258c63 | 36 | |
meurigp | 14:56e355c5cfc9 | 37 | int main() |
meurigp | 14:56e355c5cfc9 | 38 | { |
meurigp | 0:fcb5a32bc2fc | 39 | calibrateJoystick(); // get centred values of joystick |
meurigp | 8:9b767dd1e549 | 40 | pollJoystick.attach(&updateJoystick,0.5/10.0); // read joystick 20 times per second |
meurigp | 2:663b9aadf00c | 41 | |
meurigp | 2:663b9aadf00c | 42 | srand(time(NULL)); |
meurigp | 9:6d7074258c63 | 43 | |
meurigp | 9:6d7074258c63 | 44 | greenLed = 1; |
meurigp | 9:6d7074258c63 | 45 | redLed = 0; |
meurigp | 0:fcb5a32bc2fc | 46 | lcd.init(); |
meurigp | 20:f634b1060981 | 47 | snakeIntro(); |
meurigp | 16:68b9460d4c76 | 48 | gamePlaying = false; |
meurigp | 14:56e355c5cfc9 | 49 | button.rise(&buttonISR); |
meurigp | 14:56e355c5cfc9 | 50 | gameTicker.attach(&timer_isr,0.1); |
meurigp | 16:68b9460d4c76 | 51 | RB.mode(PullDown); |
meurigp | 16:68b9460d4c76 | 52 | LB.mode(PullDown); |
meurigp | 16:68b9460d4c76 | 53 | RB.rise(&rb_isr); |
meurigp | 16:68b9460d4c76 | 54 | LB.rise(&lb_isr); |
meurigp | 16:68b9460d4c76 | 55 | |
meurigp | 16:68b9460d4c76 | 56 | |
meurigp | 19:8907a82ebe09 | 57 | mainMenu(); |
meurigp | 0:fcb5a32bc2fc | 58 | |
meurigp | 0:fcb5a32bc2fc | 59 | } |
meurigp | 0:fcb5a32bc2fc | 60 | |
meurigp | 20:f634b1060981 | 61 | void mainGame() { |
meurigp | 20:f634b1060981 | 62 | |
meurigp | 20:f634b1060981 | 63 | if(gameType == classicMode) { |
meurigp | 20:f634b1060981 | 64 | hardWall(); |
meurigp | 20:f634b1060981 | 65 | i = 41; |
meurigp | 20:f634b1060981 | 66 | j = 23; |
meurigp | 20:f634b1060981 | 67 | } |
meurigp | 20:f634b1060981 | 68 | else if(gameType == infiniteMode) { |
meurigp | 20:f634b1060981 | 69 | wrapAround(); |
meurigp | 20:f634b1060981 | 70 | i = 40; |
meurigp | 20:f634b1060981 | 71 | j = 22; |
meurigp | 20:f634b1060981 | 72 | } |
meurigp | 20:f634b1060981 | 73 | else { |
meurigp | 20:f634b1060981 | 74 | specialMap(); |
meurigp | 20:f634b1060981 | 75 | i = 13; |
meurigp | 20:f634b1060981 | 76 | j = 5; |
meurigp | 20:f634b1060981 | 77 | } |
meurigp | 20:f634b1060981 | 78 | generateFood(); |
meurigp | 20:f634b1060981 | 79 | initSnakeTail(); |
meurigp | 20:f634b1060981 | 80 | snakeTailLength = 3; |
meurigp | 20:f634b1060981 | 81 | score = 0; |
meurigp | 20:f634b1060981 | 82 | fruitValue = 10; |
meurigp | 20:f634b1060981 | 83 | pauseCount = 0; // init everything |
meurigp | 20:f634b1060981 | 84 | |
meurigp | 20:f634b1060981 | 85 | while(gamePlaying == true) { |
meurigp | 20:f634b1060981 | 86 | |
meurigp | 20:f634b1060981 | 87 | lcd.setBrightness(1-pot); // turn pot right for brightness |
meurigp | 20:f634b1060981 | 88 | |
meurigp | 20:f634b1060981 | 89 | if (buttonFlag ==1) { |
meurigp | 20:f634b1060981 | 90 | buttonFlag = 0; |
meurigp | 20:f634b1060981 | 91 | if (pauseCount < 3) { |
meurigp | 20:f634b1060981 | 92 | gamePaused(); |
meurigp | 20:f634b1060981 | 93 | } |
meurigp | 20:f634b1060981 | 94 | buttonFlag = 0; |
meurigp | 20:f634b1060981 | 95 | } |
meurigp | 20:f634b1060981 | 96 | buttonFlag = 0; |
meurigp | 20:f634b1060981 | 97 | |
meurigp | 20:f634b1060981 | 98 | if(game_timer_flag) { |
meurigp | 20:f634b1060981 | 99 | game_timer_flag = 0; |
meurigp | 20:f634b1060981 | 100 | gameLogic(); |
meurigp | 20:f634b1060981 | 101 | } |
meurigp | 20:f634b1060981 | 102 | if (printFlag) { // if flag set, clear flag and print joystick values to serial port |
meurigp | 20:f634b1060981 | 103 | printFlag = 0; |
meurigp | 20:f634b1060981 | 104 | moveSnake(); |
meurigp | 20:f634b1060981 | 105 | } |
meurigp | 20:f634b1060981 | 106 | sleep(); // put the MCU to sleep until an interrupt wakes it up |
meurigp | 20:f634b1060981 | 107 | } |
meurigp | 20:f634b1060981 | 108 | } |
meurigp | 20:f634b1060981 | 109 | |
meurigp | 20:f634b1060981 | 110 | void mainMenu() { |
meurigp | 20:f634b1060981 | 111 | |
meurigp | 20:f634b1060981 | 112 | while(1) { |
meurigp | 20:f634b1060981 | 113 | |
meurigp | 20:f634b1060981 | 114 | lcd.printString("Classic",0,1); |
meurigp | 20:f634b1060981 | 115 | lcd.printString("Infinite",0,3); |
meurigp | 20:f634b1060981 | 116 | lcd.printString("Hard Map",0,5); |
meurigp | 20:f634b1060981 | 117 | |
meurigp | 20:f634b1060981 | 118 | if (rb_flag == 1) { |
meurigp | 20:f634b1060981 | 119 | rb_flag = 0; |
meurigp | 20:f634b1060981 | 120 | gamePlaying = true; |
meurigp | 20:f634b1060981 | 121 | mainGame(); |
meurigp | 20:f634b1060981 | 122 | } |
meurigp | 20:f634b1060981 | 123 | |
meurigp | 20:f634b1060981 | 124 | // check if flag i.e. interrupt has occured |
meurigp | 20:f634b1060981 | 125 | if (printFlag ==1) { |
meurigp | 20:f634b1060981 | 126 | printFlag = 0; // if it has, clear the flag |
meurigp | 20:f634b1060981 | 127 | |
meurigp | 20:f634b1060981 | 128 | // swap direction when button has been pressed |
meurigp | 20:f634b1060981 | 129 | // (could just use ! but want this to be explicit to aid understanding) |
meurigp | 20:f634b1060981 | 130 | if (joystick.direction == CENTRE) { |
meurigp | 20:f634b1060981 | 131 | serial.printf(" CENTRE\n"); |
meurigp | 20:f634b1060981 | 132 | menuDirection = menuSTOP; |
meurigp | 20:f634b1060981 | 133 | } |
meurigp | 20:f634b1060981 | 134 | else if (joystick.direction == UP) { |
meurigp | 20:f634b1060981 | 135 | serial.printf(" UP\n"); |
meurigp | 20:f634b1060981 | 136 | menuDirection = menuUP; |
meurigp | 20:f634b1060981 | 137 | buzzer.beep(2000,0.2); |
meurigp | 20:f634b1060981 | 138 | } |
meurigp | 20:f634b1060981 | 139 | else if (joystick.direction == DOWN) { |
meurigp | 20:f634b1060981 | 140 | serial.printf(" DOWN\n"); |
meurigp | 20:f634b1060981 | 141 | menuDirection = menuDOWN; |
meurigp | 20:f634b1060981 | 142 | buzzer.beep(2000,0.2); |
meurigp | 20:f634b1060981 | 143 | } |
meurigp | 20:f634b1060981 | 144 | } |
meurigp | 20:f634b1060981 | 145 | |
meurigp | 20:f634b1060981 | 146 | |
meurigp | 20:f634b1060981 | 147 | menuFSM(); |
meurigp | 20:f634b1060981 | 148 | |
meurigp | 20:f634b1060981 | 149 | |
meurigp | 20:f634b1060981 | 150 | wait(0.2); // small delay |
meurigp | 20:f634b1060981 | 151 | if (state ==0) { |
meurigp | 20:f634b1060981 | 152 | lcd.clear(); |
meurigp | 20:f634b1060981 | 153 | lcd.printString("*",70,1); |
meurigp | 20:f634b1060981 | 154 | gameType = classicMode; |
meurigp | 20:f634b1060981 | 155 | } |
meurigp | 20:f634b1060981 | 156 | else if (state ==1) { |
meurigp | 20:f634b1060981 | 157 | lcd.clear(); |
meurigp | 20:f634b1060981 | 158 | lcd.printString("*",70,3); |
meurigp | 20:f634b1060981 | 159 | gameType = infiniteMode; |
meurigp | 20:f634b1060981 | 160 | } |
meurigp | 20:f634b1060981 | 161 | else if (state ==2) { |
meurigp | 20:f634b1060981 | 162 | lcd.clear(); |
meurigp | 20:f634b1060981 | 163 | lcd.printString("*",70,5); |
meurigp | 20:f634b1060981 | 164 | gameType = hardMode; |
meurigp | 20:f634b1060981 | 165 | } |
meurigp | 20:f634b1060981 | 166 | |
meurigp | 20:f634b1060981 | 167 | } |
meurigp | 20:f634b1060981 | 168 | |
meurigp | 20:f634b1060981 | 169 | } |
meurigp | 4:3ceebacef5f1 | 170 | |
meurigp | 2:663b9aadf00c | 171 | void generateFood() |
meurigp | 1:97ac723959f2 | 172 | { |
meurigp | 17:4e6f0f7f22fb | 173 | if (gameType == classicMode || gameType == hardMode) { |
meurigp | 17:4e6f0f7f22fb | 174 | while (randomXoddEven ==0 || randomYoddEven ==0 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on |
meurigp | 17:4e6f0f7f22fb | 175 | |
meurigp | 17:4e6f0f7f22fb | 176 | randomX = rand() % 83 + 1; // randomX in the range 1 to 83 |
meurigp | 17:4e6f0f7f22fb | 177 | randomY = rand() % 47 + 1; // randomY in the range 1 to 47 |
meurigp | 17:4e6f0f7f22fb | 178 | randomXoddEven = randomX%2; // find out whether odd or even |
meurigp | 17:4e6f0f7f22fb | 179 | randomYoddEven = randomY%2; |
meurigp | 17:4e6f0f7f22fb | 180 | } |
meurigp | 17:4e6f0f7f22fb | 181 | |
meurigp | 17:4e6f0f7f22fb | 182 | lcd.drawRect(randomX,randomY,1,1,1); |
meurigp | 17:4e6f0f7f22fb | 183 | } |
meurigp | 17:4e6f0f7f22fb | 184 | |
meurigp | 17:4e6f0f7f22fb | 185 | else { |
meurigp | 17:4e6f0f7f22fb | 186 | while (randomXoddEven ==1 || randomYoddEven ==1 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on |
meurigp | 17:4e6f0f7f22fb | 187 | |
meurigp | 17:4e6f0f7f22fb | 188 | randomX = rand() % 83 + 1; // randomX in the range 1 to 83 |
meurigp | 17:4e6f0f7f22fb | 189 | randomY = rand() % 47 + 1; // randomY in the range 1 to 47 |
meurigp | 17:4e6f0f7f22fb | 190 | randomXoddEven = randomX%2; // find out whether odd or even |
meurigp | 17:4e6f0f7f22fb | 191 | randomYoddEven = randomY%2; |
meurigp | 17:4e6f0f7f22fb | 192 | } |
meurigp | 17:4e6f0f7f22fb | 193 | |
meurigp | 17:4e6f0f7f22fb | 194 | lcd.drawRect(randomX,randomY,1,1,1); |
meurigp | 17:4e6f0f7f22fb | 195 | } |
meurigp | 1:97ac723959f2 | 196 | |
meurigp | 1:97ac723959f2 | 197 | } |
meurigp | 3:0e3179c452c5 | 198 | |
meurigp | 5:257b4816ac6a | 199 | void newFruitXY() // new fruit coordinate values are given before it is passed to the generateFood function |
meurigp | 3:0e3179c452c5 | 200 | { |
meurigp | 4:3ceebacef5f1 | 201 | |
meurigp | 3:0e3179c452c5 | 202 | randomX = rand() % 83 + 1; // randomX in the range 1 to 81 |
meurigp | 3:0e3179c452c5 | 203 | randomY = rand() % 47 + 1; // randomY in the range 1 to 47 |
meurigp | 3:0e3179c452c5 | 204 | randomXoddEven = randomX%2; // find out whether odd or even |
meurigp | 3:0e3179c452c5 | 205 | randomYoddEven = randomY%2; |
meurigp | 1:97ac723959f2 | 206 | |
meurigp | 3:0e3179c452c5 | 207 | } |
meurigp | 4:3ceebacef5f1 | 208 | |
meurigp | 4:3ceebacef5f1 | 209 | void moveSnake() { |
meurigp | 4:3ceebacef5f1 | 210 | |
meurigp | 4:3ceebacef5f1 | 211 | if (joystick.direction == LEFT) { |
meurigp | 4:3ceebacef5f1 | 212 | if (currentDirection != right) { // change the currentDirection according to joystick input, providing |
meurigp | 4:3ceebacef5f1 | 213 | currentDirection = left; // it's not the opposite direction to the current direction |
meurigp | 4:3ceebacef5f1 | 214 | } |
meurigp | 4:3ceebacef5f1 | 215 | } |
meurigp | 4:3ceebacef5f1 | 216 | else if (joystick.direction == RIGHT) { |
meurigp | 4:3ceebacef5f1 | 217 | if (currentDirection != left) { // change the currentDirection according to joystick input, providing |
meurigp | 4:3ceebacef5f1 | 218 | currentDirection = right; // it's not the opposite direction to the current direction |
meurigp | 4:3ceebacef5f1 | 219 | } |
meurigp | 4:3ceebacef5f1 | 220 | } |
meurigp | 4:3ceebacef5f1 | 221 | else if (joystick.direction == UP) { |
meurigp | 4:3ceebacef5f1 | 222 | if (currentDirection != down) { // change the currentDirection according to joystick input, providing |
meurigp | 4:3ceebacef5f1 | 223 | currentDirection = up; // it's not the opposite direction to the current direction |
meurigp | 4:3ceebacef5f1 | 224 | } |
meurigp | 4:3ceebacef5f1 | 225 | } |
meurigp | 4:3ceebacef5f1 | 226 | else if (joystick.direction == DOWN) { |
meurigp | 4:3ceebacef5f1 | 227 | if (currentDirection != up) { // change the currentDirection according to joystick input, providing |
meurigp | 4:3ceebacef5f1 | 228 | currentDirection = down; // it's not the opposite direction to the current direction |
meurigp | 4:3ceebacef5f1 | 229 | } |
meurigp | 4:3ceebacef5f1 | 230 | } |
meurigp | 13:08159ea3d556 | 231 | } |
meurigp | 13:08159ea3d556 | 232 | |
meurigp | 13:08159ea3d556 | 233 | void gameLogic() { |
meurigp | 13:08159ea3d556 | 234 | |
meurigp | 13:08159ea3d556 | 235 | lcd.clear(); |
meurigp | 13:08159ea3d556 | 236 | greenLed = 1; |
meurigp | 15:a5590211888c | 237 | |
meurigp | 13:08159ea3d556 | 238 | |
meurigp | 13:08159ea3d556 | 239 | prev_i = snakeTailX[0]; |
meurigp | 13:08159ea3d556 | 240 | prev_j = snakeTailY[0]; |
meurigp | 13:08159ea3d556 | 241 | snakeTailX[0] = i; |
meurigp | 13:08159ea3d556 | 242 | snakeTailY[0] = j; |
meurigp | 8:9b767dd1e549 | 243 | |
meurigp | 15:a5590211888c | 244 | for (int a=1; a<snakeTailLength; a++) { // loops through snakeTail array and assigns previous seg's coordinates to current one |
meurigp | 13:08159ea3d556 | 245 | prev2_i = snakeTailX[a]; |
meurigp | 13:08159ea3d556 | 246 | prev2_j = snakeTailY[a]; |
meurigp | 13:08159ea3d556 | 247 | snakeTailX[a] = prev_i; |
meurigp | 13:08159ea3d556 | 248 | snakeTailY[a] = prev_j; |
meurigp | 13:08159ea3d556 | 249 | prev_i = prev2_i; |
meurigp | 13:08159ea3d556 | 250 | prev_j = prev2_j; |
meurigp | 13:08159ea3d556 | 251 | } |
meurigp | 13:08159ea3d556 | 252 | |
meurigp | 13:08159ea3d556 | 253 | |
meurigp | 13:08159ea3d556 | 254 | if (currentDirection == left) { // shift snake head coordinates according to current direction |
meurigp | 13:08159ea3d556 | 255 | i -= 2; } // move left |
meurigp | 13:08159ea3d556 | 256 | else if (currentDirection == right) { |
meurigp | 13:08159ea3d556 | 257 | i += 2; } // move right |
meurigp | 13:08159ea3d556 | 258 | else if (currentDirection == up) { |
meurigp | 13:08159ea3d556 | 259 | j -= 2; } // move up |
meurigp | 13:08159ea3d556 | 260 | else if (currentDirection == down) { |
meurigp | 13:08159ea3d556 | 261 | j += 2; } // move down |
meurigp | 13:08159ea3d556 | 262 | |
meurigp | 13:08159ea3d556 | 263 | |
meurigp | 13:08159ea3d556 | 264 | lcd.drawRect(i,j,1,1,1); // snake head |
meurigp | 13:08159ea3d556 | 265 | for (int b=0; b<snakeTailLength; b++) { |
meurigp | 13:08159ea3d556 | 266 | lcd.drawRect(snakeTailX[b],snakeTailY[b],1,1,1); |
meurigp | 13:08159ea3d556 | 267 | } |
meurigp | 13:08159ea3d556 | 268 | |
meurigp | 13:08159ea3d556 | 269 | lcd.refresh(); |
meurigp | 12:825a402d230f | 270 | |
meurigp | 13:08159ea3d556 | 271 | lcd.drawRect(randomX,randomY,1,1,1); // food |
meurigp | 18:67d5ae64fbd1 | 272 | |
meurigp | 18:67d5ae64fbd1 | 273 | if (gameType == classicMode) { // map types |
meurigp | 17:4e6f0f7f22fb | 274 | hardWall(); |
meurigp | 17:4e6f0f7f22fb | 275 | } |
meurigp | 17:4e6f0f7f22fb | 276 | else if (gameType == infiniteMode) { |
meurigp | 17:4e6f0f7f22fb | 277 | wrapAround(); |
meurigp | 17:4e6f0f7f22fb | 278 | } |
meurigp | 17:4e6f0f7f22fb | 279 | else { |
meurigp | 17:4e6f0f7f22fb | 280 | specialMap(); |
meurigp | 17:4e6f0f7f22fb | 281 | } |
meurigp | 13:08159ea3d556 | 282 | |
meurigp | 13:08159ea3d556 | 283 | if (i == randomX && j == randomY) { // if fruit is eaten |
meurigp | 13:08159ea3d556 | 284 | greenLed = 0; |
meurigp | 15:a5590211888c | 285 | buzzer.beep(2000,0.2); |
meurigp | 13:08159ea3d556 | 286 | scoreCalculation(); |
meurigp | 13:08159ea3d556 | 287 | snakeTailLength++; |
meurigp | 13:08159ea3d556 | 288 | newFruitXY(); |
meurigp | 13:08159ea3d556 | 289 | generateFood(); |
meurigp | 13:08159ea3d556 | 290 | } |
meurigp | 15:a5590211888c | 291 | |
meurigp | 15:a5590211888c | 292 | if (snakeTailLength > 4) { // if snake eats itself |
meurigp | 15:a5590211888c | 293 | for (int q=1; q<snakeTailLength; q++) { |
meurigp | 15:a5590211888c | 294 | if (snakeTailX[q] == i && snakeTailY[q] == j) { |
meurigp | 15:a5590211888c | 295 | gameOver(); |
meurigp | 15:a5590211888c | 296 | } |
meurigp | 15:a5590211888c | 297 | } |
meurigp | 15:a5590211888c | 298 | } |
meurigp | 14:56e355c5cfc9 | 299 | |
meurigp | 13:08159ea3d556 | 300 | } |
meurigp | 12:825a402d230f | 301 | |
meurigp | 10:7820b46476ea | 302 | |
meurigp | 5:257b4816ac6a | 303 | void hardWall() { |
meurigp | 5:257b4816ac6a | 304 | |
meurigp | 5:257b4816ac6a | 305 | lcd.drawRect(0,0,83,47,0); |
meurigp | 6:9a5706a27240 | 306 | lcd.refresh(); |
meurigp | 6:9a5706a27240 | 307 | if (i == 0 || i+1 == 0 || |
meurigp | 6:9a5706a27240 | 308 | i == 83 || i+1 == 83 || // if any of the 4 pixels within the snake head touch the border |
meurigp | 6:9a5706a27240 | 309 | j == 0 || j+1 == 0 || |
meurigp | 9:6d7074258c63 | 310 | j == 47 || j+1 == 47) |
meurigp | 9:6d7074258c63 | 311 | { |
meurigp | 20:f634b1060981 | 312 | i = 41; |
meurigp | 20:f634b1060981 | 313 | j = 23; |
meurigp | 9:6d7074258c63 | 314 | gameOver(); |
meurigp | 6:9a5706a27240 | 315 | } |
meurigp | 5:257b4816ac6a | 316 | |
meurigp | 5:257b4816ac6a | 317 | } |
meurigp | 5:257b4816ac6a | 318 | |
meurigp | 9:6d7074258c63 | 319 | void gameOver() { |
meurigp | 9:6d7074258c63 | 320 | |
meurigp | 9:6d7074258c63 | 321 | redLed = 1; |
meurigp | 9:6d7074258c63 | 322 | if(score > top_score) { |
meurigp | 9:6d7074258c63 | 323 | ////////////////////// Simple writing example ////////////////////////// |
meurigp | 9:6d7074258c63 | 324 | |
meurigp | 9:6d7074258c63 | 325 | // open file for writing ('w') - creates file if it doesn't exist and overwrites |
meurigp | 9:6d7074258c63 | 326 | // if it does. If you wish to add a score onto a list, then you can |
meurigp | 9:6d7074258c63 | 327 | // append instead 'a'. This will open the file if it exists and start |
meurigp | 9:6d7074258c63 | 328 | // writing at the end. It will create the file if it doesn't exist. |
meurigp | 9:6d7074258c63 | 329 | fp = fopen("/sd/topscore.txt", "w"); |
meurigp | 9:6d7074258c63 | 330 | int top_score = score; |
meurigp | 9:6d7074258c63 | 331 | |
meurigp | 9:6d7074258c63 | 332 | if (fp == NULL) { // if it can't open the file then print error message |
meurigp | 9:6d7074258c63 | 333 | serial.printf("Error! Unable to open file!\n"); |
meurigp | 9:6d7074258c63 | 334 | } else { // opened file so can write |
meurigp | 9:6d7074258c63 | 335 | serial.printf("Writing to file...."); |
meurigp | 9:6d7074258c63 | 336 | fprintf(fp, "%d",top_score); // ensure data type matches |
meurigp | 9:6d7074258c63 | 337 | serial.printf("Done.\n"); |
meurigp | 9:6d7074258c63 | 338 | fclose(fp); // ensure you close the file after writing |
meurigp | 9:6d7074258c63 | 339 | } |
meurigp | 9:6d7074258c63 | 340 | } |
meurigp | 9:6d7074258c63 | 341 | gamePlaying = false; |
meurigp | 9:6d7074258c63 | 342 | lcd.clear(); |
meurigp | 15:a5590211888c | 343 | buzzer.beep(294,0.5); |
meurigp | 9:6d7074258c63 | 344 | wait(0.5); |
meurigp | 9:6d7074258c63 | 345 | lcd.printString("Game Over",15,0); // width(6) * n(9) = 54, 84-54 = 30, 30/2 = 15 |
meurigp | 15:a5590211888c | 346 | buzzer.beep(277,0.5); |
meurigp | 9:6d7074258c63 | 347 | wait(0.5); |
meurigp | 9:6d7074258c63 | 348 | |
meurigp | 9:6d7074258c63 | 349 | char buffer[14]; |
meurigp | 9:6d7074258c63 | 350 | int length = sprintf(buffer,"Score:%i",score); // display score of round |
meurigp | 9:6d7074258c63 | 351 | if (length <= 14) { // if string will fit on display |
meurigp | 9:6d7074258c63 | 352 | lcd.printString(buffer,0,1); } // display on screen |
meurigp | 15:a5590211888c | 353 | buzzer.beep(262,2); |
meurigp | 18:67d5ae64fbd1 | 354 | wait(0.5); |
meurigp | 9:6d7074258c63 | 355 | ////////////////////// Simple reading example ////////////////////////// |
meurigp | 9:6d7074258c63 | 356 | |
meurigp | 9:6d7074258c63 | 357 | // now open file for reading |
meurigp | 9:6d7074258c63 | 358 | fp = fopen("/sd/topscore.txt", "r"); |
meurigp | 9:6d7074258c63 | 359 | // int stored_top_score = -1; // -1 to demonstrate it has changed after reading |
meurigp | 9:6d7074258c63 | 360 | |
meurigp | 9:6d7074258c63 | 361 | if (fp == NULL) { // if it can't open the file then print error message |
meurigp | 9:6d7074258c63 | 362 | serial.printf("Error! Unable to open file!\n"); |
meurigp | 9:6d7074258c63 | 363 | } else { // opened file so can write |
meurigp | 9:6d7074258c63 | 364 | fscanf(fp, "%d",&top_score); // ensure data type matches - note address operator (&) |
meurigp | 9:6d7074258c63 | 365 | serial.printf("Read %d from file.\n",top_score); |
meurigp | 9:6d7074258c63 | 366 | char buffer[14]; |
meurigp | 9:6d7074258c63 | 367 | int length = sprintf(buffer,"HI Score:%i",top_score); // display score of round |
meurigp | 9:6d7074258c63 | 368 | if (length <= 14) { // if string will fit on display |
meurigp | 9:6d7074258c63 | 369 | lcd.printString(buffer,0,2); } // display on screen |
meurigp | 9:6d7074258c63 | 370 | fclose(fp); // ensure you close the file after reading |
meurigp | 9:6d7074258c63 | 371 | } |
meurigp | 9:6d7074258c63 | 372 | wait(0.5); |
meurigp | 9:6d7074258c63 | 373 | redLed = 0; |
meurigp | 9:6d7074258c63 | 374 | |
meurigp | 9:6d7074258c63 | 375 | lcd.printString("RB - Restart",0,4); |
meurigp | 9:6d7074258c63 | 376 | wait(0.5); |
meurigp | 9:6d7074258c63 | 377 | lcd.printString("LB - Menu",0,5); |
meurigp | 18:67d5ae64fbd1 | 378 | |
meurigp | 18:67d5ae64fbd1 | 379 | while(1) { |
meurigp | 18:67d5ae64fbd1 | 380 | if (rb_flag == 1) { |
meurigp | 18:67d5ae64fbd1 | 381 | rb_flag = 0; |
meurigp | 18:67d5ae64fbd1 | 382 | gamePlaying = true; |
meurigp | 18:67d5ae64fbd1 | 383 | mainGame(); |
meurigp | 18:67d5ae64fbd1 | 384 | } |
meurigp | 20:f634b1060981 | 385 | rb_flag = 0; |
meurigp | 18:67d5ae64fbd1 | 386 | if (lb_flag == 1) { |
meurigp | 18:67d5ae64fbd1 | 387 | lb_flag = 0; |
meurigp | 19:8907a82ebe09 | 388 | gamePlaying = false; |
meurigp | 19:8907a82ebe09 | 389 | mainMenu(); |
meurigp | 20:f634b1060981 | 390 | } |
meurigp | 18:67d5ae64fbd1 | 391 | sleep(); |
meurigp | 18:67d5ae64fbd1 | 392 | } |
meurigp | 9:6d7074258c63 | 393 | } |
meurigp | 9:6d7074258c63 | 394 | |
meurigp | 9:6d7074258c63 | 395 | |
meurigp | 6:9a5706a27240 | 396 | void specialMap() { |
meurigp | 6:9a5706a27240 | 397 | |
meurigp | 18:67d5ae64fbd1 | 398 | lcd.drawRect(0,0,83,47,0); |
meurigp | 20:f634b1060981 | 399 | lcd.drawRect(15,9,1,31,1); |
meurigp | 20:f634b1060981 | 400 | lcd.drawRect(33,9,1,31,1); |
meurigp | 20:f634b1060981 | 401 | lcd.drawRect(49,9,1,31,1); |
meurigp | 18:67d5ae64fbd1 | 402 | lcd.refresh(); |
meurigp | 18:67d5ae64fbd1 | 403 | if (i == 0 || i+1 == 0 || |
meurigp | 18:67d5ae64fbd1 | 404 | i == 83 || i+1 == 83 || // if any of the 4 pixels within the snake head touch the border |
meurigp | 18:67d5ae64fbd1 | 405 | j == 0 || j+1 == 0 || |
meurigp | 18:67d5ae64fbd1 | 406 | j == 47 || j+1 == 47) |
meurigp | 18:67d5ae64fbd1 | 407 | { |
meurigp | 20:f634b1060981 | 408 | i = 13; |
meurigp | 20:f634b1060981 | 409 | j = 5; |
meurigp | 18:67d5ae64fbd1 | 410 | gameOver(); |
meurigp | 6:9a5706a27240 | 411 | } |
meurigp | 6:9a5706a27240 | 412 | |
meurigp | 6:9a5706a27240 | 413 | } |
meurigp | 6:9a5706a27240 | 414 | |
meurigp | 12:825a402d230f | 415 | void wrapAround() { |
meurigp | 6:9a5706a27240 | 416 | |
meurigp | 14:56e355c5cfc9 | 417 | if (i == -2) { |
meurigp | 14:56e355c5cfc9 | 418 | i = 82; |
meurigp | 6:9a5706a27240 | 419 | } |
meurigp | 6:9a5706a27240 | 420 | if (i == 84) { |
meurigp | 6:9a5706a27240 | 421 | i = 0; |
meurigp | 6:9a5706a27240 | 422 | } |
meurigp | 14:56e355c5cfc9 | 423 | if (j == -2) { |
meurigp | 14:56e355c5cfc9 | 424 | j = 46; |
meurigp | 6:9a5706a27240 | 425 | } |
meurigp | 14:56e355c5cfc9 | 426 | if (j == 48) { |
meurigp | 14:56e355c5cfc9 | 427 | j = 0; |
meurigp | 14:56e355c5cfc9 | 428 | } |
meurigp | 6:9a5706a27240 | 429 | |
meurigp | 12:825a402d230f | 430 | } |
meurigp | 6:9a5706a27240 | 431 | |
meurigp | 6:9a5706a27240 | 432 | |
meurigp | 5:257b4816ac6a | 433 | void scoreCalculation() { |
meurigp | 5:257b4816ac6a | 434 | |
meurigp | 5:257b4816ac6a | 435 | score += fruitValue; // each time fruit is eaten score is calculated and fruit value will increase by 1 |
meurigp | 5:257b4816ac6a | 436 | |
meurigp | 5:257b4816ac6a | 437 | fruitValue++; |
meurigp | 5:257b4816ac6a | 438 | |
meurigp | 8:9b767dd1e549 | 439 | //serial.printf("score = %i\n",score); |
meurigp | 8:9b767dd1e549 | 440 | //serial.printf("fruitValue = %i\n\n",fruitValue); |
meurigp | 5:257b4816ac6a | 441 | |
meurigp | 8:9b767dd1e549 | 442 | } |
meurigp | 8:9b767dd1e549 | 443 | |
meurigp | 8:9b767dd1e549 | 444 | void initSnakeTail() { |
meurigp | 8:9b767dd1e549 | 445 | |
meurigp | 17:4e6f0f7f22fb | 446 | if (gameType == classicMode) { |
meurigp | 8:9b767dd1e549 | 447 | prev_i = 39; |
meurigp | 8:9b767dd1e549 | 448 | prev_j = 23; |
meurigp | 17:4e6f0f7f22fb | 449 | prev2_i = 37; |
meurigp | 17:4e6f0f7f22fb | 450 | prev2_j = 23; |
meurigp | 17:4e6f0f7f22fb | 451 | } |
meurigp | 17:4e6f0f7f22fb | 452 | else if (gameType == infiniteMode) { |
meurigp | 17:4e6f0f7f22fb | 453 | prev_i = 38; |
meurigp | 17:4e6f0f7f22fb | 454 | prev_j = 22; |
meurigp | 17:4e6f0f7f22fb | 455 | prev2_i = 36; |
meurigp | 17:4e6f0f7f22fb | 456 | prev2_j = 22; |
meurigp | 17:4e6f0f7f22fb | 457 | } |
meurigp | 17:4e6f0f7f22fb | 458 | else { |
meurigp | 17:4e6f0f7f22fb | 459 | prev_i = 11; |
meurigp | 17:4e6f0f7f22fb | 460 | prev_j = 5; |
meurigp | 17:4e6f0f7f22fb | 461 | prev2_i = 9; |
meurigp | 17:4e6f0f7f22fb | 462 | prev2_j = 5; |
meurigp | 17:4e6f0f7f22fb | 463 | } |
meurigp | 17:4e6f0f7f22fb | 464 | |
meurigp | 20:f634b1060981 | 465 | snakeTailX[0] = i; |
meurigp | 20:f634b1060981 | 466 | snakeTailY[0] = j; |
meurigp | 8:9b767dd1e549 | 467 | snakeTailX[1] = prev_i; |
meurigp | 8:9b767dd1e549 | 468 | snakeTailY[1] = prev_j; |
meurigp | 8:9b767dd1e549 | 469 | snakeTailX[2] = prev2_i; |
meurigp | 8:9b767dd1e549 | 470 | snakeTailY[2] = prev2_j; |
meurigp | 8:9b767dd1e549 | 471 | |
meurigp | 8:9b767dd1e549 | 472 | lcd.drawRect(i,j,1,1,1); // snake head |
meurigp | 8:9b767dd1e549 | 473 | for (int b=0; b<snakeTailLength; b++) { |
meurigp | 9:6d7074258c63 | 474 | lcd.drawRect(snakeTailX[b],snakeTailY[b],1,1,1); |
meurigp | 9:6d7074258c63 | 475 | } |
meurigp | 9:6d7074258c63 | 476 | currentDirection = right; |
meurigp | 8:9b767dd1e549 | 477 | |
meurigp | 9:6d7074258c63 | 478 | } |
meurigp | 9:6d7074258c63 | 479 | |
meurigp | 9:6d7074258c63 | 480 | void snakeIntro() { |
meurigp | 9:6d7074258c63 | 481 | |
meurigp | 9:6d7074258c63 | 482 | for(int i=0; i<WIDTH; i++) { |
meurigp | 9:6d7074258c63 | 483 | for(int j=0; j<HEIGHT; j++) { |
meurigp | 9:6d7074258c63 | 484 | lcd.setPixel(i,j); |
meurigp | 9:6d7074258c63 | 485 | } |
meurigp | 9:6d7074258c63 | 486 | } |
meurigp | 9:6d7074258c63 | 487 | for (int q=0; q<WIDTH/2; q++) { |
meurigp | 9:6d7074258c63 | 488 | lcd.drawCircle(WIDTH/2,HEIGHT/2,q,2); |
meurigp | 9:6d7074258c63 | 489 | wait_ms(2); |
meurigp | 9:6d7074258c63 | 490 | } // x,y,radius,white fill |
meurigp | 9:6d7074258c63 | 491 | |
meurigp | 9:6d7074258c63 | 492 | lcd.printString("Snake",27,0); |
meurigp | 15:a5590211888c | 493 | buzzer.beep(2000,0.5); |
meurigp | 9:6d7074258c63 | 494 | wait(0.5); |
meurigp | 9:6d7074258c63 | 495 | lcd.printString("by",36,2); |
meurigp | 15:a5590211888c | 496 | buzzer.beep(1000,0.5); |
meurigp | 9:6d7074258c63 | 497 | wait(0.5); |
meurigp | 9:6d7074258c63 | 498 | lcd.printString("Meurig",24,3); |
meurigp | 15:a5590211888c | 499 | buzzer.beep(500,0.5); |
meurigp | 9:6d7074258c63 | 500 | wait(0.5); |
meurigp | 9:6d7074258c63 | 501 | lcd.printString("Phillips",18,4); |
meurigp | 15:a5590211888c | 502 | buzzer.beep(250,2); |
meurigp | 9:6d7074258c63 | 503 | wait(2); |
meurigp | 9:6d7074258c63 | 504 | |
meurigp | 9:6d7074258c63 | 505 | lcd.clear(); |
meurigp | 9:6d7074258c63 | 506 | |
meurigp | 12:825a402d230f | 507 | } |
meurigp | 12:825a402d230f | 508 | |
meurigp | 12:825a402d230f | 509 | void gamePaused() { |
meurigp | 12:825a402d230f | 510 | |
meurigp | 14:56e355c5cfc9 | 511 | wait_ms(100); |
meurigp | 12:825a402d230f | 512 | pauseCount++; |
meurigp | 12:825a402d230f | 513 | lcd.clear(); |
meurigp | 12:825a402d230f | 514 | char buffer[14]; |
meurigp | 12:825a402d230f | 515 | int length = sprintf(buffer,"%i left",3-pauseCount); // number of pauses left |
meurigp | 12:825a402d230f | 516 | if (length <= 14) { // if string will fit on display |
meurigp | 14:56e355c5cfc9 | 517 | lcd.printString(buffer,24,3); } // display on screen |
meurigp | 14:56e355c5cfc9 | 518 | |
meurigp | 14:56e355c5cfc9 | 519 | lcd.printString("Paused 5",18,1); |
meurigp | 14:56e355c5cfc9 | 520 | wait(1); |
meurigp | 14:56e355c5cfc9 | 521 | lcd.printString("Paused 4",18,1); |
meurigp | 14:56e355c5cfc9 | 522 | wait(1); |
meurigp | 14:56e355c5cfc9 | 523 | lcd.printString("Paused 3",18,1); |
meurigp | 14:56e355c5cfc9 | 524 | wait(1); |
meurigp | 14:56e355c5cfc9 | 525 | lcd.printString("Paused 2",18,1); |
meurigp | 14:56e355c5cfc9 | 526 | wait(1); |
meurigp | 14:56e355c5cfc9 | 527 | lcd.printString("Paused 1",18,1); |
meurigp | 14:56e355c5cfc9 | 528 | wait(1); |
meurigp | 14:56e355c5cfc9 | 529 | |
meurigp | 14:56e355c5cfc9 | 530 | } |
meurigp | 14:56e355c5cfc9 | 531 | |
meurigp | 14:56e355c5cfc9 | 532 | void timer_isr() |
meurigp | 14:56e355c5cfc9 | 533 | { |
meurigp | 14:56e355c5cfc9 | 534 | game_timer_flag = 1; |
meurigp | 16:68b9460d4c76 | 535 | } |
meurigp | 16:68b9460d4c76 | 536 | |
meurigp | 16:68b9460d4c76 | 537 | void rb_isr() |
meurigp | 16:68b9460d4c76 | 538 | { |
meurigp | 16:68b9460d4c76 | 539 | rb_flag = 1; |
meurigp | 16:68b9460d4c76 | 540 | } |
meurigp | 16:68b9460d4c76 | 541 | |
meurigp | 16:68b9460d4c76 | 542 | void lb_isr() |
meurigp | 16:68b9460d4c76 | 543 | { |
meurigp | 16:68b9460d4c76 | 544 | lb_flag = 1; |
meurigp | 12:825a402d230f | 545 | } |