ELEC2645 Joseph Allison 200860349
Dependencies: N5110 Tetris mbed
main.cpp@2:61caca4dd3f0, 2016-04-07 (annotated)
- Committer:
- el14ja
- Date:
- Thu Apr 07 15:42:46 2016 +0000
- Revision:
- 2:61caca4dd3f0
- Parent:
- 1:2b9ba34131ca
- Child:
- 3:8c5362716f4c
attempted library integration;
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 | 0:ce5c75c04dc2 | 9 | week 21 - starting to imple,ent 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 | 0:ce5c75c04dc2 | 15 | */ |
el14ja | 0:ce5c75c04dc2 | 16 | |
el14ja | 0:ce5c75c04dc2 | 17 | |
el14ja | 0:ce5c75c04dc2 | 18 | |
el14ja | 1:2b9ba34131ca | 19 | #include "main.h" |
el14ja | 0:ce5c75c04dc2 | 20 | |
el14ja | 2:61caca4dd3f0 | 21 | double currentgamespeed = 0.8; |
el14ja | 0:ce5c75c04dc2 | 22 | |
el14ja | 0:ce5c75c04dc2 | 23 | int main() |
el14ja | 0:ce5c75c04dc2 | 24 | { |
el14ja | 0:ce5c75c04dc2 | 25 | |
el14ja | 0:ce5c75c04dc2 | 26 | pc.baud(115200); |
el14ja | 0:ce5c75c04dc2 | 27 | srand(floor(ain.read()*10000)); |
el14ja | 0:ce5c75c04dc2 | 28 | |
el14ja | 2:61caca4dd3f0 | 29 | //buzzer.write(0.5); ***** |
el14ja | 0:ce5c75c04dc2 | 30 | |
el14ja | 0:ce5c75c04dc2 | 31 | left.mode(PullDown); |
el14ja | 0:ce5c75c04dc2 | 32 | right.mode(PullDown); |
el14ja | 0:ce5c75c04dc2 | 33 | spin.mode(PullDown); |
el14ja | 0:ce5c75c04dc2 | 34 | down.mode(PullDown); |
el14ja | 1:2b9ba34131ca | 35 | |
el14ja | 0:ce5c75c04dc2 | 36 | lcd.init(); |
el14ja | 0:ce5c75c04dc2 | 37 | gameSetup(); |
el14ja | 0:ce5c75c04dc2 | 38 | newShapeBag();//create bag of shapes to use |
el14ja | 0:ce5c75c04dc2 | 39 | nextpiece = nextshape[1]; |
el14ja | 0:ce5c75c04dc2 | 40 | gameInfo(0,1,nextpiece); |
el14ja | 0:ce5c75c04dc2 | 41 | |
el14ja | 0:ce5c75c04dc2 | 42 | |
el14ja | 1:2b9ba34131ca | 43 | left.rise(&left_isr); |
el14ja | 1:2b9ba34131ca | 44 | right.rise(&right_isr); |
el14ja | 1:2b9ba34131ca | 45 | spin.rise(&spin_isr); |
el14ja | 1:2b9ba34131ca | 46 | down.rise(&down_isr); |
el14ja | 0:ce5c75c04dc2 | 47 | |
el14ja | 1:2b9ba34131ca | 48 | //musicticker.attach(&music_isr,0.25); ***** stopped the music |
el14ja | 1:2b9ba34131ca | 49 | |
el14ja | 1:2b9ba34131ca | 50 | setTicker(gamespeed); |
el14ja | 2:61caca4dd3f0 | 51 | /***** |
el14ja | 2:61caca4dd3f0 | 52 | *******GAMESPEED*******!!!!!!! |
el14ja | 2:61caca4dd3f0 | 53 | */ |
el14ja | 0:ce5c75c04dc2 | 54 | while (true) { |
el14ja | 0:ce5c75c04dc2 | 55 | |
el14ja | 0:ce5c75c04dc2 | 56 | if(g_newpiece) { |
el14ja | 0:ce5c75c04dc2 | 57 | g_newpiece = 0; |
el14ja | 0:ce5c75c04dc2 | 58 | newpiece(); |
el14ja | 0:ce5c75c04dc2 | 59 | } |
el14ja | 0:ce5c75c04dc2 | 60 | |
el14ja | 0:ce5c75c04dc2 | 61 | if(g_right) { |
el14ja | 0:ce5c75c04dc2 | 62 | g_right = 0; |
el14ja | 1:2b9ba34131ca | 63 | g_righttimeout = 0; |
el14ja | 1:2b9ba34131ca | 64 | righttimeout.attach(&righttimeout_isr,0.15); |
el14ja | 2:61caca4dd3f0 | 65 | |
el14ja | 2:61caca4dd3f0 | 66 | tetris.movePieceRight(); |
el14ja | 2:61caca4dd3f0 | 67 | |
el14ja | 0:ce5c75c04dc2 | 68 | } |
el14ja | 0:ce5c75c04dc2 | 69 | |
el14ja | 0:ce5c75c04dc2 | 70 | if(g_left) { |
el14ja | 0:ce5c75c04dc2 | 71 | g_left = 0; |
el14ja | 1:2b9ba34131ca | 72 | g_lefttimeout = 0; |
el14ja | 1:2b9ba34131ca | 73 | lefttimeout.attach(&lefttimeout_isr,0.15); |
el14ja | 2:61caca4dd3f0 | 74 | |
el14ja | 2:61caca4dd3f0 | 75 | tetris.movePieceLeft(); |
el14ja | 2:61caca4dd3f0 | 76 | |
el14ja | 0:ce5c75c04dc2 | 77 | } |
el14ja | 2:61caca4dd3f0 | 78 | //ANTI-DOUBLE PRESS******* |
el14ja | 0:ce5c75c04dc2 | 79 | if (g_timer_flag) { |
el14ja | 0:ce5c75c04dc2 | 80 | g_timer_flag = 0; |
el14ja | 0:ce5c75c04dc2 | 81 | |
el14ja | 0:ce5c75c04dc2 | 82 | if(movePossible(pieceposition[0],pieceposition[1]+1,orientation)) { //returns false if peicne cant move down |
el14ja | 0:ce5c75c04dc2 | 83 | |
el14ja | 2:61caca4dd3f0 | 84 | tetris.movePieceDown(); |
el14ja | 0:ce5c75c04dc2 | 85 | |
el14ja | 0:ce5c75c04dc2 | 86 | } else { //piece placed |
el14ja | 0:ce5c75c04dc2 | 87 | |
el14ja | 1:2b9ba34131ca | 88 | pieceToGameArray(); |
el14ja | 1:2b9ba34131ca | 89 | g_newpiece = 1; |
el14ja | 0:ce5c75c04dc2 | 90 | |
el14ja | 1:2b9ba34131ca | 91 | if(harddropping) { |
el14ja | 0:ce5c75c04dc2 | 92 | harddropping = 0; |
el14ja | 0:ce5c75c04dc2 | 93 | ticker.detach(); |
el14ja | 1:2b9ba34131ca | 94 | setTicker(gamespeed); |
el14ja | 1:2b9ba34131ca | 95 | } |
el14ja | 0:ce5c75c04dc2 | 96 | checkCompleteLine(); |
el14ja | 2:61caca4dd3f0 | 97 | |
el14ja | 0:ce5c75c04dc2 | 98 | |
el14ja | 0:ce5c75c04dc2 | 99 | } |
el14ja | 0:ce5c75c04dc2 | 100 | } |
el14ja | 0:ce5c75c04dc2 | 101 | |
el14ja | 0:ce5c75c04dc2 | 102 | if(g_spin) { |
el14ja | 0:ce5c75c04dc2 | 103 | g_spin = 0; |
el14ja | 1:2b9ba34131ca | 104 | g_spintimeout = 0; |
el14ja | 1:2b9ba34131ca | 105 | spintimeout.attach(&spintimeout_isr,0.15); |
el14ja | 0:ce5c75c04dc2 | 106 | //check if peice can spin |
el14ja | 2:61caca4dd3f0 | 107 | |
el14ja | 2:61caca4dd3f0 | 108 | tetris.spinPiece(); |
el14ja | 2:61caca4dd3f0 | 109 | |
el14ja | 0:ce5c75c04dc2 | 110 | } |
el14ja | 1:2b9ba34131ca | 111 | |
el14ja | 1:2b9ba34131ca | 112 | if(g_down) { |
el14ja | 0:ce5c75c04dc2 | 113 | g_down = 0; |
el14ja | 1:2b9ba34131ca | 114 | g_downtimeout = 0; |
el14ja | 1:2b9ba34131ca | 115 | downtimeout.attach(&downtimeout_isr,0.15); |
el14ja | 0:ce5c75c04dc2 | 116 | ticker.detach(); |
el14ja | 1:2b9ba34131ca | 117 | setTicker(0.001); |
el14ja | 0:ce5c75c04dc2 | 118 | harddropping = 1; |
el14ja | 1:2b9ba34131ca | 119 | } |
el14ja | 1:2b9ba34131ca | 120 | |
el14ja | 1:2b9ba34131ca | 121 | if(g_music) { |
el14ja | 1:2b9ba34131ca | 122 | g_music = 0; |
el14ja | 1:2b9ba34131ca | 123 | buzzer.period_us(musisnotes[musiccounter]); |
el14ja | 1:2b9ba34131ca | 124 | musiccounter++; |
el14ja | 1:2b9ba34131ca | 125 | |
el14ja | 1:2b9ba34131ca | 126 | if(musiccounter > 63) { |
el14ja | 1:2b9ba34131ca | 127 | musiccounter = 0; |
el14ja | 0:ce5c75c04dc2 | 128 | } |
el14ja | 1:2b9ba34131ca | 129 | |
el14ja | 1:2b9ba34131ca | 130 | } |
el14ja | 1:2b9ba34131ca | 131 | |
el14ja | 0:ce5c75c04dc2 | 132 | sleep(); |
el14ja | 0:ce5c75c04dc2 | 133 | |
el14ja | 2:61caca4dd3f0 | 134 | } |
el14ja | 0:ce5c75c04dc2 | 135 | } |
el14ja | 2:61caca4dd3f0 | 136 | //**** |
el14ja | 0:ce5c75c04dc2 | 137 | void gameSetup() |
el14ja | 0:ce5c75c04dc2 | 138 | { |
el14ja | 0:ce5c75c04dc2 | 139 | |
el14ja | 0:ce5c75c04dc2 | 140 | //drawRect(int x0,int y0,int width,int height,int fill) |
el14ja | 0:ce5c75c04dc2 | 141 | lcd.printString("Level:",0,0); |
el14ja | 0:ce5c75c04dc2 | 142 | lcd.printString("Score:",0,2); |
el14ja | 0:ce5c75c04dc2 | 143 | lcd.printString("0",0,1); |
el14ja | 0:ce5c75c04dc2 | 144 | lcd.printString("Next",0,4); |
el14ja | 0:ce5c75c04dc2 | 145 | |
el14ja | 0:ce5c75c04dc2 | 146 | |
el14ja | 0:ce5c75c04dc2 | 147 | //creats the game boarder |
el14ja | 0:ce5c75c04dc2 | 148 | lcd.drawRect(40,0,33,47,0); |
el14ja | 0:ce5c75c04dc2 | 149 | lcd.drawRect(41,1,31,46,0); |
el14ja | 0:ce5c75c04dc2 | 150 | lcd.refresh(); |
el14ja | 0:ce5c75c04dc2 | 151 | |
el14ja | 0:ce5c75c04dc2 | 152 | //fill gamearea add the walls |
el14ja | 0:ce5c75c04dc2 | 153 | |
el14ja | 0:ce5c75c04dc2 | 154 | for(int i = 0; i<12; i++) { |
el14ja | 0:ce5c75c04dc2 | 155 | for(int j = 0; j<16; j++) { |
el14ja | 0:ce5c75c04dc2 | 156 | gamearea[i][j] = 1; |
el14ja | 0:ce5c75c04dc2 | 157 | } |
el14ja | 0:ce5c75c04dc2 | 158 | } |
el14ja | 0:ce5c75c04dc2 | 159 | |
el14ja | 0:ce5c75c04dc2 | 160 | for(int i = 1; i<11; i++) { |
el14ja | 0:ce5c75c04dc2 | 161 | for(int j = 0; j<15; j++) { |
el14ja | 0:ce5c75c04dc2 | 162 | gamearea[i][j] = 0; |
el14ja | 0:ce5c75c04dc2 | 163 | } |
el14ja | 0:ce5c75c04dc2 | 164 | } |
el14ja | 0:ce5c75c04dc2 | 165 | } |
el14ja | 1:2b9ba34131ca | 166 | //** |
el14ja | 1:2b9ba34131ca | 167 | void gamePixel(int x, int y)//*** |
el14ja | 0:ce5c75c04dc2 | 168 | { |
el14ja | 0:ce5c75c04dc2 | 169 | |
el14ja | 0:ce5c75c04dc2 | 170 | lcd.drawRect(42+(3*x),2+(3*y),2,2,1); |
el14ja | 0:ce5c75c04dc2 | 171 | lcd.refresh(); |
el14ja | 0:ce5c75c04dc2 | 172 | } |
el14ja | 1:2b9ba34131ca | 173 | //** |
el14ja | 1:2b9ba34131ca | 174 | void clearGamePixel(int x, int y)//*** |
el14ja | 0:ce5c75c04dc2 | 175 | { |
el14ja | 0:ce5c75c04dc2 | 176 | |
el14ja | 0:ce5c75c04dc2 | 177 | for(int i =0; i<3; i++) { |
el14ja | 0:ce5c75c04dc2 | 178 | for(int j =0; j<3; j++) { |
el14ja | 0:ce5c75c04dc2 | 179 | lcd.clearPixel(42+(x*3)+i,2+(3*y)+j); |
el14ja | 0:ce5c75c04dc2 | 180 | |
el14ja | 0:ce5c75c04dc2 | 181 | } |
el14ja | 0:ce5c75c04dc2 | 182 | } |
el14ja | 0:ce5c75c04dc2 | 183 | lcd.refresh(); |
el14ja | 0:ce5c75c04dc2 | 184 | |
el14ja | 0:ce5c75c04dc2 | 185 | } |
el14ja | 0:ce5c75c04dc2 | 186 | |
el14ja | 1:2b9ba34131ca | 187 | void clearGame() //** |
el14ja | 0:ce5c75c04dc2 | 188 | { |
el14ja | 0:ce5c75c04dc2 | 189 | |
el14ja | 0:ce5c75c04dc2 | 190 | for(int i = 0; i<30; i++) { |
el14ja | 0:ce5c75c04dc2 | 191 | |
el14ja | 0:ce5c75c04dc2 | 192 | for(int j = 0; j<45; j++) { |
el14ja | 0:ce5c75c04dc2 | 193 | |
el14ja | 0:ce5c75c04dc2 | 194 | lcd.clearPixel(42+i,2+j); |
el14ja | 0:ce5c75c04dc2 | 195 | } |
el14ja | 0:ce5c75c04dc2 | 196 | } |
el14ja | 0:ce5c75c04dc2 | 197 | lcd.refresh(); |
el14ja | 0:ce5c75c04dc2 | 198 | } |
el14ja | 0:ce5c75c04dc2 | 199 | |
el14ja | 0:ce5c75c04dc2 | 200 | void gameInfo(int score,int level,int next) |
el14ja | 0:ce5c75c04dc2 | 201 | { |
el14ja | 0:ce5c75c04dc2 | 202 | |
el14ja | 0:ce5c75c04dc2 | 203 | //outputs the level |
el14ja | 0:ce5c75c04dc2 | 204 | char buffer[6]; |
el14ja | 0:ce5c75c04dc2 | 205 | int length = sprintf(buffer,"%d",level); |
el14ja | 0:ce5c75c04dc2 | 206 | if(length <=6) { |
el14ja | 0:ce5c75c04dc2 | 207 | lcd.printString(" ",0,1); |
el14ja | 0:ce5c75c04dc2 | 208 | lcd.printString(buffer,0,1); |
el14ja | 0:ce5c75c04dc2 | 209 | } |
el14ja | 0:ce5c75c04dc2 | 210 | //outputs the score |
el14ja | 0:ce5c75c04dc2 | 211 | length = sprintf(buffer,"%d",score); |
el14ja | 0:ce5c75c04dc2 | 212 | if(length <=6) { |
el14ja | 0:ce5c75c04dc2 | 213 | lcd.printString(" ",0,3); |
el14ja | 0:ce5c75c04dc2 | 214 | lcd.printString(buffer,0,3); |
el14ja | 0:ce5c75c04dc2 | 215 | } |
el14ja | 0:ce5c75c04dc2 | 216 | |
el14ja | 0:ce5c75c04dc2 | 217 | for(int i = 0; i <4; i++) { //clears the next piece shape before the new one is displayed |
el14ja | 0:ce5c75c04dc2 | 218 | for(int j = 0; j <2; j++) { |
el14ja | 0:ce5c75c04dc2 | 219 | clearGamePixel(-6+i,12+j); |
el14ja | 0:ce5c75c04dc2 | 220 | } |
el14ja | 0:ce5c75c04dc2 | 221 | } |
el14ja | 0:ce5c75c04dc2 | 222 | piecePlace(-6,12,shapes[next][0]) ; |
el14ja | 0:ce5c75c04dc2 | 223 | } |
el14ja | 0:ce5c75c04dc2 | 224 | |
el14ja | 1:2b9ba34131ca | 225 | void piecePlace(int x,int y,int shape)//*** |
el14ja | 0:ce5c75c04dc2 | 226 | { |
el14ja | 0:ce5c75c04dc2 | 227 | |
el14ja | 0:ce5c75c04dc2 | 228 | |
el14ja | 0:ce5c75c04dc2 | 229 | int count = 0; |
el14ja | 0:ce5c75c04dc2 | 230 | for(int i = 0; i<4; i++) { |
el14ja | 0:ce5c75c04dc2 | 231 | for(int j = 0; j<4; j++) { |
el14ja | 0:ce5c75c04dc2 | 232 | |
el14ja | 0:ce5c75c04dc2 | 233 | int bit = shape & (1<<count); |
el14ja | 0:ce5c75c04dc2 | 234 | |
el14ja | 0:ce5c75c04dc2 | 235 | if (bit) { |
el14ja | 0:ce5c75c04dc2 | 236 | gamePixel(j+x,i+y); |
el14ja | 0:ce5c75c04dc2 | 237 | } |
el14ja | 0:ce5c75c04dc2 | 238 | count++; |
el14ja | 0:ce5c75c04dc2 | 239 | } |
el14ja | 0:ce5c75c04dc2 | 240 | } |
el14ja | 0:ce5c75c04dc2 | 241 | } |
el14ja | 0:ce5c75c04dc2 | 242 | |
el14ja | 1:2b9ba34131ca | 243 | void pieceClear(int x,int y,int shape)//*** |
el14ja | 0:ce5c75c04dc2 | 244 | { |
el14ja | 0:ce5c75c04dc2 | 245 | int count = 0; |
el14ja | 0:ce5c75c04dc2 | 246 | for(int i = 0; i<4; i++) { |
el14ja | 0:ce5c75c04dc2 | 247 | for(int j = 0; j<4; j++) { |
el14ja | 0:ce5c75c04dc2 | 248 | |
el14ja | 0:ce5c75c04dc2 | 249 | int bit = shape & (1<<count); |
el14ja | 0:ce5c75c04dc2 | 250 | |
el14ja | 0:ce5c75c04dc2 | 251 | if (bit) { |
el14ja | 0:ce5c75c04dc2 | 252 | clearGamePixel(j+x,i+y); |
el14ja | 0:ce5c75c04dc2 | 253 | } |
el14ja | 0:ce5c75c04dc2 | 254 | count++; |
el14ja | 0:ce5c75c04dc2 | 255 | } |
el14ja | 0:ce5c75c04dc2 | 256 | } |
el14ja | 0:ce5c75c04dc2 | 257 | } |
el14ja | 0:ce5c75c04dc2 | 258 | |
el14ja | 2:61caca4dd3f0 | 259 | bool movePossible(int xpos, int ypos, int orientation)//** |
el14ja | 0:ce5c75c04dc2 | 260 | { |
el14ja | 0:ce5c75c04dc2 | 261 | |
el14ja | 0:ce5c75c04dc2 | 262 | if (orientation > 3) { |
el14ja | 0:ce5c75c04dc2 | 263 | orientation = 0; |
el14ja | 0:ce5c75c04dc2 | 264 | } |
el14ja | 0:ce5c75c04dc2 | 265 | int count = 0; |
el14ja | 0:ce5c75c04dc2 | 266 | |
el14ja | 0:ce5c75c04dc2 | 267 | for(int y = 0; y < 4; y++) { |
el14ja | 0:ce5c75c04dc2 | 268 | for(int x = 0; x < 4; x++) { |
el14ja | 0:ce5c75c04dc2 | 269 | |
el14ja | 0:ce5c75c04dc2 | 270 | int bit = shapes[currentshape][orientation] & (1<<count); //find the bit of each pixel |
el14ja | 0:ce5c75c04dc2 | 271 | |
el14ja | 0:ce5c75c04dc2 | 272 | count ++; |
el14ja | 0:ce5c75c04dc2 | 273 | |
el14ja | 0:ce5c75c04dc2 | 274 | if ((bit !=0) && (gamearea[xpos+x+1][ypos+y]!=0)) { |
el14ja | 0:ce5c75c04dc2 | 275 | return false; |
el14ja | 0:ce5c75c04dc2 | 276 | } |
el14ja | 0:ce5c75c04dc2 | 277 | } |
el14ja | 0:ce5c75c04dc2 | 278 | } |
el14ja | 0:ce5c75c04dc2 | 279 | |
el14ja | 0:ce5c75c04dc2 | 280 | return true; |
el14ja | 0:ce5c75c04dc2 | 281 | } |
el14ja | 0:ce5c75c04dc2 | 282 | |
el14ja | 2:61caca4dd3f0 | 283 | void newpiece() //** |
el14ja | 0:ce5c75c04dc2 | 284 | { |
el14ja | 0:ce5c75c04dc2 | 285 | orientation = 0; //set the orientation back to zero |
el14ja | 0:ce5c75c04dc2 | 286 | pieceposition[0] = 4; //sets the position back to the top |
el14ja | 0:ce5c75c04dc2 | 287 | pieceposition[1] = 0; |
el14ja | 0:ce5c75c04dc2 | 288 | |
el14ja | 1:2b9ba34131ca | 289 | //clearGame(); |
el14ja | 1:2b9ba34131ca | 290 | //setGameArea(); |
el14ja | 1:2b9ba34131ca | 291 | |
el14ja | 0:ce5c75c04dc2 | 292 | currentshape = nextshape[shapebagcounter]; |
el14ja | 0:ce5c75c04dc2 | 293 | piecePlace(pieceposition[0],pieceposition[1],currentshape);//places the new piece |
el14ja | 0:ce5c75c04dc2 | 294 | |
el14ja | 0:ce5c75c04dc2 | 295 | |
el14ja | 0:ce5c75c04dc2 | 296 | if(shapebagcounter > 5) { //when counter becomes 6 |
el14ja | 0:ce5c75c04dc2 | 297 | newShapeBag(); //a new bag is created |
el14ja | 0:ce5c75c04dc2 | 298 | shapebagcounter = 0; |
el14ja | 0:ce5c75c04dc2 | 299 | nextpiece = nextshape[0]; |
el14ja | 0:ce5c75c04dc2 | 300 | } else { |
el14ja | 0:ce5c75c04dc2 | 301 | nextpiece = nextshape[shapebagcounter+1]; |
el14ja | 0:ce5c75c04dc2 | 302 | shapebagcounter++; //incrimenst the counter |
el14ja | 0:ce5c75c04dc2 | 303 | } |
el14ja | 1:2b9ba34131ca | 304 | for(int j = 0; j<16; j++) { |
el14ja | 1:2b9ba34131ca | 305 | for(int i = 0; i<12; i++) { |
el14ja | 1:2b9ba34131ca | 306 | pc.printf("%d ",gamearea[i][j]); |
el14ja | 1:2b9ba34131ca | 307 | } |
el14ja | 1:2b9ba34131ca | 308 | pc.printf("\n"); |
el14ja | 1:2b9ba34131ca | 309 | } |
el14ja | 0:ce5c75c04dc2 | 310 | gameInfo(score,level,nextpiece); |
el14ja | 0:ce5c75c04dc2 | 311 | } |
el14ja | 0:ce5c75c04dc2 | 312 | |
el14ja | 2:61caca4dd3f0 | 313 | void newShapeBag() //** |
el14ja | 0:ce5c75c04dc2 | 314 | { |
el14ja | 0:ce5c75c04dc2 | 315 | |
el14ja | 0:ce5c75c04dc2 | 316 | bool match = false; |
el14ja | 0:ce5c75c04dc2 | 317 | int newrandomnumber; |
el14ja | 0:ce5c75c04dc2 | 318 | |
el14ja | 0:ce5c75c04dc2 | 319 | nextshape[0] = rand()%7; //creates first random number |
el14ja | 0:ce5c75c04dc2 | 320 | |
el14ja | 0:ce5c75c04dc2 | 321 | for(int i = 1; i < 7; i++) { |
el14ja | 0:ce5c75c04dc2 | 322 | |
el14ja | 0:ce5c75c04dc2 | 323 | do { |
el14ja | 0:ce5c75c04dc2 | 324 | newrandomnumber = rand()%7; |
el14ja | 0:ce5c75c04dc2 | 325 | |
el14ja | 0:ce5c75c04dc2 | 326 | match = false; //set to false to end the do while if no match is found |
el14ja | 0:ce5c75c04dc2 | 327 | |
el14ja | 0:ce5c75c04dc2 | 328 | for(int j = 0; j < i; j++) { |
el14ja | 0:ce5c75c04dc2 | 329 | |
el14ja | 0:ce5c75c04dc2 | 330 | if (newrandomnumber == nextshape[j]) { //checks to see if number has already been chosen |
el14ja | 0:ce5c75c04dc2 | 331 | match = true;// continue do while if a match is found |
el14ja | 0:ce5c75c04dc2 | 332 | break; //end the for loop that is checking for a match |
el14ja | 0:ce5c75c04dc2 | 333 | } |
el14ja | 0:ce5c75c04dc2 | 334 | } |
el14ja | 0:ce5c75c04dc2 | 335 | } while(match); |
el14ja | 0:ce5c75c04dc2 | 336 | |
el14ja | 0:ce5c75c04dc2 | 337 | nextshape[i] = newrandomnumber; //set new found number into the newshape array |
el14ja | 0:ce5c75c04dc2 | 338 | } |
el14ja | 0:ce5c75c04dc2 | 339 | } |
el14ja | 0:ce5c75c04dc2 | 340 | |
el14ja | 2:61caca4dd3f0 | 341 | void checkCompleteLine() //** |
el14ja | 0:ce5c75c04dc2 | 342 | { |
el14ja | 0:ce5c75c04dc2 | 343 | |
el14ja | 0:ce5c75c04dc2 | 344 | int rowscomplete[4] = {0,0,0,0}; //max of 4 rows can be completed with a single block |
el14ja | 0:ce5c75c04dc2 | 345 | int rowscompletecounter = 0; //counter to place complete row in the array |
el14ja | 0:ce5c75c04dc2 | 346 | int sum = 0; |
el14ja | 0:ce5c75c04dc2 | 347 | bool rowtoremove = false; |
el14ja | 0:ce5c75c04dc2 | 348 | for(int y = 0; y <15; y++) { //searches through all the rows |
el14ja | 0:ce5c75c04dc2 | 349 | |
el14ja | 0:ce5c75c04dc2 | 350 | for(int x = 1; x<11; x++) { |
el14ja | 0:ce5c75c04dc2 | 351 | if(gamearea[x][y] == 1) { |
el14ja | 1:2b9ba34131ca | 352 | sum++; //counts how many filled cells |
el14ja | 0:ce5c75c04dc2 | 353 | } |
el14ja | 0:ce5c75c04dc2 | 354 | } |
el14ja | 1:2b9ba34131ca | 355 | if(sum == 10) { //if all 10 cells are full |
el14ja | 0:ce5c75c04dc2 | 356 | //pc.printf("%d complete \n",y); |
el14ja | 1:2b9ba34131ca | 357 | rowscomplete[rowscompletecounter] = y; //sets it as a completed row |
el14ja | 0:ce5c75c04dc2 | 358 | rowscompletecounter++; |
el14ja | 0:ce5c75c04dc2 | 359 | rowtoremove = true; |
el14ja | 0:ce5c75c04dc2 | 360 | } |
el14ja | 0:ce5c75c04dc2 | 361 | sum = 0; |
el14ja | 0:ce5c75c04dc2 | 362 | } |
el14ja | 2:61caca4dd3f0 | 363 | |
el14ja | 0:ce5c75c04dc2 | 364 | |
el14ja | 1:2b9ba34131ca | 365 | if(rowtoremove) {//runs if a row is complete |
el14ja | 1:2b9ba34131ca | 366 | int prevlevel = level; |
el14ja | 1:2b9ba34131ca | 367 | level = int((completedlines/10)+1); |
el14ja | 1:2b9ba34131ca | 368 | |
el14ja | 1:2b9ba34131ca | 369 | if (prevlevel != level) { |
el14ja | 1:2b9ba34131ca | 370 | gamespeed = 0.8606*exp(-0.073*level); |
el14ja | 1:2b9ba34131ca | 371 | |
el14ja | 1:2b9ba34131ca | 372 | ticker.detach(); |
el14ja | 1:2b9ba34131ca | 373 | setTicker(gamespeed); |
el14ja | 1:2b9ba34131ca | 374 | } |
el14ja | 0:ce5c75c04dc2 | 375 | removeCompleteLines(rowscomplete); |
el14ja | 0:ce5c75c04dc2 | 376 | } |
el14ja | 0:ce5c75c04dc2 | 377 | completedlines += rowscompletecounter; |
el14ja | 1:2b9ba34131ca | 378 | if(rowscompletecounter == 1) { |
el14ja | 0:ce5c75c04dc2 | 379 | score += 100; |
el14ja | 1:2b9ba34131ca | 380 | } else if(rowscompletecounter == 2) { |
el14ja | 0:ce5c75c04dc2 | 381 | score += 250; |
el14ja | 1:2b9ba34131ca | 382 | } else if(rowscompletecounter == 3) { |
el14ja | 0:ce5c75c04dc2 | 383 | score += 500; |
el14ja | 1:2b9ba34131ca | 384 | } else if(rowscompletecounter == 4) { |
el14ja | 0:ce5c75c04dc2 | 385 | score += 1000; |
el14ja | 0:ce5c75c04dc2 | 386 | } |
el14ja | 0:ce5c75c04dc2 | 387 | } |
el14ja | 0:ce5c75c04dc2 | 388 | |
el14ja | 2:61caca4dd3f0 | 389 | void removeCompleteLines(int rowscomplete[4]) //** |
el14ja | 0:ce5c75c04dc2 | 390 | { |
el14ja | 1:2b9ba34131ca | 391 | for(int i = 0; i<4; i++) { //loops throught the max 4 lines that have been completed |
el14ja | 0:ce5c75c04dc2 | 392 | if(rowscomplete[i] >0) { |
el14ja | 0:ce5c75c04dc2 | 393 | |
el14ja | 0:ce5c75c04dc2 | 394 | for(int j = 1; j<11; j++) { //delete complete row |
el14ja | 0:ce5c75c04dc2 | 395 | gamearea[j][rowscomplete[i]] = 0; |
el14ja | 0:ce5c75c04dc2 | 396 | } |
el14ja | 0:ce5c75c04dc2 | 397 | |
el14ja | 0:ce5c75c04dc2 | 398 | for(int j = rowscomplete[i]; j>0; j--) { //runs through all the rows to copy, j starts at deleted row |
el14ja | 0:ce5c75c04dc2 | 399 | for(int k = 1; k<11; k++) { //runs through all the numbers in the row |
el14ja | 1:2b9ba34131ca | 400 | gamearea[k][j] = gamearea[k][j-1]; |
el14ja | 0:ce5c75c04dc2 | 401 | } |
el14ja | 0:ce5c75c04dc2 | 402 | } |
el14ja | 0:ce5c75c04dc2 | 403 | for(int j = 1; j<11; j++) { //add zeros into the top line |
el14ja | 0:ce5c75c04dc2 | 404 | gamearea[j][0] = 0; |
el14ja | 0:ce5c75c04dc2 | 405 | } |
el14ja | 0:ce5c75c04dc2 | 406 | |
el14ja | 0:ce5c75c04dc2 | 407 | } |
el14ja | 0:ce5c75c04dc2 | 408 | } |
el14ja | 2:61caca4dd3f0 | 409 | setGameArea(); |
el14ja | 0:ce5c75c04dc2 | 410 | } |
el14ja | 1:2b9ba34131ca | 411 | |
el14ja | 2:61caca4dd3f0 | 412 | void pieceToGameArray() |
el14ja | 1:2b9ba34131ca | 413 | { |
el14ja | 1:2b9ba34131ca | 414 | int count = 0; |
el14ja | 1:2b9ba34131ca | 415 | for(int y = 0; y<4; y++) { |
el14ja | 1:2b9ba34131ca | 416 | for(int x = 0; x<4; x++) { |
el14ja | 1:2b9ba34131ca | 417 | int bit = shapes[currentshape][orientation] & (1<<count); |
el14ja | 1:2b9ba34131ca | 418 | |
el14ja | 1:2b9ba34131ca | 419 | if (bit) { |
el14ja | 1:2b9ba34131ca | 420 | gamearea[pieceposition[0]+x+1][pieceposition[1]+y]=1; |
el14ja | 1:2b9ba34131ca | 421 | } |
el14ja | 1:2b9ba34131ca | 422 | count++; |
el14ja | 1:2b9ba34131ca | 423 | } |
el14ja | 1:2b9ba34131ca | 424 | } |
el14ja | 1:2b9ba34131ca | 425 | |
el14ja | 1:2b9ba34131ca | 426 | } |
el14ja | 1:2b9ba34131ca | 427 | |
el14ja | 0:ce5c75c04dc2 | 428 | void setGameArea() |
el14ja | 0:ce5c75c04dc2 | 429 | { |
el14ja | 0:ce5c75c04dc2 | 430 | clearGame();//clears the area |
el14ja | 0:ce5c75c04dc2 | 431 | for(int x = 1; x<11; x++) { |
el14ja | 0:ce5c75c04dc2 | 432 | for(int y = 0; y<15; y++) { |
el14ja | 0:ce5c75c04dc2 | 433 | if(gamearea[x][y] == 1) { |
el14ja | 0:ce5c75c04dc2 | 434 | gamePixel(x-1,y); //x-1 because the gamearea array has 1's around the outside |
el14ja | 0:ce5c75c04dc2 | 435 | } |
el14ja | 0:ce5c75c04dc2 | 436 | } |
el14ja | 0:ce5c75c04dc2 | 437 | } |
el14ja | 0:ce5c75c04dc2 | 438 | } |
el14ja | 1:2b9ba34131ca | 439 | |
el14ja | 1:2b9ba34131ca | 440 | void setTicker(float time) |
el14ja | 1:2b9ba34131ca | 441 | { |
el14ja | 2:61caca4dd3f0 | 442 | ticker.detach(); |
el14ja | 0:ce5c75c04dc2 | 443 | ticker.attach(&moveDownTicker, time); |
el14ja | 1:2b9ba34131ca | 444 | } |
el14ja | 0:ce5c75c04dc2 | 445 | |
el14ja | 0:ce5c75c04dc2 | 446 | void moveDownTicker() |
el14ja | 0:ce5c75c04dc2 | 447 | { |
el14ja | 0:ce5c75c04dc2 | 448 | g_timer_flag = 1; |
el14ja | 0:ce5c75c04dc2 | 449 | } |
el14ja | 0:ce5c75c04dc2 | 450 | void left_isr() |
el14ja | 0:ce5c75c04dc2 | 451 | { |
el14ja | 1:2b9ba34131ca | 452 | if(g_lefttimeout) { |
el14ja | 1:2b9ba34131ca | 453 | g_left = 1; |
el14ja | 1:2b9ba34131ca | 454 | } |
el14ja | 0:ce5c75c04dc2 | 455 | } |
el14ja | 0:ce5c75c04dc2 | 456 | void right_isr() |
el14ja | 0:ce5c75c04dc2 | 457 | { |
el14ja | 1:2b9ba34131ca | 458 | if(g_righttimeout) { |
el14ja | 1:2b9ba34131ca | 459 | g_right = 1; |
el14ja | 1:2b9ba34131ca | 460 | } |
el14ja | 0:ce5c75c04dc2 | 461 | } |
el14ja | 0:ce5c75c04dc2 | 462 | void spin_isr() |
el14ja | 0:ce5c75c04dc2 | 463 | { |
el14ja | 1:2b9ba34131ca | 464 | if(g_spintimeout) { |
el14ja | 1:2b9ba34131ca | 465 | g_spin = 1; |
el14ja | 1:2b9ba34131ca | 466 | } |
el14ja | 1:2b9ba34131ca | 467 | } |
el14ja | 1:2b9ba34131ca | 468 | |
el14ja | 1:2b9ba34131ca | 469 | void down_isr() |
el14ja | 1:2b9ba34131ca | 470 | { |
el14ja | 1:2b9ba34131ca | 471 | if(g_downtimeout) { |
el14ja | 1:2b9ba34131ca | 472 | g_down = 1; |
el14ja | 1:2b9ba34131ca | 473 | } |
el14ja | 1:2b9ba34131ca | 474 | } |
el14ja | 1:2b9ba34131ca | 475 | |
el14ja | 1:2b9ba34131ca | 476 | void music_isr() |
el14ja | 1:2b9ba34131ca | 477 | { |
el14ja | 1:2b9ba34131ca | 478 | g_music = 1; |
el14ja | 0:ce5c75c04dc2 | 479 | } |
el14ja | 0:ce5c75c04dc2 | 480 | |
el14ja | 1:2b9ba34131ca | 481 | void lefttimeout_isr() |
el14ja | 1:2b9ba34131ca | 482 | { |
el14ja | 1:2b9ba34131ca | 483 | g_lefttimeout = 1; |
el14ja | 1:2b9ba34131ca | 484 | } |
el14ja | 1:2b9ba34131ca | 485 | void righttimeout_isr() |
el14ja | 1:2b9ba34131ca | 486 | { |
el14ja | 1:2b9ba34131ca | 487 | g_righttimeout = 1; |
el14ja | 1:2b9ba34131ca | 488 | } |
el14ja | 1:2b9ba34131ca | 489 | void spintimeout_isr() |
el14ja | 1:2b9ba34131ca | 490 | { |
el14ja | 1:2b9ba34131ca | 491 | g_spintimeout = 1; |
el14ja | 1:2b9ba34131ca | 492 | } |
el14ja | 1:2b9ba34131ca | 493 | void downtimeout_isr() |
el14ja | 1:2b9ba34131ca | 494 | { |
el14ja | 1:2b9ba34131ca | 495 | g_downtimeout = 1; |
el14ja | 1:2b9ba34131ca | 496 | } |
el14ja | 1:2b9ba34131ca | 497 | |
el14ja | 2:61caca4dd3f0 | 498 | /* |
el14ja | 0:ce5c75c04dc2 | 499 | void movePieceRight() |
el14ja | 0:ce5c75c04dc2 | 500 | { |
el14ja | 2:61caca4dd3f0 | 501 | if(movePossible(pieceposition[0]-1,pieceposition[1],orientation)) { |
el14ja | 2:61caca4dd3f0 | 502 | movePiece(1, 0,0); |
el14ja | 2:61caca4dd3f0 | 503 | } |
el14ja | 0:ce5c75c04dc2 | 504 | } |
el14ja | 0:ce5c75c04dc2 | 505 | void movePieceLeft() |
el14ja | 0:ce5c75c04dc2 | 506 | { |
el14ja | 2:61caca4dd3f0 | 507 | if(movePossible(pieceposition[0]+1,pieceposition[1],orientation)) { |
el14ja | 2:61caca4dd3f0 | 508 | movePiece(-1, 0,0); |
el14ja | 2:61caca4dd3f0 | 509 | } |
el14ja | 0:ce5c75c04dc2 | 510 | } |
el14ja | 0:ce5c75c04dc2 | 511 | void spinPiece() |
el14ja | 0:ce5c75c04dc2 | 512 | { |
el14ja | 2:61caca4dd3f0 | 513 | if(movePossible(pieceposition[0],pieceposition[1],orientation+1)) { |
el14ja | 2:61caca4dd3f0 | 514 | |
el14ja | 2:61caca4dd3f0 | 515 | movePiece(0, 0,1); |
el14ja | 0:ce5c75c04dc2 | 516 | } |
el14ja | 0:ce5c75c04dc2 | 517 | } |
el14ja | 0:ce5c75c04dc2 | 518 | |
el14ja | 0:ce5c75c04dc2 | 519 | void movePieceDown() |
el14ja | 0:ce5c75c04dc2 | 520 | { |
el14ja | 2:61caca4dd3f0 | 521 | movePiece(0, 1,0); |
el14ja | 0:ce5c75c04dc2 | 522 | } |
el14ja | 0:ce5c75c04dc2 | 523 | |
el14ja | 1:2b9ba34131ca | 524 | void movePiece(int x, int y,int spin){ |
el14ja | 1:2b9ba34131ca | 525 | pieceClear(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]); |
el14ja | 1:2b9ba34131ca | 526 | pieceposition[0]= pieceposition[0] - x; |
el14ja | 1:2b9ba34131ca | 527 | pieceposition[1]= pieceposition[1] + y; |
el14ja | 1:2b9ba34131ca | 528 | orientation = orientation + spin; |
el14ja | 1:2b9ba34131ca | 529 | if (orientation > 3){ |
el14ja | 1:2b9ba34131ca | 530 | orientation = 0; |
el14ja | 1:2b9ba34131ca | 531 | } |
el14ja | 1:2b9ba34131ca | 532 | piecePlace(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]); |
el14ja | 1:2b9ba34131ca | 533 | } |
el14ja | 1:2b9ba34131ca | 534 | |
el14ja | 2:61caca4dd3f0 | 535 | */ |
el14ja | 1:2b9ba34131ca | 536 |