Su 200943147

Dependencies:   Gamepad N5110 mbed

Committer:
GS00
Date:
Thu May 04 14:20:35 2017 +0000
Revision:
9:6ee4c806f3e9
Parent:
8:3899d883d329
Final and Upload version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GS00 0:444b4d0a113d 1 #include "Shape.h"
GS00 0:444b4d0a113d 2
GS00 3:9dd35424cdfe 3 Shape::Shape()
GS00 0:444b4d0a113d 4 {
GS00 0:444b4d0a113d 5 }
GS00 0:444b4d0a113d 6
GS00 8:3899d883d329 7 //Create first shape block
GS00 3:9dd35424cdfe 8 void Shape::Init()
GS00 0:444b4d0a113d 9 {
GS00 8:3899d883d329 10 New=0; //initial the Flag
GS00 8:3899d883d329 11 ShapePicker(); //Generate a shape from the list
GS00 0:444b4d0a113d 12 }
GS00 0:444b4d0a113d 13
GS00 8:3899d883d329 14 /** Rotate
GS00 8:3899d883d329 15 *
GS00 8:3899d883d329 16 * Rotate the shape by 90 degree clockwise.
GS00 8:3899d883d329 17 * Insert the shape array into array only for rotating
GS00 8:3899d883d329 18 * Rotate the shape within rotating array and transfer back to shape array
GS00 8:3899d883d329 19 */
GS00 6:e919a1fd1eed 20 void Shape::Rotate(Gamepad &pad)
GS00 6:e919a1fd1eed 21 {
GS00 6:e919a1fd1eed 22 for(int i=1; i<84; i++) {
GS00 6:e919a1fd1eed 23 for(int j=1; j<48; j++) {
GS00 8:3899d883d329 24 ArrayRotate[i-1][j-1]=ShapeInit[i][j]; //Transfer the Shape from original array into a new array from rotation
GS00 6:e919a1fd1eed 25 }
GS00 6:e919a1fd1eed 26 }
GS00 8:3899d883d329 27 int Temp; //temporarily variable
GS00 6:e919a1fd1eed 28 for (int i=0; i<2; i++) {
GS00 6:e919a1fd1eed 29 for (int j=i; j<3-i; j++) {
GS00 8:3899d883d329 30 Temp=ArrayRotate[i][j]; //top to temporarily variable
GS00 8:3899d883d329 31 ArrayRotate[i][j]=ArrayRotate[j][3-i]; //bottom to top
GS00 8:3899d883d329 32 ArrayRotate[j][3-i]=ArrayRotate[3-i][3-j]; //right to left
GS00 8:3899d883d329 33 ArrayRotate[3-i][3-j]=ArrayRotate[3-j][i]; //top to bottom
GS00 8:3899d883d329 34 ArrayRotate[3-j][i]=Temp; //temporarily variable to top
GS00 6:e919a1fd1eed 35 }
GS00 8:3899d883d329 36 }//reference http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array
GS00 6:e919a1fd1eed 37 for(int i=1; i<5; i++) {
GS00 6:e919a1fd1eed 38 for(int j=1; j<5; j++) {
GS00 8:3899d883d329 39 printf("%d\n",ArrayRotate[i][j]); //print the array
GS00 6:e919a1fd1eed 40 }
GS00 9:6ee4c806f3e9 41 }//reference http://stackoverflow.com/questions/8442729/is-there-a-way-to-have-printf-properly-print-out-an-array-of-floats-say
GS00 9:6ee4c806f3e9 42 // printf("x=%d\n",x); //print value of x for debug
GS00 9:6ee4c806f3e9 43 // printf("y=%d\n",y); //print value of y for debug
GS00 8:3899d883d329 44 if(pad.check_event(Gamepad::A_PRESSED) == true) { //event check
GS00 8:3899d883d329 45 for(int i=0; i<84; i++) {
GS00 6:e919a1fd1eed 46 for(int j=0; j<48; j++) {
GS00 8:3899d883d329 47 ShapeInit[i][j]=0; //clear the array
GS00 7:31dd8865cc44 48 ShapeArray[i][j]=0;
GS00 6:e919a1fd1eed 49 }
GS00 8:3899d883d329 50 }
GS00 9:6ee4c806f3e9 51 // printf("x1=%d\n",x); //print value of x for debug
GS00 9:6ee4c806f3e9 52 // printf("y1=%d\n",y); //print value of y for debug
GS00 8:3899d883d329 53 for(int i=0; i<83; i++) {
GS00 8:3899d883d329 54 for(int j=0; j<47; j++) {
GS00 8:3899d883d329 55 ShapeInit[i+1][j+1]=ArrayRotate[i][j]; //transfer the rotated array back to orignail array
GS00 8:3899d883d329 56 }
GS00 6:e919a1fd1eed 57 }
GS00 9:6ee4c806f3e9 58 // printf("x2=%d\n",x); //print value of x for debug
GS00 9:6ee4c806f3e9 59 // printf("y2=%d\n",y); //print value of y for debug
GS00 6:e919a1fd1eed 60 }
GS00 6:e919a1fd1eed 61 }
GS00 3:9dd35424cdfe 62
GS00 8:3899d883d329 63 //update the shape array into moving array
GS00 6:e919a1fd1eed 64 void Shape::Update(Gamepad &pad)
GS00 0:444b4d0a113d 65 {
GS00 3:9dd35424cdfe 66 int n=14;
GS00 3:9dd35424cdfe 67 ShapeArray[x+n][y]=0;
GS00 3:9dd35424cdfe 68 ShapeArray[x+1+n][y]=0;
GS00 3:9dd35424cdfe 69 ShapeArray[x+2+n][y]=0;
GS00 3:9dd35424cdfe 70 ShapeArray[x+3+n][y]=0;
GS00 3:9dd35424cdfe 71 ShapeArray[x+4+n][y]=0;
GS00 3:9dd35424cdfe 72 ShapeArray[x+5+n][y]=0;
GS00 3:9dd35424cdfe 73
GS00 3:9dd35424cdfe 74 ShapeArray[x+n][y+1]=0;
GS00 5:afa6592a4ba5 75 ShapeArray[x+5+n][y+1]=0;
GS00 5:afa6592a4ba5 76
GS00 5:afa6592a4ba5 77 ShapeArray[x+n][y+2]=0;
GS00 5:afa6592a4ba5 78 ShapeArray[x+5+n][y+2]=0;
GS00 5:afa6592a4ba5 79
GS00 5:afa6592a4ba5 80 ShapeArray[x+n][y+3]=0;
GS00 5:afa6592a4ba5 81 ShapeArray[x+5+n][y+3]=0;
GS00 5:afa6592a4ba5 82
GS00 5:afa6592a4ba5 83 ShapeArray[x+n][y+4]=0;
GS00 5:afa6592a4ba5 84 ShapeArray[x+5+n][y+4]=0;
GS00 5:afa6592a4ba5 85
GS00 3:9dd35424cdfe 86 ShapeArray[x+1+n][y+1]=ShapeInit[l+1][k+1];
GS00 3:9dd35424cdfe 87 ShapeArray[x+2+n][y+1]=ShapeInit[l+2][k+1];
GS00 3:9dd35424cdfe 88 ShapeArray[x+3+n][y+1]=ShapeInit[l+3][k+1];
GS00 6:e919a1fd1eed 89 ShapeArray[x+4+n][y+1]=ShapeInit[l+4][k+1];
GS00 3:9dd35424cdfe 90
GS00 3:9dd35424cdfe 91 ShapeArray[x+1+n][y+2]=ShapeInit[l+1][k+2];
GS00 3:9dd35424cdfe 92 ShapeArray[x+2+n][y+2]=ShapeInit[l+2][k+2];
GS00 3:9dd35424cdfe 93 ShapeArray[x+3+n][y+2]=ShapeInit[l+3][k+2];
GS00 3:9dd35424cdfe 94 ShapeArray[x+4+n][y+2]=ShapeInit[l+4][k+2];
GS00 3:9dd35424cdfe 95
GS00 3:9dd35424cdfe 96 ShapeArray[x+1+n][y+3]=ShapeInit[l+1][k+3];
GS00 3:9dd35424cdfe 97 ShapeArray[x+2+n][y+3]=ShapeInit[l+2][k+3];
GS00 3:9dd35424cdfe 98 ShapeArray[x+3+n][y+3]=ShapeInit[l+3][k+3];
GS00 3:9dd35424cdfe 99 ShapeArray[x+4+n][y+3]=ShapeInit[l+4][k+3];
GS00 3:9dd35424cdfe 100
GS00 3:9dd35424cdfe 101 ShapeArray[x+1+n][y+4]=ShapeInit[l+1][k+4];
GS00 3:9dd35424cdfe 102 ShapeArray[x+2+n][y+4]=ShapeInit[l+2][k+4];
GS00 3:9dd35424cdfe 103 ShapeArray[x+3+n][y+4]=ShapeInit[l+3][k+4];
GS00 3:9dd35424cdfe 104 ShapeArray[x+4+n][y+4]=ShapeInit[l+4][k+4];
GS00 5:afa6592a4ba5 105
GS00 0:444b4d0a113d 106 }
GS00 0:444b4d0a113d 107
GS00 8:3899d883d329 108 /** ShapePicker
GS00 8:3899d883d329 109 *
GS00 8:3899d883d329 110 * Generate a random number from 0-6 and map one number to one shape.
GS00 8:3899d883d329 111 * Use if elseif function to identify which number is generated and present the shape to screen.
GS00 8:3899d883d329 112 * The Function only run when flag is activated.
GS00 8:3899d883d329 113 */
GS00 6:e919a1fd1eed 114 void Shape::ShapePicker()
GS00 6:e919a1fd1eed 115 {
GS00 6:e919a1fd1eed 116 t=time(NULL);
GS00 8:3899d883d329 117 srand(t); //create a seed
GS00 6:e919a1fd1eed 118
GS00 6:e919a1fd1eed 119 if(New==0) {
GS00 8:3899d883d329 120 New=1; //clear flag
GS00 8:3899d883d329 121 RandomNumber=rand()%7; //generated a randdom number
GS00 6:e919a1fd1eed 122 }
GS00 6:e919a1fd1eed 123
GS00 8:3899d883d329 124 if(RandomNumber==0) { //identify the shape
GS00 8:3899d883d329 125 T_Shape(); // T shape
GS00 6:e919a1fd1eed 126 RandomNumber=10;
GS00 6:e919a1fd1eed 127 } else if(RandomNumber==1) {
GS00 8:3899d883d329 128 J_Shape(); // J shape
GS00 6:e919a1fd1eed 129 RandomNumber=10;
GS00 6:e919a1fd1eed 130 } else if(RandomNumber==2) {
GS00 8:3899d883d329 131 L_Shape(); // L shape
GS00 6:e919a1fd1eed 132 RandomNumber=10;
GS00 6:e919a1fd1eed 133 } else if(RandomNumber==3) {
GS00 8:3899d883d329 134 O_Shape(); // o shape
GS00 6:e919a1fd1eed 135 RandomNumber=10;
GS00 6:e919a1fd1eed 136 } else if(RandomNumber==4) {
GS00 8:3899d883d329 137 I_Shape(); // I shape
GS00 6:e919a1fd1eed 138 RandomNumber=10;
GS00 6:e919a1fd1eed 139 } else if(RandomNumber==5) {
GS00 8:3899d883d329 140 S_Shape(); // S shape
GS00 6:e919a1fd1eed 141 RandomNumber=10;
GS00 6:e919a1fd1eed 142 } else if(RandomNumber==6) {
GS00 8:3899d883d329 143 Z_Shape(); // Z shape
GS00 6:e919a1fd1eed 144 RandomNumber=10;
GS00 6:e919a1fd1eed 145 }
GS00 6:e919a1fd1eed 146 }
GS00 0:444b4d0a113d 147
GS00 8:3899d883d329 148 //Draw the L shape into the array
GS00 8:3899d883d329 149 void Shape::L_Shape()
GS00 8:3899d883d329 150 {
GS00 8:3899d883d329 151 ShapeInit[l+1][k+1]=0;
GS00 8:3899d883d329 152 ShapeInit[l+2][k+1]=1;
GS00 8:3899d883d329 153 ShapeInit[l+3][k+1]=0;
GS00 8:3899d883d329 154 ShapeInit[l+4][k+1]=0;
GS00 3:9dd35424cdfe 155
GS00 8:3899d883d329 156 ShapeInit[l+1][k+2]=0;
GS00 8:3899d883d329 157 ShapeInit[l+2][k+2]=1;
GS00 8:3899d883d329 158 ShapeInit[l+3][k+2]=0;
GS00 8:3899d883d329 159 ShapeInit[l+4][k+2]=0;
GS00 8:3899d883d329 160
GS00 8:3899d883d329 161 ShapeInit[l+1][k+3]=0;
GS00 8:3899d883d329 162 ShapeInit[l+2][k+3]=1;
GS00 8:3899d883d329 163 ShapeInit[l+3][k+3]=1;
GS00 8:3899d883d329 164 ShapeInit[l+4][k+3]=0;
GS00 8:3899d883d329 165
GS00 8:3899d883d329 166 ShapeInit[l+1][k+4]=0;
GS00 8:3899d883d329 167 ShapeInit[l+2][k+4]=0;
GS00 8:3899d883d329 168 ShapeInit[l+3][k+4]=0;
GS00 8:3899d883d329 169 ShapeInit[l+4][k+4]=0;
GS00 8:3899d883d329 170 }
GS00 8:3899d883d329 171
GS00 8:3899d883d329 172 //Draw the O shape into the array
GS00 8:3899d883d329 173 void Shape::O_Shape()
GS00 8:3899d883d329 174 {
GS00 8:3899d883d329 175 ShapeInit[l+1][k+1]=0;
GS00 8:3899d883d329 176 ShapeInit[l+2][k+1]=0;
GS00 8:3899d883d329 177 ShapeInit[l+3][k+1]=0;
GS00 8:3899d883d329 178 ShapeInit[l+4][k+1]=0;
GS00 8:3899d883d329 179
GS00 8:3899d883d329 180 ShapeInit[l+1][k+2]=0;
GS00 8:3899d883d329 181 ShapeInit[l+2][k+2]=1;
GS00 8:3899d883d329 182 ShapeInit[l+3][k+2]=1;
GS00 8:3899d883d329 183 ShapeInit[l+4][k+2]=0;
GS00 3:9dd35424cdfe 184
GS00 8:3899d883d329 185 ShapeInit[l+1][k+3]=0;
GS00 8:3899d883d329 186 ShapeInit[l+2][k+3]=1;
GS00 8:3899d883d329 187 ShapeInit[l+3][k+3]=1;
GS00 8:3899d883d329 188 ShapeInit[l+4][k+3]=0;
GS00 3:9dd35424cdfe 189
GS00 8:3899d883d329 190 ShapeInit[l+1][k+4]=0;
GS00 8:3899d883d329 191 ShapeInit[l+2][k+4]=0;
GS00 8:3899d883d329 192 ShapeInit[l+3][k+4]=0;
GS00 8:3899d883d329 193 ShapeInit[l+4][k+4]=0;
GS00 8:3899d883d329 194 }
GS00 3:9dd35424cdfe 195
GS00 8:3899d883d329 196 //Draw the S shape into the array
GS00 8:3899d883d329 197 void Shape::S_Shape()
GS00 8:3899d883d329 198 {
GS00 8:3899d883d329 199 ShapeInit[l+1][k+1]=0;
GS00 8:3899d883d329 200 ShapeInit[l+2][k+1]=0;
GS00 8:3899d883d329 201 ShapeInit[l+3][k+1]=0;
GS00 8:3899d883d329 202 ShapeInit[l+4][k+1]=0;
GS00 8:3899d883d329 203
GS00 8:3899d883d329 204 ShapeInit[l+1][k+2]=0;
GS00 8:3899d883d329 205 ShapeInit[l+2][k+2]=1;
GS00 8:3899d883d329 206 ShapeInit[l+3][k+2]=1;
GS00 8:3899d883d329 207 ShapeInit[l+4][k+2]=0;
GS00 8:3899d883d329 208
GS00 8:3899d883d329 209 ShapeInit[l+1][k+3]=1;
GS00 8:3899d883d329 210 ShapeInit[l+2][k+3]=1;
GS00 8:3899d883d329 211 ShapeInit[l+3][k+3]=0;
GS00 8:3899d883d329 212 ShapeInit[l+4][k+3]=0;
GS00 8:3899d883d329 213
GS00 8:3899d883d329 214 ShapeInit[l+1][k+4]=0;
GS00 8:3899d883d329 215 ShapeInit[l+2][k+4]=0;
GS00 8:3899d883d329 216 ShapeInit[l+3][k+4]=0;
GS00 8:3899d883d329 217 ShapeInit[l+4][k+4]=0;
GS00 8:3899d883d329 218 }
GS00 8:3899d883d329 219
GS00 8:3899d883d329 220 //Draw the Z shape into the array
GS00 8:3899d883d329 221 void Shape::Z_Shape()
GS00 8:3899d883d329 222 {
GS00 8:3899d883d329 223 ShapeInit[l+1][k+1]=0;
GS00 8:3899d883d329 224 ShapeInit[l+2][k+1]=0;
GS00 8:3899d883d329 225 ShapeInit[l+3][k+1]=0;
GS00 8:3899d883d329 226 ShapeInit[l+4][k+1]=0;
GS00 3:9dd35424cdfe 227
GS00 8:3899d883d329 228 ShapeInit[l+1][k+2]=1;
GS00 8:3899d883d329 229 ShapeInit[l+2][k+2]=1;
GS00 8:3899d883d329 230 ShapeInit[l+3][k+2]=0;
GS00 8:3899d883d329 231 ShapeInit[l+4][k+2]=0;
GS00 3:9dd35424cdfe 232
GS00 8:3899d883d329 233 ShapeInit[l+1][k+3]=0;
GS00 8:3899d883d329 234 ShapeInit[l+2][k+3]=1;
GS00 8:3899d883d329 235 ShapeInit[l+3][k+3]=1;
GS00 8:3899d883d329 236 ShapeInit[l+4][k+3]=0;
GS00 8:3899d883d329 237
GS00 8:3899d883d329 238 ShapeInit[l+1][k+4]=0;
GS00 8:3899d883d329 239 ShapeInit[l+2][k+4]=0;
GS00 8:3899d883d329 240 ShapeInit[l+3][k+4]=0;
GS00 8:3899d883d329 241 ShapeInit[l+4][k+4]=0;
GS00 8:3899d883d329 242 }
GS00 8:3899d883d329 243
GS00 8:3899d883d329 244 //Draw the I shape into the array
GS00 8:3899d883d329 245 void Shape::I_Shape()
GS00 8:3899d883d329 246 {
GS00 8:3899d883d329 247 ShapeInit[l+1][k+1]=0;
GS00 8:3899d883d329 248 ShapeInit[l+2][k+1]=1;
GS00 8:3899d883d329 249 ShapeInit[l+3][k+1]=0;
GS00 8:3899d883d329 250 ShapeInit[l+4][k+1]=0;
GS00 8:3899d883d329 251
GS00 8:3899d883d329 252 ShapeInit[l+1][k+2]=0;
GS00 8:3899d883d329 253 ShapeInit[l+2][k+2]=1;
GS00 8:3899d883d329 254 ShapeInit[l+3][k+2]=0;
GS00 8:3899d883d329 255 ShapeInit[l+4][k+2]=0;
GS00 8:3899d883d329 256
GS00 8:3899d883d329 257 ShapeInit[l+1][k+3]=0;
GS00 8:3899d883d329 258 ShapeInit[l+2][k+3]=1;
GS00 8:3899d883d329 259 ShapeInit[l+3][k+3]=0;
GS00 8:3899d883d329 260 ShapeInit[l+4][k+3]=0;
GS00 3:9dd35424cdfe 261
GS00 8:3899d883d329 262 ShapeInit[l+1][k+4]=0;
GS00 8:3899d883d329 263 ShapeInit[l+2][k+4]=1;
GS00 8:3899d883d329 264 ShapeInit[l+3][k+4]=0;
GS00 8:3899d883d329 265 ShapeInit[l+4][k+4]=0;
GS00 8:3899d883d329 266 }
GS00 8:3899d883d329 267 //Draw the J shape into the array
GS00 8:3899d883d329 268 void Shape::J_Shape()
GS00 8:3899d883d329 269 {
GS00 8:3899d883d329 270 ShapeInit[l+1][k+1]=0;
GS00 8:3899d883d329 271 ShapeInit[l+2][k+1]=0;
GS00 8:3899d883d329 272 ShapeInit[l+3][k+1]=1;
GS00 8:3899d883d329 273 ShapeInit[l+4][k+1]=0;
GS00 6:e919a1fd1eed 274
GS00 8:3899d883d329 275 ShapeInit[l+1][k+2]=0;
GS00 8:3899d883d329 276 ShapeInit[l+2][k+2]=0;
GS00 8:3899d883d329 277 ShapeInit[l+3][k+2]=1;
GS00 8:3899d883d329 278 ShapeInit[l+4][k+2]=0;
GS00 6:e919a1fd1eed 279
GS00 8:3899d883d329 280 ShapeInit[l+1][k+3]=0;
GS00 8:3899d883d329 281 ShapeInit[l+2][k+3]=1;
GS00 8:3899d883d329 282 ShapeInit[l+3][k+3]=1;
GS00 8:3899d883d329 283 ShapeInit[l+4][k+3]=0;
GS00 8:3899d883d329 284
GS00 8:3899d883d329 285 ShapeInit[l+1][k+4]=0;
GS00 8:3899d883d329 286 ShapeInit[l+2][k+4]=0;
GS00 8:3899d883d329 287 ShapeInit[l+3][k+4]=0;
GS00 8:3899d883d329 288 ShapeInit[l+4][k+4]=0;
GS00 8:3899d883d329 289 }
GS00 8:3899d883d329 290 //Draw the T shape into the array
GS00 8:3899d883d329 291 void Shape::T_Shape()
GS00 8:3899d883d329 292 {
GS00 8:3899d883d329 293 ShapeInit[l+1][k+1]=0;
GS00 8:3899d883d329 294 ShapeInit[l+2][k+1]=0;
GS00 8:3899d883d329 295 ShapeInit[l+3][k+1]=0;
GS00 8:3899d883d329 296 ShapeInit[l+4][k+1]=0;
GS00 8:3899d883d329 297
GS00 8:3899d883d329 298 ShapeInit[l+1][k+2]=1;
GS00 8:3899d883d329 299 ShapeInit[l+2][k+2]=1;
GS00 8:3899d883d329 300 ShapeInit[l+3][k+2]=1;
GS00 8:3899d883d329 301 ShapeInit[l+4][k+2]=0;
GS00 8:3899d883d329 302
GS00 8:3899d883d329 303 ShapeInit[l+1][k+3]=0;
GS00 8:3899d883d329 304 ShapeInit[l+2][k+3]=1;
GS00 8:3899d883d329 305 ShapeInit[l+3][k+3]=0;
GS00 8:3899d883d329 306 ShapeInit[l+4][k+3]=0;
GS00 8:3899d883d329 307
GS00 8:3899d883d329 308 ShapeInit[l+1][k+4]=0;
GS00 8:3899d883d329 309 ShapeInit[l+2][k+4]=0;
GS00 8:3899d883d329 310 ShapeInit[l+3][k+4]=0;
GS00 8:3899d883d329 311 ShapeInit[l+4][k+4]=0;
GS00 8:3899d883d329 312 }