ELEC2645 Joseph Allison 200860349
Dependencies: N5110 Tetris mbed
main.cpp@7:dfb290c7958c, 2016-05-05 (annotated)
- Committer:
- el14ja
- Date:
- Thu May 05 10:41:45 2016 +0000
- Revision:
- 7:dfb290c7958c
- Parent:
- 6:9cb8171ca6df
submitted 1.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el14ja | 0:ce5c75c04dc2 | 1 | /* ELEC2645 Project |
el14ja | 0:ce5c75c04dc2 | 2 | week 19 - Created functions to work set pixels of the game area |
el14ja | 0:ce5c75c04dc2 | 3 | - Added the shapes and all their orientations |
el14ja | 0:ce5c75c04dc2 | 4 | - Created functions to place and remove shapes |
el14ja | 0:ce5c75c04dc2 | 5 | -added interupts and tickers for block moved and rotaion |
el14ja | 0:ce5c75c04dc2 | 6 | |
el14ja | 0:ce5c75c04dc2 | 7 | week 20 - 7system function |
el14ja | 0:ce5c75c04dc2 | 8 | |
el14ja | 4:7160c74529f7 | 9 | week 21 - starting to implement collision detection |
el14ja | 0:ce5c75c04dc2 | 10 | |
el14ja | 0:ce5c75c04dc2 | 11 | easter - colliosn detection |
el14ja | 0:ce5c75c04dc2 | 12 | - piece placement |
el14ja | 1:2b9ba34131ca | 13 | - scoring |
el14ja | 1:2b9ba34131ca | 14 | - levels and game speed changing |
el14ja | 3:8c5362716f4c | 15 | - Game Complete |
el14ja | 4:7160c74529f7 | 16 | |
el14ja | 4:7160c74529f7 | 17 | week 24 - All bugs removed |
el14ja | 0:ce5c75c04dc2 | 18 | */ |
el14ja | 0:ce5c75c04dc2 | 19 | |
el14ja | 0:ce5c75c04dc2 | 20 | |
el14ja | 1:2b9ba34131ca | 21 | #include "main.h" |
el14ja | 0:ce5c75c04dc2 | 22 | |
el14ja | 0:ce5c75c04dc2 | 23 | |
el14ja | 0:ce5c75c04dc2 | 24 | int main() |
el14ja | 0:ce5c75c04dc2 | 25 | { |
el14ja | 0:ce5c75c04dc2 | 26 | |
el14ja | 4:7160c74529f7 | 27 | srand(floor(ain.read()*10000)); //Gets a random seed so the the random number generator creates a different set of random numbers each time the game is played |
el14ja | 4:7160c74529f7 | 28 | |
el14ja | 4:7160c74529f7 | 29 | lcd.init(); //initilises the screen |
el14ja | 1:2b9ba34131ca | 30 | |
el14ja | 7:dfb290c7958c | 31 | startScreen(); |
el14ja | 7:dfb290c7958c | 32 | wait(2); |
el14ja | 7:dfb290c7958c | 33 | |
el14ja | 4:7160c74529f7 | 34 | setupInterruptInputs(); |
el14ja | 3:8c5362716f4c | 35 | |
el14ja | 0:ce5c75c04dc2 | 36 | newShapeBag();//create bag of shapes to use |
el14ja | 4:7160c74529f7 | 37 | nextpiece = nextshape[1]; //initially sets the next piece |
el14ja | 3:8c5362716f4c | 38 | tetris.gameInfo(0,1,nextpiece); |
el14ja | 4:7160c74529f7 | 39 | |
el14ja | 4:7160c74529f7 | 40 | musicticker.attach(&music_isr,0.125); //sets the speed that the music plays |
el14ja | 0:ce5c75c04dc2 | 41 | |
el14ja | 4:7160c74529f7 | 42 | setTicker(gamespeed); //sets the speed that the block falls |
el14ja | 3:8c5362716f4c | 43 | |
el14ja | 0:ce5c75c04dc2 | 44 | while (true) { |
el14ja | 0:ce5c75c04dc2 | 45 | |
el14ja | 3:8c5362716f4c | 46 | gameMenu(); |
el14ja | 3:8c5362716f4c | 47 | |
el14ja | 4:7160c74529f7 | 48 | if(musicplaying) { //checks to see if the music is playing |
el14ja | 3:8c5362716f4c | 49 | buzzer.write(0.5); |
el14ja | 3:8c5362716f4c | 50 | } else { |
el14ja | 3:8c5362716f4c | 51 | buzzer.write(0); |
el14ja | 0:ce5c75c04dc2 | 52 | } |
el14ja | 0:ce5c75c04dc2 | 53 | |
el14ja | 4:7160c74529f7 | 54 | score = 0; //sets all game varibles to default values |
el14ja | 4:7160c74529f7 | 55 | level = 1; |
el14ja | 4:7160c74529f7 | 56 | completedlines = 0; |
el14ja | 7:dfb290c7958c | 57 | gamespeed = 0.8; |
el14ja | 7:dfb290c7958c | 58 | |
el14ja | 7:dfb290c7958c | 59 | setTicker(gamespeed); |
el14ja | 7:dfb290c7958c | 60 | |
el14ja | 4:7160c74529f7 | 61 | //resests the game screen |
el14ja | 3:8c5362716f4c | 62 | setGameAreaArray(); |
el14ja | 3:8c5362716f4c | 63 | tetris.clearGame(); |
el14ja | 3:8c5362716f4c | 64 | tetris.gameSetup(); |
el14ja | 4:7160c74529f7 | 65 | tetris.gameInfo(score,level,nextpiece); |
el14ja | 3:8c5362716f4c | 66 | |
el14ja | 4:7160c74529f7 | 67 | |
el14ja | 4:7160c74529f7 | 68 | while(gameplaying) { //loops throught the gane function whilst the game is running |
el14ja | 3:8c5362716f4c | 69 | checkGameFlags(); |
el14ja | 3:8c5362716f4c | 70 | sleep(); |
el14ja | 0:ce5c75c04dc2 | 71 | } |
el14ja | 0:ce5c75c04dc2 | 72 | |
el14ja | 4:7160c74529f7 | 73 | gameOverScreen(); //when the game is over the game over screen is played |
el14ja | 4:7160c74529f7 | 74 | |
el14ja | 4:7160c74529f7 | 75 | //sets the game and game menu to run the next time ot come to them |
el14ja | 3:8c5362716f4c | 76 | gameplaying = true; |
el14ja | 3:8c5362716f4c | 77 | gamemenu = true; |
el14ja | 3:8c5362716f4c | 78 | } |
el14ja | 3:8c5362716f4c | 79 | } |
el14ja | 0:ce5c75c04dc2 | 80 | |
el14ja | 3:8c5362716f4c | 81 | void gameMenu() |
el14ja | 3:8c5362716f4c | 82 | { |
el14ja | 3:8c5362716f4c | 83 | |
el14ja | 4:7160c74529f7 | 84 | int pointer = 0; //sets the pointer to its inital place |
el14ja | 3:8c5362716f4c | 85 | |
el14ja | 3:8c5362716f4c | 86 | lcd.clear(); |
el14ja | 3:8c5362716f4c | 87 | lcd.refresh(); |
el14ja | 0:ce5c75c04dc2 | 88 | |
el14ja | 3:8c5362716f4c | 89 | while(gamemenu) { |
el14ja | 0:ce5c75c04dc2 | 90 | |
el14ja | 4:7160c74529f7 | 91 | //sets the menu text |
el14ja | 3:8c5362716f4c | 92 | lcd.printString("PLAY",15,1); |
el14ja | 3:8c5362716f4c | 93 | lcd.printString("MUSIC",15,3); |
el14ja | 3:8c5362716f4c | 94 | lcd.printString("HELP",15,5); |
el14ja | 0:ce5c75c04dc2 | 95 | |
el14ja | 4:7160c74529f7 | 96 | //checks the state fo the music to set the on/off label |
el14ja | 4:7160c74529f7 | 97 | if(musicplaying) { |
el14ja | 3:8c5362716f4c | 98 | lcd.printString(" ",50,3); |
el14ja | 3:8c5362716f4c | 99 | lcd.printString("ON",50,3); |
el14ja | 3:8c5362716f4c | 100 | } else { |
el14ja | 3:8c5362716f4c | 101 | lcd.printString("OFF",50,3); |
el14ja | 0:ce5c75c04dc2 | 102 | } |
el14ja | 0:ce5c75c04dc2 | 103 | |
el14ja | 3:8c5362716f4c | 104 | lcd.refresh(); |
el14ja | 4:7160c74529f7 | 105 | |
el14ja | 4:7160c74529f7 | 106 | //checks the down button to move the pointer down |
el14ja | 3:8c5362716f4c | 107 | if(g_down) { |
el14ja | 3:8c5362716f4c | 108 | g_left = 0; |
el14ja | 3:8c5362716f4c | 109 | g_right = 0; |
el14ja | 0:ce5c75c04dc2 | 110 | g_spin = 0; |
el14ja | 0:ce5c75c04dc2 | 111 | g_down = 0; |
el14ja | 4:7160c74529f7 | 112 | if(pointer < 2) { //the max pos is 2 so the point doesnt move any lower |
el14ja | 3:8c5362716f4c | 113 | pointer++; |
el14ja | 0:ce5c75c04dc2 | 114 | } |
el14ja | 1:2b9ba34131ca | 115 | |
el14ja | 1:2b9ba34131ca | 116 | } |
el14ja | 1:2b9ba34131ca | 117 | |
el14ja | 4:7160c74529f7 | 118 | //checks the spin button to move the pointer up |
el14ja | 3:8c5362716f4c | 119 | if(g_spin) { |
el14ja | 3:8c5362716f4c | 120 | g_left = 0; |
el14ja | 3:8c5362716f4c | 121 | g_right = 0; |
el14ja | 3:8c5362716f4c | 122 | g_spin = 0; |
el14ja | 3:8c5362716f4c | 123 | g_down = 0; |
el14ja | 4:7160c74529f7 | 124 | if(pointer >0) { //0 is the lower pointer pos |
el14ja | 3:8c5362716f4c | 125 | pointer--; |
el14ja | 3:8c5362716f4c | 126 | } |
el14ja | 3:8c5362716f4c | 127 | } |
el14ja | 3:8c5362716f4c | 128 | |
el14ja | 4:7160c74529f7 | 129 | //selects option 0 if the pointer is there and left is pressed |
el14ja | 4:7160c74529f7 | 130 | //selects main menu |
el14ja | 3:8c5362716f4c | 131 | if(g_left && pointer == 0) { |
el14ja | 3:8c5362716f4c | 132 | g_left = 0; |
el14ja | 3:8c5362716f4c | 133 | g_right = 0; |
el14ja | 3:8c5362716f4c | 134 | g_spin = 0; |
el14ja | 3:8c5362716f4c | 135 | g_down = 0; |
el14ja | 3:8c5362716f4c | 136 | gamemenu = false; |
el14ja | 3:8c5362716f4c | 137 | } |
el14ja | 3:8c5362716f4c | 138 | |
el14ja | 4:7160c74529f7 | 139 | //selects option 1 if the pointer is there and left is pressed |
el14ja | 4:7160c74529f7 | 140 | //turns music on |
el14ja | 3:8c5362716f4c | 141 | if(g_left && pointer == 1) { |
el14ja | 3:8c5362716f4c | 142 | g_left = 0; |
el14ja | 3:8c5362716f4c | 143 | g_right = 0; |
el14ja | 3:8c5362716f4c | 144 | g_spin = 0; |
el14ja | 3:8c5362716f4c | 145 | g_down = 0; |
el14ja | 3:8c5362716f4c | 146 | if(!musicplaying) { |
el14ja | 3:8c5362716f4c | 147 | musicplaying = true; |
el14ja | 3:8c5362716f4c | 148 | } else { |
el14ja | 3:8c5362716f4c | 149 | musicplaying = false; |
el14ja | 3:8c5362716f4c | 150 | } |
el14ja | 3:8c5362716f4c | 151 | lcd.refresh(); |
el14ja | 4:7160c74529f7 | 152 | |
el14ja | 3:8c5362716f4c | 153 | } |
el14ja | 0:ce5c75c04dc2 | 154 | |
el14ja | 4:7160c74529f7 | 155 | //selects option 2 if the pointer is there and left is pressed |
el14ja | 4:7160c74529f7 | 156 | //selects the help screen |
el14ja | 3:8c5362716f4c | 157 | if(g_left && pointer == 2) { |
el14ja | 3:8c5362716f4c | 158 | g_left = 0; |
el14ja | 3:8c5362716f4c | 159 | g_right = 0; |
el14ja | 3:8c5362716f4c | 160 | g_spin = 0; |
el14ja | 3:8c5362716f4c | 161 | g_down = 0; |
el14ja | 3:8c5362716f4c | 162 | helpScreen(); |
el14ja | 3:8c5362716f4c | 163 | } |
el14ja | 3:8c5362716f4c | 164 | |
el14ja | 3:8c5362716f4c | 165 | for(int i = 0; i<4; i++) { //clear the points before the new one is places |
el14ja | 3:8c5362716f4c | 166 | for(int j = 10; j < 50; j++) { |
el14ja | 4:7160c74529f7 | 167 | lcd.clearPixel(70+i,j); |
el14ja | 3:8c5362716f4c | 168 | } |
el14ja | 3:8c5362716f4c | 169 | } |
el14ja | 0:ce5c75c04dc2 | 170 | |
el14ja | 4:7160c74529f7 | 171 | lcd.drawRect(70,10+ (pointer *16),3,3,0); //draws pointer on the screen |
el14ja | 3:8c5362716f4c | 172 | lcd.refresh(); |
el14ja | 3:8c5362716f4c | 173 | |
el14ja | 3:8c5362716f4c | 174 | sleep(); |
el14ja | 3:8c5362716f4c | 175 | } |
el14ja | 3:8c5362716f4c | 176 | //set all the flags to zero before the game starts |
el14ja | 3:8c5362716f4c | 177 | } |
el14ja | 3:8c5362716f4c | 178 | |
el14ja | 7:dfb290c7958c | 179 | void startScreen(){ |
el14ja | 7:dfb290c7958c | 180 | |
el14ja | 7:dfb290c7958c | 181 | lcd.printString("TETRIS",27,1); |
el14ja | 7:dfb290c7958c | 182 | lcd.printString("Created by",12,2); |
el14ja | 7:dfb290c7958c | 183 | lcd.printString("Joe Allison",9,3); |
el14ja | 7:dfb290c7958c | 184 | lcd.refresh(); |
el14ja | 7:dfb290c7958c | 185 | } |
el14ja | 7:dfb290c7958c | 186 | |
el14ja | 4:7160c74529f7 | 187 | |
el14ja | 4:7160c74529f7 | 188 | void gameOverScreen() |
el14ja | 4:7160c74529f7 | 189 | { |
el14ja | 7:dfb290c7958c | 190 | buzzer.write(0); |
el14ja | 4:7160c74529f7 | 191 | bool gameoverscreen = true; |
el14ja | 4:7160c74529f7 | 192 | lcd.clear(); |
el14ja | 4:7160c74529f7 | 193 | |
el14ja | 4:7160c74529f7 | 194 | lcd.printString("GAME OVER",15,1); |
el14ja | 4:7160c74529f7 | 195 | |
el14ja | 4:7160c74529f7 | 196 | //prints the score on the screen |
el14ja | 4:7160c74529f7 | 197 | char buffer[14]; |
el14ja | 4:7160c74529f7 | 198 | int length = sprintf(buffer,"Score = %d",score); |
el14ja | 4:7160c74529f7 | 199 | if(length <=14) { |
el14ja | 4:7160c74529f7 | 200 | lcd.printString(buffer,0,3); |
el14ja | 4:7160c74529f7 | 201 | lcd.refresh(); |
el14ja | 4:7160c74529f7 | 202 | } |
el14ja | 4:7160c74529f7 | 203 | |
el14ja | 4:7160c74529f7 | 204 | while (gameoverscreen) { |
el14ja | 4:7160c74529f7 | 205 | //waits for any button to be pressed to leave the game over screen |
el14ja | 4:7160c74529f7 | 206 | if(g_left || g_right || g_spin || g_down) { |
el14ja | 4:7160c74529f7 | 207 | g_left = 0; |
el14ja | 4:7160c74529f7 | 208 | g_right = 0; |
el14ja | 4:7160c74529f7 | 209 | g_spin = 0; |
el14ja | 4:7160c74529f7 | 210 | g_down = 0; |
el14ja | 4:7160c74529f7 | 211 | gameoverscreen = false; |
el14ja | 4:7160c74529f7 | 212 | lcd.clear(); |
el14ja | 4:7160c74529f7 | 213 | |
el14ja | 4:7160c74529f7 | 214 | } |
el14ja | 4:7160c74529f7 | 215 | sleep(); |
el14ja | 4:7160c74529f7 | 216 | } |
el14ja | 4:7160c74529f7 | 217 | } |
el14ja | 4:7160c74529f7 | 218 | |
el14ja | 3:8c5362716f4c | 219 | void helpScreen() |
el14ja | 3:8c5362716f4c | 220 | { |
el14ja | 4:7160c74529f7 | 221 | |
el14ja | 4:7160c74529f7 | 222 | // help screen 1 |
el14ja | 3:8c5362716f4c | 223 | bool helpScreen = true; |
el14ja | 3:8c5362716f4c | 224 | lcd.clear(); |
el14ja | 4:7160c74529f7 | 225 | //prints screen text |
el14ja | 3:8c5362716f4c | 226 | lcd.printString("Move pieces",0,0); |
el14ja | 3:8c5362716f4c | 227 | lcd.printString("with the D-pad",0,1); |
el14ja | 3:8c5362716f4c | 228 | lcd.printString("to create rows",0,2); |
el14ja | 3:8c5362716f4c | 229 | lcd.printString("Press any",0,4); |
el14ja | 3:8c5362716f4c | 230 | lcd.printString("button",0,5); |
el14ja | 3:8c5362716f4c | 231 | lcd.refresh(); |
el14ja | 4:7160c74529f7 | 232 | //waits for any button to be pressed |
el14ja | 3:8c5362716f4c | 233 | while (helpScreen) { |
el14ja | 3:8c5362716f4c | 234 | |
el14ja | 3:8c5362716f4c | 235 | if(g_left || g_right || g_spin || g_down) { |
el14ja | 3:8c5362716f4c | 236 | g_left = 0; |
el14ja | 3:8c5362716f4c | 237 | g_right = 0; |
el14ja | 3:8c5362716f4c | 238 | g_spin = 0; |
el14ja | 3:8c5362716f4c | 239 | g_down = 0; |
el14ja | 3:8c5362716f4c | 240 | helpScreen = false; |
el14ja | 3:8c5362716f4c | 241 | lcd.clear(); |
el14ja | 3:8c5362716f4c | 242 | |
el14ja | 3:8c5362716f4c | 243 | } |
el14ja | 3:8c5362716f4c | 244 | sleep(); |
el14ja | 3:8c5362716f4c | 245 | } |
el14ja | 3:8c5362716f4c | 246 | |
el14ja | 4:7160c74529f7 | 247 | // help screen 2 |
el14ja | 3:8c5362716f4c | 248 | helpScreen = true; |
el14ja | 3:8c5362716f4c | 249 | lcd.clear(); |
el14ja | 4:7160c74529f7 | 250 | |
el14ja | 4:7160c74529f7 | 251 | //prints screen text and images |
el14ja | 3:8c5362716f4c | 252 | lcd.drawCircle(42,5,4,0); |
el14ja | 3:8c5362716f4c | 253 | lcd.drawCircle(31,16,4,0); |
el14ja | 3:8c5362716f4c | 254 | lcd.drawCircle(53,16,4,0); |
el14ja | 3:8c5362716f4c | 255 | lcd.drawCircle(42,26,4,0); |
el14ja | 3:8c5362716f4c | 256 | lcd.printString("Spin",5,0); |
el14ja | 3:8c5362716f4c | 257 | lcd.printString("Left",2,2); |
el14ja | 3:8c5362716f4c | 258 | lcd.printString("Right",55,0); |
el14ja | 3:8c5362716f4c | 259 | lcd.printString("Drop",60,3); |
el14ja | 3:8c5362716f4c | 260 | lcd.drawLine(29,4,42,5,1); //spin |
el14ja | 3:8c5362716f4c | 261 | lcd.drawLine(53,5,53,16,1); //right |
el14ja | 3:8c5362716f4c | 262 | lcd.drawLine(25,19,31,16,1); //left |
el14ja | 3:8c5362716f4c | 263 | lcd.drawLine(58,27,42,26,1); //drop |
el14ja | 3:8c5362716f4c | 264 | |
el14ja | 3:8c5362716f4c | 265 | lcd.printString("Press any",0,4); |
el14ja | 3:8c5362716f4c | 266 | lcd.printString("button",0,5); |
el14ja | 0:ce5c75c04dc2 | 267 | lcd.refresh(); |
el14ja | 0:ce5c75c04dc2 | 268 | |
el14ja | 4:7160c74529f7 | 269 | //waits for any button to be pressed |
el14ja | 3:8c5362716f4c | 270 | while (helpScreen) { |
el14ja | 3:8c5362716f4c | 271 | |
el14ja | 3:8c5362716f4c | 272 | if(g_left || g_right || g_spin || g_down) { |
el14ja | 3:8c5362716f4c | 273 | g_left = 0; |
el14ja | 3:8c5362716f4c | 274 | g_right = 0; |
el14ja | 3:8c5362716f4c | 275 | g_spin = 0; |
el14ja | 3:8c5362716f4c | 276 | g_down = 0; |
el14ja | 3:8c5362716f4c | 277 | helpScreen = false; |
el14ja | 3:8c5362716f4c | 278 | lcd.clear(); |
el14ja | 3:8c5362716f4c | 279 | } |
el14ja | 3:8c5362716f4c | 280 | sleep(); |
el14ja | 3:8c5362716f4c | 281 | } |
el14ja | 3:8c5362716f4c | 282 | |
el14ja | 4:7160c74529f7 | 283 | |
el14ja | 4:7160c74529f7 | 284 | // help screen 3 |
el14ja | 4:7160c74529f7 | 285 | |
el14ja | 4:7160c74529f7 | 286 | helpScreen = true; |
el14ja | 4:7160c74529f7 | 287 | lcd.clear(); |
el14ja | 4:7160c74529f7 | 288 | |
el14ja | 4:7160c74529f7 | 289 | //prints screen text |
el14ja | 4:7160c74529f7 | 290 | lcd.printString("Press left and",0,0); |
el14ja | 4:7160c74529f7 | 291 | lcd.printString("right together",0,1); |
el14ja | 6:9cb8171ca6df | 292 | lcd.printString("to pause the",0,2); |
el14ja | 4:7160c74529f7 | 293 | lcd.printString("game",0,3); |
el14ja | 4:7160c74529f7 | 294 | |
el14ja | 4:7160c74529f7 | 295 | |
el14ja | 4:7160c74529f7 | 296 | lcd.printString("Press any",0,4); |
el14ja | 4:7160c74529f7 | 297 | lcd.printString("button",0,5); |
el14ja | 4:7160c74529f7 | 298 | lcd.refresh(); |
el14ja | 4:7160c74529f7 | 299 | |
el14ja | 4:7160c74529f7 | 300 | //waits for any button to be pressed |
el14ja | 4:7160c74529f7 | 301 | while (helpScreen) { |
el14ja | 4:7160c74529f7 | 302 | |
el14ja | 4:7160c74529f7 | 303 | if(g_left || g_right || g_spin || g_down) { |
el14ja | 4:7160c74529f7 | 304 | g_left = 0; |
el14ja | 4:7160c74529f7 | 305 | g_right = 0; |
el14ja | 4:7160c74529f7 | 306 | g_spin = 0; |
el14ja | 4:7160c74529f7 | 307 | g_down = 0; |
el14ja | 4:7160c74529f7 | 308 | helpScreen = false; |
el14ja | 4:7160c74529f7 | 309 | lcd.clear(); |
el14ja | 4:7160c74529f7 | 310 | } |
el14ja | 4:7160c74529f7 | 311 | sleep(); |
el14ja | 4:7160c74529f7 | 312 | } |
el14ja | 3:8c5362716f4c | 313 | } |
el14ja | 3:8c5362716f4c | 314 | |
el14ja | 3:8c5362716f4c | 315 | void checkGameFlags() |
el14ja | 3:8c5362716f4c | 316 | { |
el14ja | 4:7160c74529f7 | 317 | if(g_pausemenu) { //if both left and right havent been pressed within 0.1s both flags are reset to 0 |
el14ja | 4:7160c74529f7 | 318 | g_pausemenu = 0; |
el14ja | 4:7160c74529f7 | 319 | pauseright = 0; |
el14ja | 4:7160c74529f7 | 320 | pauseleft = 0; |
el14ja | 4:7160c74529f7 | 321 | } |
el14ja | 4:7160c74529f7 | 322 | |
el14ja | 4:7160c74529f7 | 323 | //checks if both left and right have been pressed at the same time |
el14ja | 4:7160c74529f7 | 324 | if(pauseright && pauseleft) { |
el14ja | 4:7160c74529f7 | 325 | pauseright = 0; |
el14ja | 4:7160c74529f7 | 326 | pauseleft = 0; |
el14ja | 4:7160c74529f7 | 327 | lcd.clear(); |
el14ja | 4:7160c74529f7 | 328 | lcd.printString("PAUSE",27,2); |
el14ja | 4:7160c74529f7 | 329 | lcd.refresh(); |
el14ja | 4:7160c74529f7 | 330 | buzzer.write(0.0); //silences music while on the pause menu |
el14ja | 4:7160c74529f7 | 331 | while(1) { //waits in a loop til the both left and right are pressed |
el14ja | 4:7160c74529f7 | 332 | |
el14ja | 4:7160c74529f7 | 333 | if(pauseright && pauseleft) { |
el14ja | 4:7160c74529f7 | 334 | pauseright = 0; |
el14ja | 4:7160c74529f7 | 335 | pauseleft = 0; |
el14ja | 4:7160c74529f7 | 336 | |
el14ja | 4:7160c74529f7 | 337 | break; |
el14ja | 4:7160c74529f7 | 338 | } |
el14ja | 4:7160c74529f7 | 339 | sleep(); |
el14ja | 4:7160c74529f7 | 340 | } |
el14ja | 4:7160c74529f7 | 341 | |
el14ja | 4:7160c74529f7 | 342 | //sets all flags to 0 incase they are pressed during the pause screen |
el14ja | 4:7160c74529f7 | 343 | g_left = 0; |
el14ja | 4:7160c74529f7 | 344 | g_right = 0; |
el14ja | 4:7160c74529f7 | 345 | g_spin = 0; |
el14ja | 4:7160c74529f7 | 346 | g_down = 0; |
el14ja | 4:7160c74529f7 | 347 | |
el14ja | 4:7160c74529f7 | 348 | if(musicplaying) { //trubs music back on |
el14ja | 4:7160c74529f7 | 349 | buzzer.write(0.5); |
el14ja | 4:7160c74529f7 | 350 | } else { |
el14ja | 4:7160c74529f7 | 351 | buzzer.write(0); |
el14ja | 4:7160c74529f7 | 352 | } |
el14ja | 4:7160c74529f7 | 353 | |
el14ja | 4:7160c74529f7 | 354 | //resets the game area |
el14ja | 4:7160c74529f7 | 355 | setGameArea(); |
el14ja | 4:7160c74529f7 | 356 | tetris.piecePlace(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]); |
el14ja | 4:7160c74529f7 | 357 | } |
el14ja | 4:7160c74529f7 | 358 | |
el14ja | 4:7160c74529f7 | 359 | //checks if a new piece need placing in the game |
el14ja | 3:8c5362716f4c | 360 | if(g_newpiece) { |
el14ja | 3:8c5362716f4c | 361 | g_newpiece = 0; |
el14ja | 3:8c5362716f4c | 362 | newpiece(); |
el14ja | 3:8c5362716f4c | 363 | } |
el14ja | 3:8c5362716f4c | 364 | |
el14ja | 4:7160c74529f7 | 365 | //checks if the right button has been pressed |
el14ja | 3:8c5362716f4c | 366 | if(g_right) { |
el14ja | 3:8c5362716f4c | 367 | g_right = 0; |
el14ja | 3:8c5362716f4c | 368 | movePieceRight(); |
el14ja | 3:8c5362716f4c | 369 | } |
el14ja | 3:8c5362716f4c | 370 | |
el14ja | 4:7160c74529f7 | 371 | //checks if the left button has been pressed |
el14ja | 3:8c5362716f4c | 372 | if(g_left) { |
el14ja | 3:8c5362716f4c | 373 | g_left = 0; |
el14ja | 4:7160c74529f7 | 374 | movePieceLeft(); |
el14ja | 4:7160c74529f7 | 375 | } |
el14ja | 3:8c5362716f4c | 376 | |
el14ja | 4:7160c74529f7 | 377 | //checks the game time to see if the piece needs moving down |
el14ja | 3:8c5362716f4c | 378 | if (g_timer_flag) { |
el14ja | 3:8c5362716f4c | 379 | g_timer_flag = 0; |
el14ja | 3:8c5362716f4c | 380 | |
el14ja | 4:7160c74529f7 | 381 | //checks if there is a place for the piece to move down to, if not the piece is placed |
el14ja | 4:7160c74529f7 | 382 | if(movePossible(pieceposition[0],pieceposition[1]+1,orientation)) { //returns false if piece cant move down |
el14ja | 3:8c5362716f4c | 383 | |
el14ja | 3:8c5362716f4c | 384 | movePieceDown(); |
el14ja | 3:8c5362716f4c | 385 | |
el14ja | 3:8c5362716f4c | 386 | } else { //piece placed |
el14ja | 3:8c5362716f4c | 387 | |
el14ja | 4:7160c74529f7 | 388 | g_newpiece = 1; //a new piece is needed as the last one is placed |
el14ja | 4:7160c74529f7 | 389 | pieceToGameArray(); //adds the placed piece to the game area |
el14ja | 3:8c5362716f4c | 390 | |
el14ja | 4:7160c74529f7 | 391 | if(harddropping) {//if piece has been placed after a hard drop the game speed is set back to normal |
el14ja | 3:8c5362716f4c | 392 | harddropping = 0; |
el14ja | 3:8c5362716f4c | 393 | setTicker(gamespeed); |
el14ja | 3:8c5362716f4c | 394 | } |
el14ja | 4:7160c74529f7 | 395 | |
el14ja | 4:7160c74529f7 | 396 | //if a piece has been placed, it needs to be check to see if a full line is complete |
el14ja | 3:8c5362716f4c | 397 | checkCompleteLine(); |
el14ja | 3:8c5362716f4c | 398 | setGameArea(); |
el14ja | 3:8c5362716f4c | 399 | |
el14ja | 3:8c5362716f4c | 400 | } |
el14ja | 3:8c5362716f4c | 401 | } |
el14ja | 3:8c5362716f4c | 402 | |
el14ja | 3:8c5362716f4c | 403 | if(g_spin) { |
el14ja | 3:8c5362716f4c | 404 | g_spin = 0; |
el14ja | 3:8c5362716f4c | 405 | spinPiece(); |
el14ja | 3:8c5362716f4c | 406 | |
el14ja | 3:8c5362716f4c | 407 | } |
el14ja | 3:8c5362716f4c | 408 | |
el14ja | 3:8c5362716f4c | 409 | if(g_down) { |
el14ja | 3:8c5362716f4c | 410 | g_down = 0; |
el14ja | 4:7160c74529f7 | 411 | //when hard dropping the drop ticker is spead up |
el14ja | 3:8c5362716f4c | 412 | setTicker(0.001); |
el14ja | 3:8c5362716f4c | 413 | harddropping = 1; |
el14ja | 3:8c5362716f4c | 414 | } |
el14ja | 3:8c5362716f4c | 415 | |
el14ja | 4:7160c74529f7 | 416 | |
el14ja | 3:8c5362716f4c | 417 | if(g_music) { |
el14ja | 3:8c5362716f4c | 418 | g_music = 0; |
el14ja | 4:7160c74529f7 | 419 | buzzer.period_us(musisnotes[musiccounter]); //play the note in the musicnotes |
el14ja | 4:7160c74529f7 | 420 | musiccounter++; //incriments the music counter |
el14ja | 3:8c5362716f4c | 421 | |
el14ja | 3:8c5362716f4c | 422 | if(musiccounter > 63) { |
el14ja | 3:8c5362716f4c | 423 | musiccounter = 0; |
el14ja | 3:8c5362716f4c | 424 | } |
el14ja | 3:8c5362716f4c | 425 | |
el14ja | 3:8c5362716f4c | 426 | } |
el14ja | 3:8c5362716f4c | 427 | } |
el14ja | 3:8c5362716f4c | 428 | |
el14ja | 4:7160c74529f7 | 429 | void setupInterruptInputs(){ |
el14ja | 4:7160c74529f7 | 430 | //internal resistors set to pull down so that when the button is pressed the voltage is read as high |
el14ja | 4:7160c74529f7 | 431 | left.mode(PullDown); |
el14ja | 4:7160c74529f7 | 432 | right.mode(PullDown); |
el14ja | 4:7160c74529f7 | 433 | spin.mode(PullDown); |
el14ja | 4:7160c74529f7 | 434 | down.mode(PullDown); |
el14ja | 4:7160c74529f7 | 435 | |
el14ja | 4:7160c74529f7 | 436 | //sets the the interrupts to be called when the button is pressed down |
el14ja | 4:7160c74529f7 | 437 | left.rise(&left_isr); |
el14ja | 4:7160c74529f7 | 438 | right.rise(&right_isr); |
el14ja | 4:7160c74529f7 | 439 | spin.rise(&spin_isr); |
el14ja | 4:7160c74529f7 | 440 | down.rise(&down_isr); |
el14ja | 4:7160c74529f7 | 441 | } |
el14ja | 4:7160c74529f7 | 442 | |
el14ja | 3:8c5362716f4c | 443 | void setGameAreaArray() |
el14ja | 3:8c5362716f4c | 444 | { |
el14ja | 3:8c5362716f4c | 445 | //fill gamearea add the walls |
el14ja | 4:7160c74529f7 | 446 | //sets 1's around the bottoms and sides |
el14ja | 0:ce5c75c04dc2 | 447 | for(int i = 0; i<12; i++) { |
el14ja | 0:ce5c75c04dc2 | 448 | for(int j = 0; j<16; j++) { |
el14ja | 0:ce5c75c04dc2 | 449 | gamearea[i][j] = 1; |
el14ja | 0:ce5c75c04dc2 | 450 | } |
el14ja | 0:ce5c75c04dc2 | 451 | } |
el14ja | 4:7160c74529f7 | 452 | //fills the middle with 0's |
el14ja | 0:ce5c75c04dc2 | 453 | for(int i = 1; i<11; i++) { |
el14ja | 0:ce5c75c04dc2 | 454 | for(int j = 0; j<15; j++) { |
el14ja | 0:ce5c75c04dc2 | 455 | gamearea[i][j] = 0; |
el14ja | 0:ce5c75c04dc2 | 456 | } |
el14ja | 0:ce5c75c04dc2 | 457 | } |
el14ja | 0:ce5c75c04dc2 | 458 | } |
el14ja | 0:ce5c75c04dc2 | 459 | |
el14ja | 4:7160c74529f7 | 460 | bool movePossible(int xpos, int ypos, int orientation) |
el14ja | 0:ce5c75c04dc2 | 461 | { |
el14ja | 0:ce5c75c04dc2 | 462 | |
el14ja | 0:ce5c75c04dc2 | 463 | if (orientation > 3) { |
el14ja | 0:ce5c75c04dc2 | 464 | orientation = 0; |
el14ja | 0:ce5c75c04dc2 | 465 | } |
el14ja | 4:7160c74529f7 | 466 | int count = 0; //counter to count through all 16 bits |
el14ja | 0:ce5c75c04dc2 | 467 | |
el14ja | 4:7160c74529f7 | 468 | for(int y = 0; y < 4; y++) { |
el14ja | 0:ce5c75c04dc2 | 469 | for(int x = 0; x < 4; x++) { |
el14ja | 0:ce5c75c04dc2 | 470 | |
el14ja | 0:ce5c75c04dc2 | 471 | int bit = shapes[currentshape][orientation] & (1<<count); //find the bit of each pixel |
el14ja | 0:ce5c75c04dc2 | 472 | |
el14ja | 0:ce5c75c04dc2 | 473 | count ++; |
el14ja | 0:ce5c75c04dc2 | 474 | |
el14ja | 4:7160c74529f7 | 475 | if ((bit !=0) && (gamearea[xpos+x+1][ypos+y]!=0)) { //compares each pixel of the piece witht he game array to check for collisions |
el14ja | 0:ce5c75c04dc2 | 476 | return false; |
el14ja | 0:ce5c75c04dc2 | 477 | } |
el14ja | 0:ce5c75c04dc2 | 478 | } |
el14ja | 0:ce5c75c04dc2 | 479 | } |
el14ja | 0:ce5c75c04dc2 | 480 | |
el14ja | 0:ce5c75c04dc2 | 481 | return true; |
el14ja | 0:ce5c75c04dc2 | 482 | } |
el14ja | 0:ce5c75c04dc2 | 483 | |
el14ja | 4:7160c74529f7 | 484 | void newpiece() |
el14ja | 0:ce5c75c04dc2 | 485 | { |
el14ja | 3:8c5362716f4c | 486 | |
el14ja | 0:ce5c75c04dc2 | 487 | orientation = 0; //set the orientation back to zero |
el14ja | 0:ce5c75c04dc2 | 488 | pieceposition[0] = 4; //sets the position back to the top |
el14ja | 0:ce5c75c04dc2 | 489 | pieceposition[1] = 0; |
el14ja | 0:ce5c75c04dc2 | 490 | |
el14ja | 1:2b9ba34131ca | 491 | //clearGame(); |
el14ja | 1:2b9ba34131ca | 492 | //setGameArea(); |
el14ja | 1:2b9ba34131ca | 493 | |
el14ja | 4:7160c74529f7 | 494 | currentshape = nextshape[shapebagcounter]; //gets the next shape to place on the screen |
el14ja | 4:7160c74529f7 | 495 | tetris.piecePlace(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]);//places the new piece |
el14ja | 0:ce5c75c04dc2 | 496 | |
el14ja | 0:ce5c75c04dc2 | 497 | |
el14ja | 0:ce5c75c04dc2 | 498 | if(shapebagcounter > 5) { //when counter becomes 6 |
el14ja | 0:ce5c75c04dc2 | 499 | newShapeBag(); //a new bag is created |
el14ja | 0:ce5c75c04dc2 | 500 | shapebagcounter = 0; |
el14ja | 0:ce5c75c04dc2 | 501 | nextpiece = nextshape[0]; |
el14ja | 0:ce5c75c04dc2 | 502 | } else { |
el14ja | 0:ce5c75c04dc2 | 503 | nextpiece = nextshape[shapebagcounter+1]; |
el14ja | 0:ce5c75c04dc2 | 504 | shapebagcounter++; //incrimenst the counter |
el14ja | 0:ce5c75c04dc2 | 505 | } |
el14ja | 4:7160c74529f7 | 506 | |
el14ja | 3:8c5362716f4c | 507 | tetris.gameInfo(score,level,nextpiece); |
el14ja | 3:8c5362716f4c | 508 | |
el14ja | 4:7160c74529f7 | 509 | if(!movePossible(pieceposition[0],pieceposition[1]+1,orientation)) {//if a piece is placed and imidiatly cant move the game is over |
el14ja | 3:8c5362716f4c | 510 | gameplaying = false; |
el14ja | 3:8c5362716f4c | 511 | } |
el14ja | 3:8c5362716f4c | 512 | |
el14ja | 0:ce5c75c04dc2 | 513 | } |
el14ja | 0:ce5c75c04dc2 | 514 | |
el14ja | 4:7160c74529f7 | 515 | void newShapeBag() |
el14ja | 0:ce5c75c04dc2 | 516 | { |
el14ja | 0:ce5c75c04dc2 | 517 | |
el14ja | 0:ce5c75c04dc2 | 518 | bool match = false; |
el14ja | 0:ce5c75c04dc2 | 519 | int newrandomnumber; |
el14ja | 0:ce5c75c04dc2 | 520 | |
el14ja | 0:ce5c75c04dc2 | 521 | nextshape[0] = rand()%7; //creates first random number |
el14ja | 0:ce5c75c04dc2 | 522 | |
el14ja | 4:7160c74529f7 | 523 | for(int i = 1; i < 7; i++){ //loops to find all 7 numbers |
el14ja | 0:ce5c75c04dc2 | 524 | |
el14ja | 0:ce5c75c04dc2 | 525 | do { |
el14ja | 0:ce5c75c04dc2 | 526 | newrandomnumber = rand()%7; |
el14ja | 0:ce5c75c04dc2 | 527 | |
el14ja | 0:ce5c75c04dc2 | 528 | match = false; //set to false to end the do while if no match is found |
el14ja | 0:ce5c75c04dc2 | 529 | |
el14ja | 0:ce5c75c04dc2 | 530 | for(int j = 0; j < i; j++) { |
el14ja | 0:ce5c75c04dc2 | 531 | |
el14ja | 0:ce5c75c04dc2 | 532 | if (newrandomnumber == nextshape[j]) { //checks to see if number has already been chosen |
el14ja | 0:ce5c75c04dc2 | 533 | match = true;// continue do while if a match is found |
el14ja | 0:ce5c75c04dc2 | 534 | break; //end the for loop that is checking for a match |
el14ja | 0:ce5c75c04dc2 | 535 | } |
el14ja | 0:ce5c75c04dc2 | 536 | } |
el14ja | 0:ce5c75c04dc2 | 537 | } while(match); |
el14ja | 0:ce5c75c04dc2 | 538 | |
el14ja | 0:ce5c75c04dc2 | 539 | nextshape[i] = newrandomnumber; //set new found number into the newshape array |
el14ja | 0:ce5c75c04dc2 | 540 | } |
el14ja | 0:ce5c75c04dc2 | 541 | } |
el14ja | 0:ce5c75c04dc2 | 542 | |
el14ja | 4:7160c74529f7 | 543 | void checkCompleteLine() |
el14ja | 0:ce5c75c04dc2 | 544 | { |
el14ja | 0:ce5c75c04dc2 | 545 | |
el14ja | 0:ce5c75c04dc2 | 546 | int rowscomplete[4] = {0,0,0,0}; //max of 4 rows can be completed with a single block |
el14ja | 0:ce5c75c04dc2 | 547 | int rowscompletecounter = 0; //counter to place complete row in the array |
el14ja | 0:ce5c75c04dc2 | 548 | int sum = 0; |
el14ja | 0:ce5c75c04dc2 | 549 | bool rowtoremove = false; |
el14ja | 0:ce5c75c04dc2 | 550 | for(int y = 0; y <15; y++) { //searches through all the rows |
el14ja | 0:ce5c75c04dc2 | 551 | |
el14ja | 0:ce5c75c04dc2 | 552 | for(int x = 1; x<11; x++) { |
el14ja | 0:ce5c75c04dc2 | 553 | if(gamearea[x][y] == 1) { |
el14ja | 1:2b9ba34131ca | 554 | sum++; //counts how many filled cells |
el14ja | 0:ce5c75c04dc2 | 555 | } |
el14ja | 0:ce5c75c04dc2 | 556 | } |
el14ja | 1:2b9ba34131ca | 557 | if(sum == 10) { //if all 10 cells are full |
el14ja | 0:ce5c75c04dc2 | 558 | //pc.printf("%d complete \n",y); |
el14ja | 1:2b9ba34131ca | 559 | rowscomplete[rowscompletecounter] = y; //sets it as a completed row |
el14ja | 0:ce5c75c04dc2 | 560 | rowscompletecounter++; |
el14ja | 0:ce5c75c04dc2 | 561 | rowtoremove = true; |
el14ja | 0:ce5c75c04dc2 | 562 | } |
el14ja | 0:ce5c75c04dc2 | 563 | sum = 0; |
el14ja | 0:ce5c75c04dc2 | 564 | } |
el14ja | 3:8c5362716f4c | 565 | |
el14ja | 4:7160c74529f7 | 566 | completedlines += rowscompletecounter; //adds the new completed rows to the row total |
el14ja | 1:2b9ba34131ca | 567 | if(rowtoremove) {//runs if a row is complete |
el14ja | 1:2b9ba34131ca | 568 | int prevlevel = level; |
el14ja | 1:2b9ba34131ca | 569 | level = int((completedlines/10)+1); |
el14ja | 1:2b9ba34131ca | 570 | |
el14ja | 1:2b9ba34131ca | 571 | if (prevlevel != level) { |
el14ja | 4:7160c74529f7 | 572 | gamespeed = 0.8606*exp(-0.073*level); //the game speed is set depending on the level |
el14ja | 1:2b9ba34131ca | 573 | |
el14ja | 1:2b9ba34131ca | 574 | setTicker(gamespeed); |
el14ja | 1:2b9ba34131ca | 575 | } |
el14ja | 4:7160c74529f7 | 576 | removeCompleteLines(rowscomplete); |
el14ja | 0:ce5c75c04dc2 | 577 | } |
el14ja | 4:7160c74529f7 | 578 | |
el14ja | 4:7160c74529f7 | 579 | //increases the score depending on the number of rows complete |
el14ja | 1:2b9ba34131ca | 580 | if(rowscompletecounter == 1) { |
el14ja | 0:ce5c75c04dc2 | 581 | score += 100; |
el14ja | 1:2b9ba34131ca | 582 | } else if(rowscompletecounter == 2) { |
el14ja | 0:ce5c75c04dc2 | 583 | score += 250; |
el14ja | 1:2b9ba34131ca | 584 | } else if(rowscompletecounter == 3) { |
el14ja | 0:ce5c75c04dc2 | 585 | score += 500; |
el14ja | 1:2b9ba34131ca | 586 | } else if(rowscompletecounter == 4) { |
el14ja | 0:ce5c75c04dc2 | 587 | score += 1000; |
el14ja | 0:ce5c75c04dc2 | 588 | } |
el14ja | 0:ce5c75c04dc2 | 589 | } |
el14ja | 0:ce5c75c04dc2 | 590 | |
el14ja | 4:7160c74529f7 | 591 | void removeCompleteLines(int rowscomplete[4]) |
el14ja | 0:ce5c75c04dc2 | 592 | { |
el14ja | 1:2b9ba34131ca | 593 | for(int i = 0; i<4; i++) { //loops throught the max 4 lines that have been completed |
el14ja | 0:ce5c75c04dc2 | 594 | if(rowscomplete[i] >0) { |
el14ja | 0:ce5c75c04dc2 | 595 | |
el14ja | 0:ce5c75c04dc2 | 596 | for(int j = 1; j<11; j++) { //delete complete row |
el14ja | 0:ce5c75c04dc2 | 597 | gamearea[j][rowscomplete[i]] = 0; |
el14ja | 0:ce5c75c04dc2 | 598 | } |
el14ja | 0:ce5c75c04dc2 | 599 | |
el14ja | 0:ce5c75c04dc2 | 600 | for(int j = rowscomplete[i]; j>0; j--) { //runs through all the rows to copy, j starts at deleted row |
el14ja | 0:ce5c75c04dc2 | 601 | for(int k = 1; k<11; k++) { //runs through all the numbers in the row |
el14ja | 1:2b9ba34131ca | 602 | gamearea[k][j] = gamearea[k][j-1]; |
el14ja | 0:ce5c75c04dc2 | 603 | } |
el14ja | 0:ce5c75c04dc2 | 604 | } |
el14ja | 0:ce5c75c04dc2 | 605 | for(int j = 1; j<11; j++) { //add zeros into the top line |
el14ja | 0:ce5c75c04dc2 | 606 | gamearea[j][0] = 0; |
el14ja | 0:ce5c75c04dc2 | 607 | } |
el14ja | 0:ce5c75c04dc2 | 608 | } |
el14ja | 0:ce5c75c04dc2 | 609 | } |
el14ja | 0:ce5c75c04dc2 | 610 | } |
el14ja | 1:2b9ba34131ca | 611 | |
el14ja | 3:8c5362716f4c | 612 | void pieceToGameArray() |
el14ja | 1:2b9ba34131ca | 613 | { |
el14ja | 4:7160c74529f7 | 614 | int count = 0; //looks through the 16 bits of the shape |
el14ja | 1:2b9ba34131ca | 615 | for(int y = 0; y<4; y++) { |
el14ja | 1:2b9ba34131ca | 616 | for(int x = 0; x<4; x++) { |
el14ja | 4:7160c74529f7 | 617 | int bit = shapes[currentshape][orientation] & (1<<count); //finds the bits of each shape |
el14ja | 1:2b9ba34131ca | 618 | |
el14ja | 1:2b9ba34131ca | 619 | if (bit) { |
el14ja | 4:7160c74529f7 | 620 | gamearea[pieceposition[0]+x+1][pieceposition[1]+y]=1; //sets the shape in the game area array |
el14ja | 1:2b9ba34131ca | 621 | } |
el14ja | 1:2b9ba34131ca | 622 | count++; |
el14ja | 1:2b9ba34131ca | 623 | } |
el14ja | 1:2b9ba34131ca | 624 | } |
el14ja | 1:2b9ba34131ca | 625 | } |
el14ja | 1:2b9ba34131ca | 626 | |
el14ja | 0:ce5c75c04dc2 | 627 | void setGameArea() |
el14ja | 0:ce5c75c04dc2 | 628 | { |
el14ja | 3:8c5362716f4c | 629 | tetris.clearGame();//clears the area |
el14ja | 4:7160c74529f7 | 630 | for(int x = 1; x<11; x++) { //loops through the whole game array |
el14ja | 0:ce5c75c04dc2 | 631 | for(int y = 0; y<15; y++) { |
el14ja | 0:ce5c75c04dc2 | 632 | if(gamearea[x][y] == 1) { |
el14ja | 3:8c5362716f4c | 633 | tetris.gamePixel(x-1,y); //x-1 because the gamearea array has 1's around the outside |
el14ja | 0:ce5c75c04dc2 | 634 | } |
el14ja | 0:ce5c75c04dc2 | 635 | } |
el14ja | 0:ce5c75c04dc2 | 636 | } |
el14ja | 0:ce5c75c04dc2 | 637 | } |
el14ja | 1:2b9ba34131ca | 638 | |
el14ja | 1:2b9ba34131ca | 639 | void setTicker(float time) |
el14ja | 1:2b9ba34131ca | 640 | { |
el14ja | 2:61caca4dd3f0 | 641 | ticker.detach(); |
el14ja | 0:ce5c75c04dc2 | 642 | ticker.attach(&moveDownTicker, time); |
el14ja | 1:2b9ba34131ca | 643 | } |
el14ja | 0:ce5c75c04dc2 | 644 | |
el14ja | 4:7160c74529f7 | 645 | |
el14ja | 4:7160c74529f7 | 646 | |
el14ja | 0:ce5c75c04dc2 | 647 | void left_isr() |
el14ja | 0:ce5c75c04dc2 | 648 | { |
el14ja | 1:2b9ba34131ca | 649 | if(g_lefttimeout) { |
el14ja | 1:2b9ba34131ca | 650 | g_left = 1; |
el14ja | 4:7160c74529f7 | 651 | g_lefttimeout = 0; |
el14ja | 4:7160c74529f7 | 652 | pauseleft = 1; |
el14ja | 4:7160c74529f7 | 653 | lefttimeout.attach(&lefttimeout_isr,0.15); //sets the timeout so the button cant be pressed in quick sucsession |
el14ja | 4:7160c74529f7 | 654 | pausemenu.attach(&pausemenu_isr,0.09); //sets timeout for the detection of both left and right pressed at the same time |
el14ja | 1:2b9ba34131ca | 655 | } |
el14ja | 0:ce5c75c04dc2 | 656 | } |
el14ja | 0:ce5c75c04dc2 | 657 | void right_isr() |
el14ja | 0:ce5c75c04dc2 | 658 | { |
el14ja | 1:2b9ba34131ca | 659 | if(g_righttimeout) { |
el14ja | 1:2b9ba34131ca | 660 | g_right = 1; |
el14ja | 4:7160c74529f7 | 661 | g_righttimeout = 0; |
el14ja | 4:7160c74529f7 | 662 | pauseright = 1; |
el14ja | 4:7160c74529f7 | 663 | righttimeout.attach(&righttimeout_isr,0.15);//sets the timeout so the button cant be pressed in quick sucsession |
el14ja | 4:7160c74529f7 | 664 | pausemenu.attach(&pausemenu_isr,0.09); //sets timeout for the detection of both left and right pressed at the same time |
el14ja | 1:2b9ba34131ca | 665 | } |
el14ja | 0:ce5c75c04dc2 | 666 | } |
el14ja | 0:ce5c75c04dc2 | 667 | void spin_isr() |
el14ja | 0:ce5c75c04dc2 | 668 | { |
el14ja | 1:2b9ba34131ca | 669 | if(g_spintimeout) { |
el14ja | 1:2b9ba34131ca | 670 | g_spin = 1; |
el14ja | 4:7160c74529f7 | 671 | g_spintimeout = 0; |
el14ja | 4:7160c74529f7 | 672 | spintimeout.attach(&spintimeout_isr,0.15);//sets the timeout so the button cant be pressed in quick sucsession |
el14ja | 1:2b9ba34131ca | 673 | } |
el14ja | 1:2b9ba34131ca | 674 | } |
el14ja | 1:2b9ba34131ca | 675 | |
el14ja | 1:2b9ba34131ca | 676 | void down_isr() |
el14ja | 1:2b9ba34131ca | 677 | { |
el14ja | 1:2b9ba34131ca | 678 | if(g_downtimeout) { |
el14ja | 1:2b9ba34131ca | 679 | g_down = 1; |
el14ja | 4:7160c74529f7 | 680 | g_downtimeout = 0; |
el14ja | 4:7160c74529f7 | 681 | downtimeout.attach(&downtimeout_isr,0.4);//sets the timeout so the button cant be pressed in quick sucsession |
el14ja | 1:2b9ba34131ca | 682 | } |
el14ja | 1:2b9ba34131ca | 683 | } |
el14ja | 1:2b9ba34131ca | 684 | |
el14ja | 4:7160c74529f7 | 685 | //ISR's and flag setting |
el14ja | 4:7160c74529f7 | 686 | void moveDownTicker() |
el14ja | 4:7160c74529f7 | 687 | { |
el14ja | 4:7160c74529f7 | 688 | g_timer_flag = 1; |
el14ja | 4:7160c74529f7 | 689 | } |
el14ja | 4:7160c74529f7 | 690 | |
el14ja | 1:2b9ba34131ca | 691 | void music_isr() |
el14ja | 1:2b9ba34131ca | 692 | { |
el14ja | 1:2b9ba34131ca | 693 | g_music = 1; |
el14ja | 0:ce5c75c04dc2 | 694 | } |
el14ja | 0:ce5c75c04dc2 | 695 | |
el14ja | 1:2b9ba34131ca | 696 | void lefttimeout_isr() |
el14ja | 1:2b9ba34131ca | 697 | { |
el14ja | 1:2b9ba34131ca | 698 | g_lefttimeout = 1; |
el14ja | 1:2b9ba34131ca | 699 | } |
el14ja | 1:2b9ba34131ca | 700 | void righttimeout_isr() |
el14ja | 1:2b9ba34131ca | 701 | { |
el14ja | 1:2b9ba34131ca | 702 | g_righttimeout = 1; |
el14ja | 1:2b9ba34131ca | 703 | } |
el14ja | 1:2b9ba34131ca | 704 | void spintimeout_isr() |
el14ja | 1:2b9ba34131ca | 705 | { |
el14ja | 1:2b9ba34131ca | 706 | g_spintimeout = 1; |
el14ja | 1:2b9ba34131ca | 707 | } |
el14ja | 1:2b9ba34131ca | 708 | void downtimeout_isr() |
el14ja | 1:2b9ba34131ca | 709 | { |
el14ja | 1:2b9ba34131ca | 710 | g_downtimeout = 1; |
el14ja | 1:2b9ba34131ca | 711 | } |
el14ja | 1:2b9ba34131ca | 712 | |
el14ja | 4:7160c74529f7 | 713 | void pausemenu_isr() |
el14ja | 4:7160c74529f7 | 714 | { |
el14ja | 4:7160c74529f7 | 715 | g_pausemenu = 1; |
el14ja | 4:7160c74529f7 | 716 | } |
el14ja | 3:8c5362716f4c | 717 | |
el14ja | 4:7160c74529f7 | 718 | |
el14ja | 4:7160c74529f7 | 719 | //each move position it checks if the piece can be moved there and then moves them |
el14ja | 0:ce5c75c04dc2 | 720 | void movePieceRight() |
el14ja | 0:ce5c75c04dc2 | 721 | { |
el14ja | 2:61caca4dd3f0 | 722 | if(movePossible(pieceposition[0]-1,pieceposition[1],orientation)) { |
el14ja | 3:8c5362716f4c | 723 | movePiece(1, 0,0); |
el14ja | 2:61caca4dd3f0 | 724 | } |
el14ja | 0:ce5c75c04dc2 | 725 | } |
el14ja | 0:ce5c75c04dc2 | 726 | void movePieceLeft() |
el14ja | 0:ce5c75c04dc2 | 727 | { |
el14ja | 2:61caca4dd3f0 | 728 | if(movePossible(pieceposition[0]+1,pieceposition[1],orientation)) { |
el14ja | 3:8c5362716f4c | 729 | movePiece(-1, 0,0); |
el14ja | 2:61caca4dd3f0 | 730 | } |
el14ja | 0:ce5c75c04dc2 | 731 | } |
el14ja | 0:ce5c75c04dc2 | 732 | void spinPiece() |
el14ja | 0:ce5c75c04dc2 | 733 | { |
el14ja | 2:61caca4dd3f0 | 734 | if(movePossible(pieceposition[0],pieceposition[1],orientation+1)) { |
el14ja | 3:8c5362716f4c | 735 | |
el14ja | 3:8c5362716f4c | 736 | movePiece(0, 0,1); |
el14ja | 0:ce5c75c04dc2 | 737 | } |
el14ja | 0:ce5c75c04dc2 | 738 | } |
el14ja | 0:ce5c75c04dc2 | 739 | |
el14ja | 0:ce5c75c04dc2 | 740 | void movePieceDown() |
el14ja | 0:ce5c75c04dc2 | 741 | { |
el14ja | 2:61caca4dd3f0 | 742 | movePiece(0, 1,0); |
el14ja | 0:ce5c75c04dc2 | 743 | } |
el14ja | 0:ce5c75c04dc2 | 744 | |
el14ja | 4:7160c74529f7 | 745 | |
el14ja | 3:8c5362716f4c | 746 | void movePiece(int x, int y,int spin) |
el14ja | 3:8c5362716f4c | 747 | { |
el14ja | 4:7160c74529f7 | 748 | tetris.pieceClear(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]); //clears the piece before the piece is moved |
el14ja | 4:7160c74529f7 | 749 | //changes the piece position or orientation |
el14ja | 1:2b9ba34131ca | 750 | pieceposition[0]= pieceposition[0] - x; |
el14ja | 1:2b9ba34131ca | 751 | pieceposition[1]= pieceposition[1] + y; |
el14ja | 1:2b9ba34131ca | 752 | orientation = orientation + spin; |
el14ja | 3:8c5362716f4c | 753 | if (orientation > 3) { |
el14ja | 1:2b9ba34131ca | 754 | orientation = 0; |
el14ja | 1:2b9ba34131ca | 755 | } |
el14ja | 4:7160c74529f7 | 756 | tetris.piecePlace(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]); //resets the piece |
el14ja | 3:8c5362716f4c | 757 | } |
el14ja | 3:8c5362716f4c | 758 | |
el14ja | 3:8c5362716f4c | 759 |