| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void keyTyped(KeyEvent e) { |
| 235 | if (e.getKeyChar() == 'a') {// 方格进行左移 |
| 236 | if (running == false) { |
| 237 | return; |
| 238 | } |
| 239 | if (y <= 1)//碰到左边墙壁时 |
| 240 | return; |
| 241 | int temp = 0x8000;//表示1000 0000 0000 0000 |
| 242 | for (int i = x; i < x + 4; i++) {//循环遍历16个方格(4*4) |
| 243 | for (int j = y; j < y + 4; j++) { |
| 244 | if ((rect & temp) != 0) {// 此处有方块时 |
| 245 | if (data[i][j - 1] == 1) {//如果左移一格有方块时 |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | temp >>= 1; |
| 250 | } |
| 251 | } |
| 252 | clear(x, y);//可以进行左移操作时,清除左移前方块颜色 |
| 253 | y--; |
| 254 | draw(x, y);//然后重新绘制左移后方块的图像 |
| 255 | } |
| 256 | if (e.getKeyChar() == 'd') {//方块进行右移操作 |
| 257 | if (running == false) { |
| 258 | return; |
| 259 | } |
| 260 | int temp = 0x8000; |
| 261 | int m = x, n = y; |
| 262 | int num = 7; |
| 263 | for (int i = 0; i < 4; i++) { |
| 264 | for (int j = 0; j < 4; j++) { |
| 265 | if ((temp & rect) != 0) { |
| 266 | if (n > num) { |
| 267 | num = n; |
| 268 | } |
| 269 | } |
| 270 | temp >>= 1; |
| 271 | n++; |
| 272 | } |
| 273 | m++; |
| 274 | n = n - 4; |
| 275 | } |
| 276 | if (num >= 10) { |
| 277 | return; |
| 278 | } |
| 279 | temp = 0x8000; |
| 280 | for (int i = x; i < x + 4; i++) { |
| 281 | for (int j = y; j < y + 4; j++) { |
| 282 | if ((rect & temp) != 0) { |
| 283 | if (data[i][j + 1] == 1) { |
| 284 | return; |
| 285 | } |
| 286 | } |
| 287 | temp >>= 1; |
| 288 | } |
| 289 | } |
| 290 | clear(x, y);//可以进行右移操作时,清除右移前方块颜色 |