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: N5110 PinDetect PowerControl mbed
BrickBreaker.cpp@9:8447ce6f51ae, 2015-05-07 (annotated)
- Committer:
- el13cj
- Date:
- Thu May 07 18:01:40 2015 +0000
- Revision:
- 9:8447ce6f51ae
- Parent:
- 8:7889867308d3
- Child:
- 10:19c6aba3beec
FIXED ball strike-through error; INTERMITTENT fail to destroy brick, persistent menu on game start; TODO (if can) spkr, clear high scores, ball speed select
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el13cj | 8:7889867308d3 | 1 | /** |
el13cj | 9:8447ce6f51ae | 2 | * @file BrickBreaker.cpp |
el13cj | 9:8447ce6f51ae | 3 | * |
el13cj | 9:8447ce6f51ae | 4 | * @brief Function implementations |
el13cj | 9:8447ce6f51ae | 5 | * @brief Credits to Craig A. Evans for his implementation of the 2-axis Joystick. |
el13cj | 9:8447ce6f51ae | 6 | * |
el13cj | 8:7889867308d3 | 7 | */ |
el13cj | 8:7889867308d3 | 8 | |
el13cj | 8:7889867308d3 | 9 | |
el13cj | 8:7889867308d3 | 10 | #include "BrickBreaker.h" |
el13cj | 8:7889867308d3 | 11 | #include "mbed.h" |
el13cj | 8:7889867308d3 | 12 | |
el13cj | 8:7889867308d3 | 13 | |
el13cj | 8:7889867308d3 | 14 | |
el13cj | 8:7889867308d3 | 15 | // Initialise the N5110 screen |
el13cj | 8:7889867308d3 | 16 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
el13cj | 8:7889867308d3 | 17 | // \ \ | | / / / |
el13cj | 8:7889867308d3 | 18 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
el13cj | 8:7889867308d3 | 19 | |
el13cj | 8:7889867308d3 | 20 | int main() { |
el13cj | 8:7889867308d3 | 21 | |
el13cj | 8:7889867308d3 | 22 | |
el13cj | 8:7889867308d3 | 23 | PHY_PowerDown(); |
el13cj | 8:7889867308d3 | 24 | int result = semihost_powerdown(); |
el13cj | 8:7889867308d3 | 25 | |
el13cj | 8:7889867308d3 | 26 | lcd.init(); |
el13cj | 8:7889867308d3 | 27 | calibrateJoystick(); |
el13cj | 8:7889867308d3 | 28 | initBricks(0); |
el13cj | 8:7889867308d3 | 29 | |
el13cj | 8:7889867308d3 | 30 | readHighScores(); |
el13cj | 8:7889867308d3 | 31 | |
el13cj | 8:7889867308d3 | 32 | pollJoystick.attach(&updateJoystick,1.0/10); // read joystick 10 times per second |
el13cj | 8:7889867308d3 | 33 | select.attach(&getMenuSelect,0.15); |
el13cj | 8:7889867308d3 | 34 | |
el13cj | 8:7889867308d3 | 35 | movePaddle.attach(&paddle,0.025); |
el13cj | 8:7889867308d3 | 36 | |
el13cj | 8:7889867308d3 | 37 | initBricks(0); |
el13cj | 8:7889867308d3 | 38 | |
el13cj | 8:7889867308d3 | 39 | button.attach_asserted(&newScreen); |
el13cj | 8:7889867308d3 | 40 | button.setSampleFrequency(); |
el13cj | 8:7889867308d3 | 41 | |
el13cj | 8:7889867308d3 | 42 | playPause.mode(PullUp); |
el13cj | 8:7889867308d3 | 43 | playPause.attach_asserted(&pause); |
el13cj | 8:7889867308d3 | 44 | playPause.setSampleFrequency(); |
el13cj | 8:7889867308d3 | 45 | |
el13cj | 8:7889867308d3 | 46 | while(1) { |
el13cj | 8:7889867308d3 | 47 | |
el13cj | 8:7889867308d3 | 48 | while(displayMenu == 1) { |
el13cj | 8:7889867308d3 | 49 | menu(); |
el13cj | 8:7889867308d3 | 50 | } |
el13cj | 8:7889867308d3 | 51 | while(displayGame == 1) { |
el13cj | 8:7889867308d3 | 52 | game(startStop); |
el13cj | 8:7889867308d3 | 53 | if (lives < 0) { |
el13cj | 8:7889867308d3 | 54 | displayGame = 0; |
el13cj | 8:7889867308d3 | 55 | displayGameOver = 1; |
el13cj | 8:7889867308d3 | 56 | } |
el13cj | 8:7889867308d3 | 57 | if (getNumBricks() == 0) { |
el13cj | 8:7889867308d3 | 58 | newLevel(); |
el13cj | 8:7889867308d3 | 59 | } |
el13cj | 8:7889867308d3 | 60 | } |
el13cj | 8:7889867308d3 | 61 | while(displayHelp == 1) { |
el13cj | 8:7889867308d3 | 62 | help(); |
el13cj | 8:7889867308d3 | 63 | } |
el13cj | 8:7889867308d3 | 64 | while(displayHighScores == 1) { |
el13cj | 8:7889867308d3 | 65 | highScores(); |
el13cj | 8:7889867308d3 | 66 | } |
el13cj | 8:7889867308d3 | 67 | while(displayGameOver == 1) { |
el13cj | 8:7889867308d3 | 68 | gameOver(); |
el13cj | 8:7889867308d3 | 69 | } |
el13cj | 8:7889867308d3 | 70 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 71 | |
el13cj | 8:7889867308d3 | 72 | } |
el13cj | 8:7889867308d3 | 73 | } |
el13cj | 8:7889867308d3 | 74 | |
el13cj | 8:7889867308d3 | 75 | void readHighScores() { |
el13cj | 8:7889867308d3 | 76 | FILE *fp = fopen("/local/scores.txt","r"); |
el13cj | 8:7889867308d3 | 77 | fscanf(fp,"%i",&highscore1); |
el13cj | 8:7889867308d3 | 78 | fscanf(fp,"%i",&highscore2); |
el13cj | 8:7889867308d3 | 79 | fscanf(fp,"%i",&highscore3); |
el13cj | 8:7889867308d3 | 80 | fclose(fp); |
el13cj | 8:7889867308d3 | 81 | } |
el13cj | 8:7889867308d3 | 82 | |
el13cj | 8:7889867308d3 | 83 | void writeHighScores() { |
el13cj | 8:7889867308d3 | 84 | FILE *fp = fopen("/local/scores.txt","w"); |
el13cj | 8:7889867308d3 | 85 | fprintf(fp,"%i\r\n",highscore1); |
el13cj | 8:7889867308d3 | 86 | fprintf(fp,"%i\r\n",highscore2); |
el13cj | 8:7889867308d3 | 87 | fprintf(fp,"%i\r\n",highscore3); |
el13cj | 8:7889867308d3 | 88 | fclose(fp); |
el13cj | 8:7889867308d3 | 89 | } |
el13cj | 8:7889867308d3 | 90 | |
el13cj | 8:7889867308d3 | 91 | void newLevel() |
el13cj | 8:7889867308d3 | 92 | { |
el13cj | 8:7889867308d3 | 93 | lcd.clear(); |
el13cj | 8:7889867308d3 | 94 | level++; |
el13cj | 8:7889867308d3 | 95 | initBricks(level); |
el13cj | 8:7889867308d3 | 96 | by = 30; |
el13cj | 8:7889867308d3 | 97 | bx = 42; |
el13cj | 8:7889867308d3 | 98 | px = 38; |
el13cj | 8:7889867308d3 | 99 | d = 8; |
el13cj | 8:7889867308d3 | 100 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 101 | |
el13cj | 8:7889867308d3 | 102 | |
el13cj | 8:7889867308d3 | 103 | } |
el13cj | 8:7889867308d3 | 104 | |
el13cj | 8:7889867308d3 | 105 | void pause() |
el13cj | 8:7889867308d3 | 106 | { |
el13cj | 8:7889867308d3 | 107 | if (displayGame == 1) { |
el13cj | 8:7889867308d3 | 108 | startStop = !startStop; |
el13cj | 8:7889867308d3 | 109 | } |
el13cj | 8:7889867308d3 | 110 | } |
el13cj | 8:7889867308d3 | 111 | |
el13cj | 8:7889867308d3 | 112 | int getNumBricks() { |
el13cj | 8:7889867308d3 | 113 | int sum = 0; |
el13cj | 8:7889867308d3 | 114 | for (int i = 0; i < 8; i++) { |
el13cj | 8:7889867308d3 | 115 | for (int j = 0; j < 4; j++) { |
el13cj | 8:7889867308d3 | 116 | |
el13cj | 8:7889867308d3 | 117 | sum+=bricks[j][i]; |
el13cj | 8:7889867308d3 | 118 | } |
el13cj | 8:7889867308d3 | 119 | } |
el13cj | 8:7889867308d3 | 120 | return sum; |
el13cj | 8:7889867308d3 | 121 | } |
el13cj | 8:7889867308d3 | 122 | |
el13cj | 8:7889867308d3 | 123 | int lifeLost() { |
el13cj | 8:7889867308d3 | 124 | if (by >= 48 && by <= 50) { |
el13cj | 8:7889867308d3 | 125 | lives-=1; |
el13cj | 8:7889867308d3 | 126 | by = 30; |
el13cj | 8:7889867308d3 | 127 | bx = 42; |
el13cj | 8:7889867308d3 | 128 | px = 38; |
el13cj | 8:7889867308d3 | 129 | d = 8; |
el13cj | 8:7889867308d3 | 130 | startStop = 0; |
el13cj | 8:7889867308d3 | 131 | return 1; |
el13cj | 8:7889867308d3 | 132 | } else { |
el13cj | 8:7889867308d3 | 133 | return 0; |
el13cj | 8:7889867308d3 | 134 | } |
el13cj | 8:7889867308d3 | 135 | } |
el13cj | 8:7889867308d3 | 136 | |
el13cj | 8:7889867308d3 | 137 | void game(int g) { |
el13cj | 8:7889867308d3 | 138 | if (g == 1) { |
el13cj | 8:7889867308d3 | 139 | lifeLost(); |
el13cj | 8:7889867308d3 | 140 | borderInit(); |
el13cj | 8:7889867308d3 | 141 | dispScore(); |
el13cj | 8:7889867308d3 | 142 | dispLives(); |
el13cj | 8:7889867308d3 | 143 | ball(); |
el13cj | 8:7889867308d3 | 144 | } else if (g == 0) { |
el13cj | 8:7889867308d3 | 145 | } |
el13cj | 8:7889867308d3 | 146 | } |
el13cj | 8:7889867308d3 | 147 | |
el13cj | 8:7889867308d3 | 148 | void highScores() { |
el13cj | 8:7889867308d3 | 149 | readHighScores(); |
el13cj | 8:7889867308d3 | 150 | lcd.clear(); |
el13cj | 8:7889867308d3 | 151 | lcd.printString("HIGH SCORES",10,0); |
el13cj | 8:7889867308d3 | 152 | lcd.printString("1/",10,1); |
el13cj | 8:7889867308d3 | 153 | lcd.printNum(highscore1,30,1); |
el13cj | 8:7889867308d3 | 154 | lcd.printString("2/",10,2); |
el13cj | 8:7889867308d3 | 155 | lcd.printNum(highscore2,30,2); |
el13cj | 8:7889867308d3 | 156 | lcd.printString("3/",10,3); |
el13cj | 8:7889867308d3 | 157 | lcd.printNum(highscore3,30,3); |
el13cj | 8:7889867308d3 | 158 | lcd.printString("menu",10,5); |
el13cj | 8:7889867308d3 | 159 | lcd.printString(">",5,5); |
el13cj | 8:7889867308d3 | 160 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 161 | Sleep(); |
el13cj | 8:7889867308d3 | 162 | } |
el13cj | 8:7889867308d3 | 163 | |
el13cj | 8:7889867308d3 | 164 | void help() |
el13cj | 8:7889867308d3 | 165 | { |
el13cj | 8:7889867308d3 | 166 | //menuSelect = 35; |
el13cj | 8:7889867308d3 | 167 | if (helpScreen == 0) { |
el13cj | 8:7889867308d3 | 168 | lcd.clear(); |
el13cj | 8:7889867308d3 | 169 | lcd.printString("HELP",10,0); |
el13cj | 8:7889867308d3 | 170 | lcd.printString("Destroy bricks",0,1); |
el13cj | 8:7889867308d3 | 171 | lcd.printString("to reach the",0,2); |
el13cj | 8:7889867308d3 | 172 | lcd.printString("next level",0,3); |
el13cj | 8:7889867308d3 | 173 | lcd.printString("more",40,5); |
el13cj | 8:7889867308d3 | 174 | lcd.printString("menu",10,5); |
el13cj | 8:7889867308d3 | 175 | lcd.printString(">",helpSelect,5); |
el13cj | 8:7889867308d3 | 176 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 177 | Sleep(); |
el13cj | 8:7889867308d3 | 178 | } else if (helpScreen == 1) { |
el13cj | 8:7889867308d3 | 179 | lcd.clear(); |
el13cj | 8:7889867308d3 | 180 | lcd.printString("HELP",10,0); |
el13cj | 8:7889867308d3 | 181 | lcd.printString("Use joystick",0,1); |
el13cj | 8:7889867308d3 | 182 | lcd.printString("to control",0,2); |
el13cj | 8:7889867308d3 | 183 | lcd.printString("the paddle",0,3); |
el13cj | 8:7889867308d3 | 184 | lcd.printString("more",40,5); |
el13cj | 8:7889867308d3 | 185 | lcd.printString("menu",10,5); |
el13cj | 8:7889867308d3 | 186 | lcd.printString(">",helpSelect,5); |
el13cj | 8:7889867308d3 | 187 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 188 | Sleep(); |
el13cj | 8:7889867308d3 | 189 | } else if (helpScreen == 2) { |
el13cj | 8:7889867308d3 | 190 | lcd.clear(); |
el13cj | 8:7889867308d3 | 191 | lcd.printString("HELP",10,0); |
el13cj | 8:7889867308d3 | 192 | lcd.printString("Pause the game",0,1); |
el13cj | 8:7889867308d3 | 193 | lcd.printString("using the >/||",0,2); |
el13cj | 8:7889867308d3 | 194 | lcd.printString("button",0,3); |
el13cj | 8:7889867308d3 | 195 | lcd.printString("more",40,5); |
el13cj | 8:7889867308d3 | 196 | lcd.printString("menu",10,5); |
el13cj | 8:7889867308d3 | 197 | lcd.printString(">",helpSelect,5); |
el13cj | 8:7889867308d3 | 198 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 199 | Sleep(); |
el13cj | 8:7889867308d3 | 200 | } else if (helpScreen == 3) { |
el13cj | 8:7889867308d3 | 201 | lcd.clear(); |
el13cj | 8:7889867308d3 | 202 | lcd.printString("HELP",10,0); |
el13cj | 8:7889867308d3 | 203 | lcd.printString("Control volume",0,1); |
el13cj | 8:7889867308d3 | 204 | lcd.printString("using the blue",0,2); |
el13cj | 8:7889867308d3 | 205 | lcd.printString("potentiometer",0,3); |
el13cj | 8:7889867308d3 | 206 | lcd.printString("more",40,5); |
el13cj | 8:7889867308d3 | 207 | lcd.printString("menu",10,5); |
el13cj | 8:7889867308d3 | 208 | lcd.printString(">",helpSelect,5); |
el13cj | 8:7889867308d3 | 209 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 210 | Sleep(); |
el13cj | 8:7889867308d3 | 211 | } |
el13cj | 8:7889867308d3 | 212 | } |
el13cj | 8:7889867308d3 | 213 | |
el13cj | 8:7889867308d3 | 214 | void gameOver() { |
el13cj | 8:7889867308d3 | 215 | |
el13cj | 8:7889867308d3 | 216 | |
el13cj | 8:7889867308d3 | 217 | lcd.clear(); |
el13cj | 8:7889867308d3 | 218 | lcd.printString("GAME OVER",10,0); |
el13cj | 8:7889867308d3 | 219 | lcd.printString("score",10,1); |
el13cj | 8:7889867308d3 | 220 | lcd.printNum(score,42,1); |
el13cj | 8:7889867308d3 | 221 | lcd.printString(">menu",10,5); |
el13cj | 8:7889867308d3 | 222 | if ((score >= highscore1) && scoreFlag) { |
el13cj | 8:7889867308d3 | 223 | highscore3 = highscore2; |
el13cj | 8:7889867308d3 | 224 | highscore2 = highscore1; |
el13cj | 8:7889867308d3 | 225 | highscore1 = score; |
el13cj | 8:7889867308d3 | 226 | writeHighScores(); |
el13cj | 8:7889867308d3 | 227 | scoreFlag = 0; |
el13cj | 8:7889867308d3 | 228 | } else if ((score >= highscore2) && scoreFlag) { |
el13cj | 8:7889867308d3 | 229 | highscore3 = highscore2; |
el13cj | 8:7889867308d3 | 230 | highscore2 = score; |
el13cj | 8:7889867308d3 | 231 | writeHighScores(); |
el13cj | 8:7889867308d3 | 232 | scoreFlag = 0; |
el13cj | 8:7889867308d3 | 233 | } else if ((score >= highscore3) && scoreFlag) { |
el13cj | 8:7889867308d3 | 234 | highscore3 = score; |
el13cj | 8:7889867308d3 | 235 | writeHighScores(); |
el13cj | 8:7889867308d3 | 236 | scoreFlag = 0; |
el13cj | 8:7889867308d3 | 237 | } |
el13cj | 8:7889867308d3 | 238 | if (score >= highscore3) { |
el13cj | 8:7889867308d3 | 239 | lcd.printString("NEW HIGH SCORE",0,3); |
el13cj | 8:7889867308d3 | 240 | } |
el13cj | 8:7889867308d3 | 241 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 242 | Sleep(); |
el13cj | 8:7889867308d3 | 243 | } |
el13cj | 8:7889867308d3 | 244 | |
el13cj | 8:7889867308d3 | 245 | void newScreen() |
el13cj | 8:7889867308d3 | 246 | { |
el13cj | 8:7889867308d3 | 247 | if (displayMenu) { |
el13cj | 8:7889867308d3 | 248 | switch (menuSelect) { |
el13cj | 8:7889867308d3 | 249 | case 1: |
el13cj | 8:7889867308d3 | 250 | lcd.clear(); |
el13cj | 8:7889867308d3 | 251 | displayMenu = 0; |
el13cj | 8:7889867308d3 | 252 | displayGame = 1; |
el13cj | 8:7889867308d3 | 253 | break; |
el13cj | 8:7889867308d3 | 254 | case 2: |
el13cj | 8:7889867308d3 | 255 | displayMenu = 0; |
el13cj | 8:7889867308d3 | 256 | displayHelp = 1; |
el13cj | 8:7889867308d3 | 257 | break; |
el13cj | 8:7889867308d3 | 258 | case 3: |
el13cj | 8:7889867308d3 | 259 | displayMenu = 0; |
el13cj | 8:7889867308d3 | 260 | displayHighScores = 1; |
el13cj | 8:7889867308d3 | 261 | break; |
el13cj | 8:7889867308d3 | 262 | } |
el13cj | 8:7889867308d3 | 263 | } else if (displayHelp) { |
el13cj | 8:7889867308d3 | 264 | switch (helpSelect) { |
el13cj | 8:7889867308d3 | 265 | case 5: |
el13cj | 8:7889867308d3 | 266 | displayMenu = 1; |
el13cj | 8:7889867308d3 | 267 | displayHelp = 0; |
el13cj | 8:7889867308d3 | 268 | break; |
el13cj | 8:7889867308d3 | 269 | case 35: |
el13cj | 8:7889867308d3 | 270 | helpScreen++; |
el13cj | 8:7889867308d3 | 271 | if (helpScreen > 3) { |
el13cj | 8:7889867308d3 | 272 | helpScreen = 0; |
el13cj | 8:7889867308d3 | 273 | } |
el13cj | 8:7889867308d3 | 274 | break; |
el13cj | 8:7889867308d3 | 275 | } |
el13cj | 8:7889867308d3 | 276 | } else if (displayHighScores) { |
el13cj | 8:7889867308d3 | 277 | displayMenu = 1; |
el13cj | 8:7889867308d3 | 278 | displayHighScores = 0; |
el13cj | 8:7889867308d3 | 279 | } else if (displayGame) { |
el13cj | 8:7889867308d3 | 280 | startStop = !startStop; |
el13cj | 8:7889867308d3 | 281 | } else if (displayGameOver) { |
el13cj | 8:7889867308d3 | 282 | displayMenu = 1; |
el13cj | 8:7889867308d3 | 283 | displayGameOver = 0; |
el13cj | 8:7889867308d3 | 284 | } |
el13cj | 8:7889867308d3 | 285 | } |
el13cj | 8:7889867308d3 | 286 | |
el13cj | 8:7889867308d3 | 287 | void getMenuSelect() |
el13cj | 8:7889867308d3 | 288 | { |
el13cj | 8:7889867308d3 | 289 | if (displayMenu) { |
el13cj | 8:7889867308d3 | 290 | |
el13cj | 8:7889867308d3 | 291 | |
el13cj | 8:7889867308d3 | 292 | if (joystick.direction == DOWN) { |
el13cj | 8:7889867308d3 | 293 | lcd.clearRect(5,(menuSelect*8),5,8); |
el13cj | 8:7889867308d3 | 294 | if (menuSelect < 3) { |
el13cj | 8:7889867308d3 | 295 | menuSelect++; |
el13cj | 8:7889867308d3 | 296 | } else if (menuSelect == 3) { |
el13cj | 8:7889867308d3 | 297 | menuSelect = 1; |
el13cj | 8:7889867308d3 | 298 | } |
el13cj | 8:7889867308d3 | 299 | } else if (joystick.direction == UP) { |
el13cj | 8:7889867308d3 | 300 | lcd.clearRect(5,(menuSelect*8),5,8); |
el13cj | 8:7889867308d3 | 301 | if (menuSelect > 1) { |
el13cj | 8:7889867308d3 | 302 | menuSelect-=1; |
el13cj | 8:7889867308d3 | 303 | } else if (menuSelect == 1) { |
el13cj | 8:7889867308d3 | 304 | menuSelect = 3; |
el13cj | 8:7889867308d3 | 305 | } |
el13cj | 8:7889867308d3 | 306 | } |
el13cj | 8:7889867308d3 | 307 | } else if (displayHelp) { |
el13cj | 8:7889867308d3 | 308 | |
el13cj | 8:7889867308d3 | 309 | |
el13cj | 8:7889867308d3 | 310 | if (joystick.direction == RIGHT) { |
el13cj | 8:7889867308d3 | 311 | lcd.clearRect(helpSelect,40,5,8); |
el13cj | 8:7889867308d3 | 312 | |
el13cj | 8:7889867308d3 | 313 | if (helpSelect < 35) { |
el13cj | 8:7889867308d3 | 314 | helpSelect+=30; |
el13cj | 8:7889867308d3 | 315 | } else if (menuSelect == 35) { |
el13cj | 8:7889867308d3 | 316 | helpSelect = 5; |
el13cj | 8:7889867308d3 | 317 | } |
el13cj | 8:7889867308d3 | 318 | } else if (joystick.direction == LEFT) { |
el13cj | 8:7889867308d3 | 319 | lcd.clearRect(helpSelect,40,5,8); |
el13cj | 8:7889867308d3 | 320 | |
el13cj | 8:7889867308d3 | 321 | if (helpSelect > 5) { |
el13cj | 8:7889867308d3 | 322 | helpSelect-=30; |
el13cj | 8:7889867308d3 | 323 | } else if (menuSelect == 5) { |
el13cj | 8:7889867308d3 | 324 | helpSelect = 35; |
el13cj | 8:7889867308d3 | 325 | } |
el13cj | 8:7889867308d3 | 326 | } |
el13cj | 8:7889867308d3 | 327 | } |
el13cj | 8:7889867308d3 | 328 | } |
el13cj | 8:7889867308d3 | 329 | |
el13cj | 8:7889867308d3 | 330 | void menu() { |
el13cj | 8:7889867308d3 | 331 | lcd.clear(); |
el13cj | 8:7889867308d3 | 332 | lcd.printString("BRICK BREAKER",0,0); |
el13cj | 8:7889867308d3 | 333 | lcd.printString("Start",10,1); |
el13cj | 8:7889867308d3 | 334 | lcd.printString("Help",10,2); |
el13cj | 8:7889867308d3 | 335 | lcd.printString("Scores",10,3); |
el13cj | 8:7889867308d3 | 336 | lcd.printString(">",5,menuSelect); |
el13cj | 8:7889867308d3 | 337 | lives = 3; |
el13cj | 8:7889867308d3 | 338 | score = 0; |
el13cj | 8:7889867308d3 | 339 | initBricks(0); |
el13cj | 8:7889867308d3 | 340 | startStop = 1; |
el13cj | 8:7889867308d3 | 341 | level = 0; |
el13cj | 8:7889867308d3 | 342 | scoreFlag = 1; |
el13cj | 8:7889867308d3 | 343 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 344 | Sleep(); |
el13cj | 8:7889867308d3 | 345 | } |
el13cj | 8:7889867308d3 | 346 | |
el13cj | 8:7889867308d3 | 347 | void dispLives() { |
el13cj | 8:7889867308d3 | 348 | |
el13cj | 8:7889867308d3 | 349 | lcd.printNum(lives,77,5); |
el13cj | 8:7889867308d3 | 350 | lcd.printChar(0x80,77,4); |
el13cj | 8:7889867308d3 | 351 | } |
el13cj | 8:7889867308d3 | 352 | |
el13cj | 8:7889867308d3 | 353 | void dispScore() { |
el13cj | 8:7889867308d3 | 354 | |
el13cj | 8:7889867308d3 | 355 | |
el13cj | 8:7889867308d3 | 356 | if (score<10) { |
el13cj | 8:7889867308d3 | 357 | lcd.printNum(score,77,0); |
el13cj | 8:7889867308d3 | 358 | } else if (score<100) { |
el13cj | 8:7889867308d3 | 359 | |
el13cj | 8:7889867308d3 | 360 | lcd.printNum(((score/10)%10),77,0); |
el13cj | 8:7889867308d3 | 361 | lcd.printNum((score%10),77,1); |
el13cj | 8:7889867308d3 | 362 | |
el13cj | 8:7889867308d3 | 363 | } else if (score<1000) { |
el13cj | 8:7889867308d3 | 364 | |
el13cj | 8:7889867308d3 | 365 | lcd.printNum(((score/100)%10),77,0); |
el13cj | 8:7889867308d3 | 366 | lcd.printNum(((score/10)%10),77,1); |
el13cj | 8:7889867308d3 | 367 | lcd.printNum((score%10),77,2); |
el13cj | 8:7889867308d3 | 368 | |
el13cj | 8:7889867308d3 | 369 | } |
el13cj | 8:7889867308d3 | 370 | } |
el13cj | 8:7889867308d3 | 371 | |
el13cj | 8:7889867308d3 | 372 | void doBricks() { |
el13cj | 8:7889867308d3 | 373 | |
el13cj | 8:7889867308d3 | 374 | for (int x = 0; x<8; x++) { |
el13cj | 8:7889867308d3 | 375 | for (int y = 0; y<4; y++) { |
el13cj | 8:7889867308d3 | 376 | getBrickTouch(x,y); |
el13cj | 8:7889867308d3 | 377 | } |
el13cj | 8:7889867308d3 | 378 | } |
el13cj | 8:7889867308d3 | 379 | |
el13cj | 8:7889867308d3 | 380 | for (int i = 0; i < 4; i++) { |
el13cj | 8:7889867308d3 | 381 | for (int j = 0; j < 8; j++) { |
el13cj | 8:7889867308d3 | 382 | if (bricks[i][j] && brickDrawFlag[i][j]) { |
el13cj | 8:7889867308d3 | 383 | lcd.drawRect(bricksx[j],bricksy[i], 6, 2, 1); |
el13cj | 8:7889867308d3 | 384 | brickDrawFlag[i][j] = 0; |
el13cj | 8:7889867308d3 | 385 | } |
el13cj | 8:7889867308d3 | 386 | } |
el13cj | 8:7889867308d3 | 387 | } |
el13cj | 8:7889867308d3 | 388 | |
el13cj | 8:7889867308d3 | 389 | clearBricks(); |
el13cj | 8:7889867308d3 | 390 | } |
el13cj | 8:7889867308d3 | 391 | |
el13cj | 8:7889867308d3 | 392 | void initBricks(int l) |
el13cj | 8:7889867308d3 | 393 | { |
el13cj | 8:7889867308d3 | 394 | |
el13cj | 8:7889867308d3 | 395 | int bricksTemp[4][8]; |
el13cj | 8:7889867308d3 | 396 | |
el13cj | 8:7889867308d3 | 397 | if (l == 0) { |
el13cj | 8:7889867308d3 | 398 | for (int i = 0; i < 4; i++) { |
el13cj | 8:7889867308d3 | 399 | for (int j = 0; j < 8; j++) { |
el13cj | 8:7889867308d3 | 400 | bricks[i][j] = 1; |
el13cj | 8:7889867308d3 | 401 | brickDrawFlag[i][j] = 1; |
el13cj | 8:7889867308d3 | 402 | } |
el13cj | 8:7889867308d3 | 403 | } |
el13cj | 8:7889867308d3 | 404 | } else if (l == 1) { |
el13cj | 8:7889867308d3 | 405 | for (int i = 0; i < 4; i++) { |
el13cj | 8:7889867308d3 | 406 | for (int j = 0; j < 8; j+=2) { |
el13cj | 8:7889867308d3 | 407 | bricks[i][j] = 1; |
el13cj | 8:7889867308d3 | 408 | brickDrawFlag[i][j] = 1; |
el13cj | 8:7889867308d3 | 409 | } |
el13cj | 8:7889867308d3 | 410 | } |
el13cj | 8:7889867308d3 | 411 | } else if (l > 1) { |
el13cj | 8:7889867308d3 | 412 | for (int i = 0; i < 4; i++) { |
el13cj | 8:7889867308d3 | 413 | for (int j = 0; j < 8; j++) { |
el13cj | 8:7889867308d3 | 414 | int mint = ceil((noise.read() * 100000)); |
el13cj | 8:7889867308d3 | 415 | bricksTemp[i][j] = mint%100; |
el13cj | 8:7889867308d3 | 416 | if (bricksTemp[i][j] > ((l*57)%73)) { |
el13cj | 8:7889867308d3 | 417 | bricks[i][j] = 1; |
el13cj | 8:7889867308d3 | 418 | brickDrawFlag[i][j] = 1; |
el13cj | 8:7889867308d3 | 419 | } |
el13cj | 8:7889867308d3 | 420 | } |
el13cj | 8:7889867308d3 | 421 | } |
el13cj | 8:7889867308d3 | 422 | } |
el13cj | 8:7889867308d3 | 423 | } |
el13cj | 8:7889867308d3 | 424 | |
el13cj | 8:7889867308d3 | 425 | void clearBricks() { |
el13cj | 8:7889867308d3 | 426 | for (int i = 0; i < 8; i++) { |
el13cj | 8:7889867308d3 | 427 | for (int j = 0; j < 4; j++) { |
el13cj | 8:7889867308d3 | 428 | if (clearFlag[j][i]) { |
el13cj | 8:7889867308d3 | 429 | lcd.clearRect(bricksx[i],bricksy[j],7,3); |
el13cj | 8:7889867308d3 | 430 | clearFlag[j][i] = 0; |
el13cj | 8:7889867308d3 | 431 | score++; |
el13cj | 8:7889867308d3 | 432 | |
el13cj | 8:7889867308d3 | 433 | } |
el13cj | 8:7889867308d3 | 434 | } |
el13cj | 8:7889867308d3 | 435 | } |
el13cj | 8:7889867308d3 | 436 | |
el13cj | 8:7889867308d3 | 437 | } |
el13cj | 8:7889867308d3 | 438 | |
el13cj | 8:7889867308d3 | 439 | |
el13cj | 8:7889867308d3 | 440 | void getBrickTouch(int x, int y) |
el13cj | 8:7889867308d3 | 441 | { |
el13cj | 8:7889867308d3 | 442 | |
el13cj | 8:7889867308d3 | 443 | if (bricks[y][x]) { |
el13cj | 9:8447ce6f51ae | 444 | for (int a = -1; a < 8; a++) { |
el13cj | 8:7889867308d3 | 445 | if (lcd.getPixel(bricksx[x]+a,bricksy[y]-1)) { |
el13cj | 8:7889867308d3 | 446 | clearFlag[y][x] = 1; |
el13cj | 8:7889867308d3 | 447 | bricks[y][x] = 0; |
el13cj | 8:7889867308d3 | 448 | } else if (lcd.getPixel(bricksx[x]+a,bricksy[y]+3)) { |
el13cj | 8:7889867308d3 | 449 | clearFlag[y][x] = 1; |
el13cj | 8:7889867308d3 | 450 | bricks[y][x] = 0; |
el13cj | 8:7889867308d3 | 451 | } |
el13cj | 8:7889867308d3 | 452 | } |
el13cj | 9:8447ce6f51ae | 453 | for (int b = -1; b < 3; b++) { |
el13cj | 8:7889867308d3 | 454 | if (lcd.getPixel(bricksx[x]-1,bricksy[y]+b)) { |
el13cj | 8:7889867308d3 | 455 | clearFlag[y][x] = 1; |
el13cj | 8:7889867308d3 | 456 | bricks[y][x] = 0; |
el13cj | 8:7889867308d3 | 457 | } else if (lcd.getPixel(bricksx[x]+7,bricksy[y]+b)) { |
el13cj | 8:7889867308d3 | 458 | clearFlag[y][x] = 1; |
el13cj | 8:7889867308d3 | 459 | bricks[y][x] = 0; |
el13cj | 8:7889867308d3 | 460 | } |
el13cj | 8:7889867308d3 | 461 | } |
el13cj | 8:7889867308d3 | 462 | } |
el13cj | 8:7889867308d3 | 463 | } |
el13cj | 8:7889867308d3 | 464 | |
el13cj | 8:7889867308d3 | 465 | |
el13cj | 8:7889867308d3 | 466 | // read default positions of the joystick to calibrate later readings |
el13cj | 8:7889867308d3 | 467 | void calibrateJoystick() |
el13cj | 8:7889867308d3 | 468 | { |
el13cj | 8:7889867308d3 | 469 | button.mode(PullUp); |
el13cj | 8:7889867308d3 | 470 | // must not move during calibration |
el13cj | 8:7889867308d3 | 471 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
el13cj | 8:7889867308d3 | 472 | joystick.y0 = yPot; |
el13cj | 8:7889867308d3 | 473 | } |
el13cj | 8:7889867308d3 | 474 | |
el13cj | 8:7889867308d3 | 475 | |
el13cj | 8:7889867308d3 | 476 | void updateJoystick() |
el13cj | 8:7889867308d3 | 477 | { |
el13cj | 8:7889867308d3 | 478 | if (startStop == 1) { |
el13cj | 8:7889867308d3 | 479 | |
el13cj | 8:7889867308d3 | 480 | |
el13cj | 8:7889867308d3 | 481 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
el13cj | 8:7889867308d3 | 482 | joystick.x = xPot - joystick.x0; |
el13cj | 8:7889867308d3 | 483 | joystick.y = yPot - joystick.y0; |
el13cj | 8:7889867308d3 | 484 | // read button state |
el13cj | 8:7889867308d3 | 485 | |
el13cj | 8:7889867308d3 | 486 | // calculate direction depending on x,y values |
el13cj | 8:7889867308d3 | 487 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
el13cj | 8:7889867308d3 | 488 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 489 | joystick.direction = CENTRE; |
el13cj | 8:7889867308d3 | 490 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 491 | joystick.direction = UP; |
el13cj | 8:7889867308d3 | 492 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 493 | joystick.direction = DOWN; |
el13cj | 8:7889867308d3 | 494 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 495 | joystick.direction = RIGHT; |
el13cj | 8:7889867308d3 | 496 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 497 | joystick.direction = LEFT; |
el13cj | 8:7889867308d3 | 498 | } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 499 | joystick.direction = UPRIGHT; |
el13cj | 8:7889867308d3 | 500 | } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y > -1*DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 501 | joystick.direction = DOWNRIGHT; |
el13cj | 8:7889867308d3 | 502 | } else if ( joystick.y < DIRECTION_TOLERANCE && joystick.x < -1*DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 503 | joystick.direction = UPLEFT; |
el13cj | 8:7889867308d3 | 504 | } else if ( joystick.x < -1*DIRECTION_TOLERANCE && joystick.y > -1*DIRECTION_TOLERANCE) { |
el13cj | 8:7889867308d3 | 505 | joystick.direction = DOWNLEFT; |
el13cj | 8:7889867308d3 | 506 | } else { |
el13cj | 8:7889867308d3 | 507 | joystick.direction = UNKNOWN; |
el13cj | 8:7889867308d3 | 508 | } |
el13cj | 8:7889867308d3 | 509 | } else if (startStop == 0) { |
el13cj | 8:7889867308d3 | 510 | joystick.direction = CENTRE; |
el13cj | 8:7889867308d3 | 511 | } |
el13cj | 8:7889867308d3 | 512 | |
el13cj | 8:7889867308d3 | 513 | } |
el13cj | 8:7889867308d3 | 514 | |
el13cj | 8:7889867308d3 | 515 | |
el13cj | 8:7889867308d3 | 516 | //function to move the game paddle left and right with the joystick |
el13cj | 8:7889867308d3 | 517 | void paddle() |
el13cj | 8:7889867308d3 | 518 | { |
el13cj | 8:7889867308d3 | 519 | if (!displayGame) return; |
el13cj | 8:7889867308d3 | 520 | |
el13cj | 8:7889867308d3 | 521 | lcd.clearRect(1,py,74,3); |
el13cj | 8:7889867308d3 | 522 | |
el13cj | 8:7889867308d3 | 523 | if ((joystick.direction == RIGHT || joystick.direction == UPRIGHT || joystick.direction == DOWNRIGHT) && (px < 67)) { |
el13cj | 8:7889867308d3 | 524 | px = px+1; |
el13cj | 8:7889867308d3 | 525 | lcd.drawRect(px,py,pw,ph,1); |
el13cj | 8:7889867308d3 | 526 | } else if ((joystick.direction == LEFT || joystick.direction == UPLEFT || joystick.direction == DOWNLEFT) && (px > 0)) { |
el13cj | 8:7889867308d3 | 527 | px = px-1; |
el13cj | 8:7889867308d3 | 528 | lcd.drawRect(px,py,pw,ph,1); |
el13cj | 8:7889867308d3 | 529 | } else { |
el13cj | 8:7889867308d3 | 530 | lcd.drawRect(px,py,pw,ph,1); |
el13cj | 8:7889867308d3 | 531 | } |
el13cj | 8:7889867308d3 | 532 | |
el13cj | 8:7889867308d3 | 533 | } |
el13cj | 8:7889867308d3 | 534 | |
el13cj | 8:7889867308d3 | 535 | |
el13cj | 8:7889867308d3 | 536 | |
el13cj | 8:7889867308d3 | 537 | //set the touchFlag if any of the pixels touching the ball are set |
el13cj | 8:7889867308d3 | 538 | //store which pixels are set in an array |
el13cj | 8:7889867308d3 | 539 | void getTouchFlag() |
el13cj | 8:7889867308d3 | 540 | { |
el13cj | 8:7889867308d3 | 541 | if (lcd.getPixel(bx-1,by)) {//11 |
el13cj | 8:7889867308d3 | 542 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 543 | surround[11] = 1; |
el13cj | 8:7889867308d3 | 544 | } if (lcd.getPixel(bx-1,by-1)) {//0 |
el13cj | 8:7889867308d3 | 545 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 546 | surround[0] = 1; |
el13cj | 8:7889867308d3 | 547 | } if (lcd.getPixel(bx,by-1)) {//1 |
el13cj | 8:7889867308d3 | 548 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 549 | surround[1] = 1; |
el13cj | 8:7889867308d3 | 550 | } if (lcd.getPixel(bx+1,by-1)) {//2 |
el13cj | 8:7889867308d3 | 551 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 552 | surround[2] = 1; |
el13cj | 8:7889867308d3 | 553 | } if (lcd.getPixel(bx+2,by-1)) {//3 |
el13cj | 8:7889867308d3 | 554 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 555 | surround[3] = 1; |
el13cj | 8:7889867308d3 | 556 | } if (lcd.getPixel(bx+2,by)) {//4 |
el13cj | 8:7889867308d3 | 557 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 558 | surround[4] = 1; |
el13cj | 8:7889867308d3 | 559 | } if (lcd.getPixel(bx+2,by+1)) {//5 |
el13cj | 8:7889867308d3 | 560 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 561 | surround[5] = 1; |
el13cj | 8:7889867308d3 | 562 | } if (lcd.getPixel(bx+2,by+2)) {//6 |
el13cj | 8:7889867308d3 | 563 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 564 | surround[6] = 1; |
el13cj | 8:7889867308d3 | 565 | } if (lcd.getPixel(bx+1,by+2)) {//7 |
el13cj | 8:7889867308d3 | 566 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 567 | surround[7] = 1; |
el13cj | 8:7889867308d3 | 568 | } if (lcd.getPixel(bx,by+2)) {//8 |
el13cj | 8:7889867308d3 | 569 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 570 | surround[8] = 1; |
el13cj | 8:7889867308d3 | 571 | } if (lcd.getPixel(bx-1,by+2)) {//9 |
el13cj | 8:7889867308d3 | 572 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 573 | surround[9] = 1; |
el13cj | 8:7889867308d3 | 574 | } if (lcd.getPixel(bx-1,by+1)) {//10 |
el13cj | 8:7889867308d3 | 575 | touchFlag = 1; |
el13cj | 8:7889867308d3 | 576 | surround[10] = 1; |
el13cj | 8:7889867308d3 | 577 | } |
el13cj | 8:7889867308d3 | 578 | } |
el13cj | 8:7889867308d3 | 579 | |
el13cj | 8:7889867308d3 | 580 | //work out what the ball has hit and calculate/set the new angle accordingly |
el13cj | 8:7889867308d3 | 581 | int setAngle() |
el13cj | 8:7889867308d3 | 582 | { |
el13cj | 8:7889867308d3 | 583 | getTouchFlag(); |
el13cj | 8:7889867308d3 | 584 | |
el13cj | 8:7889867308d3 | 585 | if (touchFlag) { |
el13cj | 8:7889867308d3 | 586 | |
el13cj | 8:7889867308d3 | 587 | //FOR 3 CORNER SQUARES |
el13cj | 8:7889867308d3 | 588 | if (surround[11] && surround[0] && surround[1]) { //top right corner |
el13cj | 8:7889867308d3 | 589 | d = 6; |
el13cj | 8:7889867308d3 | 590 | } else if (surround[2] && surround[3] && surround[4]) { //bottom right corner |
el13cj | 8:7889867308d3 | 591 | d = 10; |
el13cj | 8:7889867308d3 | 592 | } else if (surround[5] && surround[6] && surround[7]) { //bottom left corner |
el13cj | 8:7889867308d3 | 593 | d = 14; |
el13cj | 8:7889867308d3 | 594 | } else if (surround[8] && surround[9] && surround[10]) { //top left corner |
el13cj | 8:7889867308d3 | 595 | d = 2; |
el13cj | 8:7889867308d3 | 596 | } |
el13cj | 8:7889867308d3 | 597 | //FOR 3 TOUCHING OFFCENTRE EDGE SQUARES |
el13cj | 8:7889867308d3 | 598 | else if (surround[0] && surround[1] && surround[2] && !surround[3]) { //top edge left |
el13cj | 8:7889867308d3 | 599 | d = 5; |
el13cj | 8:7889867308d3 | 600 | } else if (surround[3] && surround[1] && surround[2] && !surround[0]) { //top edge right |
el13cj | 8:7889867308d3 | 601 | d = 11; |
el13cj | 8:7889867308d3 | 602 | } else if (surround[6] && surround[7] && surround[8] && !surround[9]) { // low edge right |
el13cj | 8:7889867308d3 | 603 | d = 13; |
el13cj | 8:7889867308d3 | 604 | } else if (surround[7] && surround[8] && surround[9] && !surround[6]) { //low edge left |
el13cj | 8:7889867308d3 | 605 | d = 3; |
el13cj | 8:7889867308d3 | 606 | } else if (surround[0] && surround[11] && surround[10] && !surround[9]) { //left edge top |
el13cj | 8:7889867308d3 | 607 | d = 5; |
el13cj | 8:7889867308d3 | 608 | } else if (surround[3] && surround[4] && surround[5] && !surround[6]) { //right edge top |
el13cj | 8:7889867308d3 | 609 | d = 11; |
el13cj | 8:7889867308d3 | 610 | } else if (surround[11] && surround[10] && surround[9] && !surround[0]) { //left edge low |
el13cj | 8:7889867308d3 | 611 | d = 3; |
el13cj | 8:7889867308d3 | 612 | } else if (surround[4] && surround[5] && surround[6] && !surround[3]) { //right edge low |
el13cj | 8:7889867308d3 | 613 | d = 13; |
el13cj | 8:7889867308d3 | 614 | } |
el13cj | 8:7889867308d3 | 615 | //FOR CeNTRE SQUARES |
el13cj | 8:7889867308d3 | 616 | else if (surround[1] && surround[2]) { //top edge |
el13cj | 8:7889867308d3 | 617 | switch (d) { |
el13cj | 8:7889867308d3 | 618 | case 3: |
el13cj | 8:7889867308d3 | 619 | d = 5; |
el13cj | 8:7889867308d3 | 620 | break; |
el13cj | 8:7889867308d3 | 621 | case 2: |
el13cj | 8:7889867308d3 | 622 | d = 6; |
el13cj | 8:7889867308d3 | 623 | break; |
el13cj | 8:7889867308d3 | 624 | case 1: |
el13cj | 8:7889867308d3 | 625 | d = 7; |
el13cj | 8:7889867308d3 | 626 | break; |
el13cj | 8:7889867308d3 | 627 | case 0: |
el13cj | 8:7889867308d3 | 628 | d = 8; |
el13cj | 8:7889867308d3 | 629 | break; |
el13cj | 8:7889867308d3 | 630 | case 15: |
el13cj | 8:7889867308d3 | 631 | d = 9; |
el13cj | 8:7889867308d3 | 632 | break; |
el13cj | 8:7889867308d3 | 633 | case 14: |
el13cj | 8:7889867308d3 | 634 | d = 10; |
el13cj | 8:7889867308d3 | 635 | break; |
el13cj | 8:7889867308d3 | 636 | case 13: |
el13cj | 8:7889867308d3 | 637 | d = 11; |
el13cj | 8:7889867308d3 | 638 | break; |
el13cj | 8:7889867308d3 | 639 | } |
el13cj | 8:7889867308d3 | 640 | |
el13cj | 8:7889867308d3 | 641 | } else if (surround[4] && surround[5]) { //RHS edge |
el13cj | 8:7889867308d3 | 642 | switch (d) { |
el13cj | 8:7889867308d3 | 643 | case 1: |
el13cj | 8:7889867308d3 | 644 | d = 15; |
el13cj | 8:7889867308d3 | 645 | break; |
el13cj | 8:7889867308d3 | 646 | case 2: |
el13cj | 8:7889867308d3 | 647 | d = 14; |
el13cj | 8:7889867308d3 | 648 | break; |
el13cj | 8:7889867308d3 | 649 | case 3: |
el13cj | 8:7889867308d3 | 650 | d = 13; |
el13cj | 8:7889867308d3 | 651 | break; |
el13cj | 8:7889867308d3 | 652 | case 4: |
el13cj | 8:7889867308d3 | 653 | d = 12; |
el13cj | 8:7889867308d3 | 654 | break; |
el13cj | 8:7889867308d3 | 655 | case 5: |
el13cj | 8:7889867308d3 | 656 | d = 11; |
el13cj | 8:7889867308d3 | 657 | break; |
el13cj | 8:7889867308d3 | 658 | case 6: |
el13cj | 8:7889867308d3 | 659 | d = 10; |
el13cj | 8:7889867308d3 | 660 | break; |
el13cj | 8:7889867308d3 | 661 | case 7: |
el13cj | 8:7889867308d3 | 662 | d = 9; |
el13cj | 8:7889867308d3 | 663 | break; |
el13cj | 8:7889867308d3 | 664 | } |
el13cj | 8:7889867308d3 | 665 | |
el13cj | 8:7889867308d3 | 666 | } else if (surround[10] && surround[11]) { //LHS edge |
el13cj | 8:7889867308d3 | 667 | switch (d) { |
el13cj | 8:7889867308d3 | 668 | case 9: |
el13cj | 8:7889867308d3 | 669 | d = 7; |
el13cj | 8:7889867308d3 | 670 | break; |
el13cj | 8:7889867308d3 | 671 | case 10: |
el13cj | 8:7889867308d3 | 672 | d = 6; |
el13cj | 8:7889867308d3 | 673 | break; |
el13cj | 8:7889867308d3 | 674 | case 11: |
el13cj | 8:7889867308d3 | 675 | d = 5; |
el13cj | 8:7889867308d3 | 676 | break; |
el13cj | 8:7889867308d3 | 677 | case 12: |
el13cj | 8:7889867308d3 | 678 | d = 4; |
el13cj | 8:7889867308d3 | 679 | break; |
el13cj | 8:7889867308d3 | 680 | case 13: |
el13cj | 8:7889867308d3 | 681 | d = 3; |
el13cj | 8:7889867308d3 | 682 | break; |
el13cj | 8:7889867308d3 | 683 | case 14: |
el13cj | 8:7889867308d3 | 684 | d = 2; |
el13cj | 8:7889867308d3 | 685 | break; |
el13cj | 8:7889867308d3 | 686 | case 15: |
el13cj | 8:7889867308d3 | 687 | d = 1; |
el13cj | 8:7889867308d3 | 688 | break; |
el13cj | 8:7889867308d3 | 689 | } |
el13cj | 8:7889867308d3 | 690 | } else if (surround[7] && surround[8]) { //bottom edge |
el13cj | 8:7889867308d3 | 691 | switch (d) { |
el13cj | 8:7889867308d3 | 692 | case 5: |
el13cj | 8:7889867308d3 | 693 | d = 3; |
el13cj | 8:7889867308d3 | 694 | break; |
el13cj | 8:7889867308d3 | 695 | case 6: |
el13cj | 8:7889867308d3 | 696 | d = 2; |
el13cj | 8:7889867308d3 | 697 | break; |
el13cj | 8:7889867308d3 | 698 | case 7: |
el13cj | 8:7889867308d3 | 699 | d = 1; |
el13cj | 8:7889867308d3 | 700 | break; |
el13cj | 8:7889867308d3 | 701 | case 8: |
el13cj | 8:7889867308d3 | 702 | d = 0; |
el13cj | 8:7889867308d3 | 703 | break; |
el13cj | 8:7889867308d3 | 704 | case 9: |
el13cj | 8:7889867308d3 | 705 | d = 15; |
el13cj | 8:7889867308d3 | 706 | break; |
el13cj | 8:7889867308d3 | 707 | case 10: |
el13cj | 8:7889867308d3 | 708 | d = 14; |
el13cj | 8:7889867308d3 | 709 | break; |
el13cj | 8:7889867308d3 | 710 | case 11: |
el13cj | 8:7889867308d3 | 711 | d = 13; |
el13cj | 8:7889867308d3 | 712 | break; |
el13cj | 8:7889867308d3 | 713 | } |
el13cj | 8:7889867308d3 | 714 | } |
el13cj | 8:7889867308d3 | 715 | |
el13cj | 8:7889867308d3 | 716 | //FOR 2 TOUCHING OFFCENTRE EDGE SQUARES |
el13cj | 8:7889867308d3 | 717 | else if (surround[0] && surround[1]) { //top edge left |
el13cj | 8:7889867308d3 | 718 | d = 9; |
el13cj | 8:7889867308d3 | 719 | } else if (surround[2] && surround[3]) { //top edge right |
el13cj | 8:7889867308d3 | 720 | d = 7; |
el13cj | 8:7889867308d3 | 721 | } else if (surround[9] && surround[8]) { // low edge right |
el13cj | 8:7889867308d3 | 722 | d = 1; |
el13cj | 8:7889867308d3 | 723 | } else if (surround[7] && surround[6]) { //low edge left |
el13cj | 8:7889867308d3 | 724 | d = 15; |
el13cj | 8:7889867308d3 | 725 | } else if (surround[0] && surround[11]) { //left edge top |
el13cj | 8:7889867308d3 | 726 | d =7; |
el13cj | 8:7889867308d3 | 727 | } else if (surround[3] && surround[4]) { //right edge top |
el13cj | 8:7889867308d3 | 728 | d = 9; |
el13cj | 8:7889867308d3 | 729 | } else if (surround[10] && surround[9]) { //left edge low |
el13cj | 8:7889867308d3 | 730 | d = 1; |
el13cj | 8:7889867308d3 | 731 | } else if (surround[5] && surround[6]) { //right edge low |
el13cj | 8:7889867308d3 | 732 | d = 15; |
el13cj | 8:7889867308d3 | 733 | } |
el13cj | 8:7889867308d3 | 734 | // FOR 1 TOUCHING CORNER SQUARE |
el13cj | 8:7889867308d3 | 735 | else if (surround[3]) { //top right |
el13cj | 8:7889867308d3 | 736 | d = 10; |
el13cj | 8:7889867308d3 | 737 | } else if (surround[6]) { // bottom right |
el13cj | 8:7889867308d3 | 738 | d = 14; |
el13cj | 8:7889867308d3 | 739 | } else if (surround[9]) { //bottom left |
el13cj | 8:7889867308d3 | 740 | d = 2; |
el13cj | 8:7889867308d3 | 741 | } else if (surround[0]) { //top left |
el13cj | 8:7889867308d3 | 742 | d = 6; |
el13cj | 8:7889867308d3 | 743 | } |
el13cj | 8:7889867308d3 | 744 | |
el13cj | 8:7889867308d3 | 745 | |
el13cj | 8:7889867308d3 | 746 | |
el13cj | 8:7889867308d3 | 747 | touchFlag = 0; //clear the touch flag |
el13cj | 8:7889867308d3 | 748 | |
el13cj | 8:7889867308d3 | 749 | for (int i = 0; i<12; i++) { //clear the set points |
el13cj | 8:7889867308d3 | 750 | surround[i] = 0; |
el13cj | 8:7889867308d3 | 751 | } |
el13cj | 8:7889867308d3 | 752 | } |
el13cj | 8:7889867308d3 | 753 | |
el13cj | 8:7889867308d3 | 754 | return d; |
el13cj | 8:7889867308d3 | 755 | |
el13cj | 8:7889867308d3 | 756 | } |
el13cj | 8:7889867308d3 | 757 | |
el13cj | 8:7889867308d3 | 758 | void ball() |
el13cj | 8:7889867308d3 | 759 | |
el13cj | 8:7889867308d3 | 760 | { |
el13cj | 9:8447ce6f51ae | 761 | int e = d; |
el13cj | 9:8447ce6f51ae | 762 | |
el13cj | 9:8447ce6f51ae | 763 | setAngle(); //check what ball is touching get new angle |
el13cj | 9:8447ce6f51ae | 764 | doBricks(); //check what bricks are touching clear all necessary |
el13cj | 9:8447ce6f51ae | 765 | moveBall1(d); //move ball with new angle |
el13cj | 9:8447ce6f51ae | 766 | wait_ms(25); //wait |
el13cj | 8:7889867308d3 | 767 | |
el13cj | 9:8447ce6f51ae | 768 | if (d == e) { |
el13cj | 8:7889867308d3 | 769 | |
el13cj | 9:8447ce6f51ae | 770 | setAngle(); |
el13cj | 9:8447ce6f51ae | 771 | doBricks(); |
el13cj | 9:8447ce6f51ae | 772 | moveBall2(d); |
el13cj | 9:8447ce6f51ae | 773 | wait_ms(25); |
el13cj | 9:8447ce6f51ae | 774 | } |
el13cj | 8:7889867308d3 | 775 | } |
el13cj | 8:7889867308d3 | 776 | |
el13cj | 8:7889867308d3 | 777 | void moveBall1(int d) //move the ball, quantised into 16 directions of motion |
el13cj | 8:7889867308d3 | 778 | { |
el13cj | 8:7889867308d3 | 779 | lcd.clearRect(bx,by,bw+1,bh+1); |
el13cj | 8:7889867308d3 | 780 | |
el13cj | 8:7889867308d3 | 781 | if (d == 0) { //0deg (vertical up) |
el13cj | 8:7889867308d3 | 782 | by-=1; |
el13cj | 8:7889867308d3 | 783 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 784 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 785 | } else if (d == 1) { //22.5deg |
el13cj | 8:7889867308d3 | 786 | by-=1; |
el13cj | 8:7889867308d3 | 787 | bx+=1; |
el13cj | 8:7889867308d3 | 788 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 789 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 790 | } else if (d == 2) { //45deg |
el13cj | 8:7889867308d3 | 791 | by-=1; |
el13cj | 8:7889867308d3 | 792 | bx+=1; |
el13cj | 8:7889867308d3 | 793 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 794 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 795 | } else if (d == 3) { //62.5deg |
el13cj | 8:7889867308d3 | 796 | by-=1; |
el13cj | 8:7889867308d3 | 797 | bx+=1; |
el13cj | 8:7889867308d3 | 798 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 799 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 800 | } else if (d == 4) { //90deg (right) |
el13cj | 8:7889867308d3 | 801 | bx+=1; |
el13cj | 8:7889867308d3 | 802 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 803 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 804 | } else if (d == 5) { //112.5deg |
el13cj | 8:7889867308d3 | 805 | bx+=1; |
el13cj | 8:7889867308d3 | 806 | by+=1; |
el13cj | 8:7889867308d3 | 807 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 808 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 809 | } else if (d == 6) { //135deg |
el13cj | 8:7889867308d3 | 810 | bx+=1; |
el13cj | 8:7889867308d3 | 811 | by+=1; |
el13cj | 8:7889867308d3 | 812 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 813 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 814 | } else if (d == 7) { //157.5deg |
el13cj | 8:7889867308d3 | 815 | bx+=1; |
el13cj | 8:7889867308d3 | 816 | by+=1; |
el13cj | 8:7889867308d3 | 817 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 818 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 819 | } else if (d == 8) { //180deg (vertical down) |
el13cj | 8:7889867308d3 | 820 | by++; |
el13cj | 8:7889867308d3 | 821 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 822 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 823 | } else if (d == 9) { //202.5deg |
el13cj | 8:7889867308d3 | 824 | by++; |
el13cj | 8:7889867308d3 | 825 | bx-=1; |
el13cj | 8:7889867308d3 | 826 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 827 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 828 | } else if (d == 10) { //225deg |
el13cj | 8:7889867308d3 | 829 | by+=1; |
el13cj | 8:7889867308d3 | 830 | bx-=1; |
el13cj | 8:7889867308d3 | 831 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 832 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 833 | } else if (d == 11) { //247.5deg |
el13cj | 8:7889867308d3 | 834 | by+=1; |
el13cj | 8:7889867308d3 | 835 | bx-=1; |
el13cj | 8:7889867308d3 | 836 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 837 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 838 | } else if (d == 12) { //270deg (left) |
el13cj | 8:7889867308d3 | 839 | bx-=1; |
el13cj | 8:7889867308d3 | 840 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 841 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 842 | } else if (d == 13) { //292.5deg |
el13cj | 8:7889867308d3 | 843 | bx-=1; |
el13cj | 8:7889867308d3 | 844 | by-=1; |
el13cj | 8:7889867308d3 | 845 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 846 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 847 | } else if (d == 14) { //315deg |
el13cj | 8:7889867308d3 | 848 | bx-=1; |
el13cj | 8:7889867308d3 | 849 | by-=1; |
el13cj | 8:7889867308d3 | 850 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 851 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 852 | } else if (d == 15) { //337.5deg |
el13cj | 8:7889867308d3 | 853 | bx-=1; |
el13cj | 8:7889867308d3 | 854 | by-=1; |
el13cj | 8:7889867308d3 | 855 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 856 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 857 | } |
el13cj | 8:7889867308d3 | 858 | } |
el13cj | 8:7889867308d3 | 859 | |
el13cj | 8:7889867308d3 | 860 | |
el13cj | 8:7889867308d3 | 861 | void moveBall2(int d) //move the ball, quantised into 16 directions of motion |
el13cj | 8:7889867308d3 | 862 | { |
el13cj | 8:7889867308d3 | 863 | lcd.clearRect(bx,by,bw+1,bh+1); |
el13cj | 8:7889867308d3 | 864 | |
el13cj | 8:7889867308d3 | 865 | if (d == 0) { //0deg (vertical up) |
el13cj | 8:7889867308d3 | 866 | by-=1; |
el13cj | 8:7889867308d3 | 867 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 868 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 869 | } else if (d == 1) { //22.5deg |
el13cj | 8:7889867308d3 | 870 | by-=1; |
el13cj | 8:7889867308d3 | 871 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 872 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 873 | } else if (d == 2) { //45deg |
el13cj | 8:7889867308d3 | 874 | by-=1; |
el13cj | 8:7889867308d3 | 875 | bx+=1; |
el13cj | 8:7889867308d3 | 876 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 877 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 878 | } else if (d == 3) { //62.5deg |
el13cj | 8:7889867308d3 | 879 | bx+=1; |
el13cj | 8:7889867308d3 | 880 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 881 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 882 | } else if (d == 4) { //90deg (right) |
el13cj | 8:7889867308d3 | 883 | bx+=1; |
el13cj | 8:7889867308d3 | 884 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 885 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 886 | } else if (d == 5) { //112.5deg |
el13cj | 8:7889867308d3 | 887 | bx+=1; |
el13cj | 8:7889867308d3 | 888 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 889 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 890 | } else if (d == 6) { //135deg |
el13cj | 8:7889867308d3 | 891 | bx+=1; |
el13cj | 8:7889867308d3 | 892 | by+=1; |
el13cj | 8:7889867308d3 | 893 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 894 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 895 | } else if (d == 7) { //157.5deg |
el13cj | 8:7889867308d3 | 896 | by+=1; |
el13cj | 8:7889867308d3 | 897 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 898 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 899 | } else if (d == 8) { //180deg (vertical down) |
el13cj | 8:7889867308d3 | 900 | by++; |
el13cj | 8:7889867308d3 | 901 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 902 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 903 | } else if (d == 9) { //202.5deg |
el13cj | 8:7889867308d3 | 904 | by++; |
el13cj | 8:7889867308d3 | 905 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 906 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 907 | } else if (d == 10) { //225deg |
el13cj | 8:7889867308d3 | 908 | by+=1; |
el13cj | 8:7889867308d3 | 909 | bx-=1; |
el13cj | 8:7889867308d3 | 910 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 911 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 912 | } else if (d == 11) { //247.5deg |
el13cj | 8:7889867308d3 | 913 | bx-=1; |
el13cj | 8:7889867308d3 | 914 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 915 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 916 | } else if (d == 12) { //270deg (left) |
el13cj | 8:7889867308d3 | 917 | bx-=1; |
el13cj | 8:7889867308d3 | 918 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 919 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 920 | } else if (d == 13) { //292.5deg |
el13cj | 8:7889867308d3 | 921 | bx-=1; |
el13cj | 8:7889867308d3 | 922 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 923 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 924 | } else if (d == 14) { //315deg |
el13cj | 8:7889867308d3 | 925 | bx-=1; |
el13cj | 8:7889867308d3 | 926 | by-=1; |
el13cj | 8:7889867308d3 | 927 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 928 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 929 | } else if (d == 15) { //337.5deg |
el13cj | 8:7889867308d3 | 930 | by-=1; |
el13cj | 8:7889867308d3 | 931 | lcd.drawRect(bx,by,bw,bh,bf); |
el13cj | 8:7889867308d3 | 932 | lcd.refresh(); |
el13cj | 8:7889867308d3 | 933 | } |
el13cj | 8:7889867308d3 | 934 | } |
el13cj | 8:7889867308d3 | 935 | |
el13cj | 8:7889867308d3 | 936 | |
el13cj | 8:7889867308d3 | 937 | //set pixels around the edge of the board |
el13cj | 8:7889867308d3 | 938 | void borderInit() |
el13cj | 8:7889867308d3 | 939 | { |
el13cj | 8:7889867308d3 | 940 | |
el13cj | 8:7889867308d3 | 941 | if (!displayGame) return; |
el13cj | 8:7889867308d3 | 942 | |
el13cj | 8:7889867308d3 | 943 | |
el13cj | 8:7889867308d3 | 944 | for (int i =0; i<=75; i++) { |
el13cj | 8:7889867308d3 | 945 | lcd.setPixel(i,0); |
el13cj | 8:7889867308d3 | 946 | } |
el13cj | 8:7889867308d3 | 947 | for (int i = 0; i<=48; i++) { |
el13cj | 8:7889867308d3 | 948 | lcd.setPixel(0,i); |
el13cj | 8:7889867308d3 | 949 | lcd.setPixel(75,i); |
el13cj | 8:7889867308d3 | 950 | lcd.setPixel(83,i); |
el13cj | 8:7889867308d3 | 951 | } |
el13cj | 8:7889867308d3 | 952 | for (int i =75; i<84; i++) { |
el13cj | 8:7889867308d3 | 953 | lcd.setPixel(i,31); |
el13cj | 8:7889867308d3 | 954 | } |
el13cj | 8:7889867308d3 | 955 | borderFlag = 1; |
el13cj | 8:7889867308d3 | 956 | |
el13cj | 8:7889867308d3 | 957 | } |