Código de Java - Juego del Tetris

Versión 1
estrellaestrellaestrellaestrellaestrella(18)

Publicado el 26 de Junio del 2013gráfica de visualizaciones de la versión: Versión 1
125.887 visualizaciones desde el 26 de Junio del 2013
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella


Forma parte de PasteBin
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import java.awt.*;
import java.awt.event.*;
 
import javax.swing.*;
 
public class Tetris extends JFrame implements KeyListener {
    int pos[] = {0,1};
    boolean bottom = false;
    int n = 20;
    int m = 10;
    JButton b[][];
    Color tmp[][] = new Color[m][n];
    int rand = 0;
    int centralx = 0;
    int centraly = 0;
    int deltax = 0;
    int perim[][] = new int[m+4][n+4];
    or[][][] prof = new or[4][4][7];
    Color rnd[] = {Color.red, Color.yellow, Color.cyan, Color.green, Color.white, Color.blue, Color.orange};
    int rowsclrd = 0;
    public Tetris(){
        for(int a = 0;a<4;a++){
            for(int b = 0;b<4;b++){
                for(int c = 0;c<7;c++){
                    prof[a][b][c] = new or();
                }
            }
        }
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        prof[0][0][0].x = -1;
        prof[0][1][0].x = 0;
        prof[0][2][0].x = 0;
        prof[0][3][0].x = 1;
        prof[1][0][0].x = 0;
        prof[1][1][0].x = 0;
        prof[1][2][0].x = -1;
        prof[1][3][0].x = -1;
        prof[2][0][0].x = 1;
        prof[2][1][0].x = 0;
        prof[2][2][0].x = 0;
        prof[2][3][0].x = -1;
        prof[3][0][0].x = 0;
        prof[3][1][0].x = 0;
        prof[3][2][0].x = 1;
        prof[3][3][0].x = 1;
        prof[0][0][1].x = -1;
        prof[0][1][1].x = -1;
        prof[0][2][1].x = 0;
        prof[0][3][1].x = 1;
        prof[1][0][1].x = -1;
        prof[1][1][1].x = 0;
        prof[1][2][1].x = 0;
        prof[1][3][1].x = 0;
        prof[2][0][1].x = 1;
        prof[2][1][1].x = 1;
        prof[2][2][1].x = 0;
        prof[2][3][1].x = -1;
        prof[3][0][1].x = 1;
        prof[3][1][1].x = 0;
        prof[3][2][1].x = 0;
        prof[3][3][1].x = 0;
        prof[0][0][2].x = -1;
        prof[0][1][2].x = 0;
        prof[0][2][2].x = 1;
        prof[0][3][2].x = 1;
        prof[1][0][2].x = 0;
        prof[1][1][2].x = 0;
        prof[1][2][2].x = 0;
        prof[1][3][2].x = -1;
        prof[2][0][2].x = 1;
        prof[2][1][2].x = 0;
        prof[2][2][2].x = -1;
        prof[2][3][2].x = -1;
        prof[3][0][2].x = 0;
        prof[3][1][2].x = 0;
        prof[3][2][2].x = 0;
        prof[3][3][2].x = 1;
        prof[0][0][3].x = -1;
        prof[0][1][3].x = 0;
        prof[0][2][3].x = 0;
        prof[0][3][3].x = 1;
        prof[1][0][3].x = -1;
        prof[1][1][3].x = -1;
        prof[1][2][3].x = 0;
        prof[1][3][3].x = 0;
        prof[2][0][3].x = 1;
        prof[2][1][3].x = 0;
        prof[2][2][3].x = 0;
        prof[2][3][3].x = -1;
        prof[3][0][3].x = 1;
        prof[3][1][3].x = 1;
        prof[3][2][3].x = 0;
        prof[3][3][3].x = 0;
        prof[0][0][4].x = -1;
        prof[0][1][4].x = 0;
        prof[0][2][4].x = 0;
        prof[0][3][4].x = 1;
        prof[1][0][4].x = 0;
        prof[1][1][4].x = 0;
        prof[1][2][4].x = -1;
        prof[1][3][4].x = 0;
        prof[2][0][4].x = 1;
        prof[2][1][4].x = 0;
        prof[2][2][4].x = 0;
        prof[2][3][4].x = -1;
        prof[3][0][4].x = 0;
        prof[3][1][4].x = 0;
        prof[3][2][4].x = 1;
        prof[3][3][4].x = 0;
        prof[0][0][5].x = 0;
        prof[0][1][5].x = 0;
        prof[0][2][5].x = 1;
        prof[0][3][5].x = 1;
        prof[1][0][5].x = 0;
        prof[1][1][5].x = 0;
        prof[1][2][5].x = 1;
        prof[1][3][5].x = 1;
        prof[2][0][5].x = 0;
        prof[2][1][5].x = 0;
        prof[2][2][5].x = 1;
        prof[2][3][5].x = 1;
        prof[3][0][5].x = 0;
        prof[3][1][5].x = 0;
        prof[3][2][5].x = 1;
        prof[3][3][5].x = 1;
        prof[0][0][6].x = -1;
        prof[0][1][6].x = 0;
        prof[0][2][6].x = 1;
        prof[0][3][6].x = 2;
        prof[1][0][6].x = 0;
        prof[1][1][6].x = 0;
        prof[1][2][6].x = 0;
        prof[1][3][6].x = 0;
        prof[2][0][6].x = 1;
        prof[2][1][6].x = 0;
        prof[2][2][6].x = -1;
        prof[2][3][6].x = -2;
        prof[3][0][6].x = 0;
        prof[3][1][6].x = 0;
        prof[3][2][6].x = 0;
        prof[3][3][6].x = 0;
        prof[0][0][0].y = 0;
        prof[0][1][0].y = 0;
        prof[0][2][0].y = 1;
        prof[0][3][0].y = 1;
        prof[1][0][0].y = -1;
        prof[1][1][0].y = 0;
        prof[1][2][0].y = 0;
        prof[1][3][0].y = 1;
        prof[2][0][0].y = 0;
        prof[2][1][0].y = 0;
        prof[2][2][0].y = -1;
        prof[2][3][0].y = -1;
        prof[3][0][0].y = 1;
        prof[3][1][0].y = 0;
        prof[3][2][0].y = 0;
        prof[3][3][0].y = -1;
        prof[0][0][1].y = 0;
        prof[0][1][1].y = 1;
        prof[0][2][1].y = 0;
        prof[0][3][1].y = 0;
        prof[1][0][1].y = -1;
        prof[1][1][1].y = -1;
        prof[1][2][1].y = 0;
        prof[1][3][1].y = 1;
        prof[2][0][1].y = -1;
        prof[2][1][1].y = 0;
        prof[2][2][1].y = 0;
        prof[2][3][1].y = 0;
        prof[3][0][1].y = 1;
        prof[3][1][1].y = 1;
        prof[3][2][1].y = 0;
        prof[3][3][1].y = -1;
        prof[0][0][2].y = 0;
        prof[0][1][2].y = 0;
        prof[0][2][2].y = 0;
        prof[0][3][2].y = 1;
        prof[1][0][2].y = -1;
        prof[1][1][2].y = 0;
        prof[1][2][2].y = 1;
        prof[1][3][2].y = 1;
        prof[2][0][2].y = 0;
        prof[2][1][2].y = 0;
        prof[2][2][2].y = 0;
        prof[2][3][2].y = -1;
        prof[3][0][2].y = 1;
        prof[3][1][2].y = 0;
        prof[3][2][2].y = -1;
        prof[3][3][2].y = -1;
        prof[0][0][3].y = 1;
        prof[0][1][3].y = 1;
        prof[0][2][3].y = 0;
        prof[0][3][3].y = 0;
        prof[1][0][3].y = -1;
        prof[1][1][3].y = 0;
        prof[1][2][3].y = 0;
        prof[1][3][3].y = 1;
        prof[2][0][3].y = -1;
        prof[2][1][3].y = -1;
        prof[2][2][3].y = 0;
        prof[2][3][3].y = 0;
        prof[3][0][3].y = 1;
        prof[3][1][3].y = 0;
        prof[3][2][3].y = 0;
        prof[3][3][3].y = -1;
        prof[0][0][4].y = 0;
        prof[0][1][4].y = 0;
        prof[0][2][4].y = 1;
        prof[0][3][4].y = 0;
        prof[1][0][4].y = -1;
        prof[1][1][4].y = 0;
        prof[1][2][4].y = 0;
        prof[1][3][4].y = 1;
        prof[2][0][4].y = 0;
        prof[2][1][4].y = 0;
        prof[2][2][4].y = -1;
        prof[2][3][4].y = 0;
        prof[3][0][4].y = 1;
        prof[3][1][4].y = 0;
        prof[3][2][4].y = 0;
        prof[3][3][4].y = -1;
        prof[0][0][5].y = 0;
        prof[0][1][5].y = 1;
        prof[0][2][5].y = 0;
        prof[0][3][5].y = 1;
        prof[1][0][5].y = 0;
        prof[1][1][5].y = 1;
        prof[1][2][5].y = 0;
        prof[1][3][5].y = 1;
        prof[2][0][5].y = 0;
        prof[2][1][5].y = 1;
        prof[2][2][5].y = 0;
        prof[2][3][5].y = 1;
        prof[3][0][5].y = 0;
        prof[3][1][5].y = 1;
        prof[3][2][5].y = 0;
        prof[3][3][5].y = 1;
        prof[0][0][6].y = 0;
        prof[0][1][6].y = 0;
        prof[0][2][6].y = 0;
        prof[0][3][6].y = 0;
        prof[1][0][6].y = -1;
        prof[1][1][6].y = 0;
        prof[1][2][6].y = 1;
        prof[1][3][6].y = 2;
        prof[2][0][6].y = 0;
        prof[2][1][6].y = 0;
        prof[2][2][6].y = 0;
        prof[2][3][6].y = 0;
        prof[3][0][6].y = -1;
        prof[3][1][6].y = 0;
        prof[3][2][6].y = 1;
        prof[3][3][6].y = 2;
        for (int y = 0;y<2;y++){
            for (int x = 0;x<m+4;x++){
                perim[x][y]= 1;
            }
        }
        for (int y = n+2;y<n+4;y++){
            for (int x = 0;x<m+4;x++){
                perim[x][y]= 4;
            }
        }
        for (int y = 2;y<n+2;y++){
            for (int x = 0;x<2;x++){
                perim[x][y]= 2;
            }
        }
        for (int y = 2;y<n+2;y++){
            for (int x = m+2;x<m+4;x++){
                perim[x][y]= 2;
            }
        }
        for(int y = 0;y<n+4;y++){
            for (int x = 0;x<m+4;x++){
                System.out.print(perim[x][y]);
            }
            System.out.println("");
        }
        b = new JButton [m][n];
        setLayout(new GridLayout(n,m));//Grid layouts x and y are SWAPPED!
        for (int y = 0;y<n;y++){
            for (int x = 0;x<m;x++){
                b[x][y] = new JButton(" ");
                tmp[x][y] = Color.DARK_GRAY;
                b[x][y].setBackground(Color.DARK_GRAY);
                add(b[x][y]);
                b[x][y].setEnabled(true);
            }//end inner for
        }
        setFocusable(true);
        addKeyListener(this);
        pack();
        setVisible(true);
        blockgen();
 
    }//end constructor Mine()
 
    class or {
        int x;
        int y;
    }
 
    public void blockgen(){
        Component temporaryLostComponent = null;
        pos[0] = 0;
        pos[1] = 1;
        rand = (int) (Math.floor(Math.random()*7+1));
        centralx = 4;
        centraly = 0;
        System.out.print(rand);
        if ((b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].getBackground() == Color.DARK_GRAY) &&
        (b[4+prof[pos[0]][1][rand-1].x][prof[pos[0]][1][rand-1].y].getBackground() == Color.DARK_GRAY) &&
        (b[4+prof[pos[0]][2][rand-1].x][prof[pos[0]][2][rand-1].y].getBackground() == Color.DARK_GRAY) &&
        (b[4+prof[pos[0]][3][rand-1].x][prof[pos[0]][3][rand-1].y].getBackground() == Color.DARK_GRAY)){
            b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
            b[4+prof[pos[0]][1][rand-1].x][prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
            b[4+prof[pos[0]][2][rand-1].x][prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
            b[4+prof[pos[0]][3][rand-1].x][prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
            go();
        } else {
            JOptionPane.showMessageDialog(temporaryLostComponent, "Game Over! You cleared "+rowsclrd+" rows, well done!");
            System.exit(0);
        }
    }
 
    public void rotate(){
        if (pos[0] < 3){
            pos[1] = pos[0];
        pos[0]++;
        } else if (pos[0] == 3){
            pos[0] = 0;
            pos[1] = 3;
        } else {
            System.out.println("error");
        }
        if ((perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 4) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 1) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 2) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 3)
        && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 4) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 1) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 2) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 3)
        && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 4) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 1) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 2) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 3)
        && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 4) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 1) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 2) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 3)){
            b[centralx+prof[pos[1]][0][rand-1].x][centraly+prof[pos[1]][0][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[1]][1][rand-1].x][centraly+prof[pos[1]][1][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[1]][2][rand-1].x][centraly+prof[pos[1]][2][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[1]][3][rand-1].x][centraly+prof[pos[1]][3][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+(prof[pos[0]][0][rand-1].x)][centraly+(prof[pos[0]][0][rand-1].y)].setBackground(rnd[rand-1]);
            b[centralx+(prof[pos[0]][1][rand-1].x)][centraly+(prof[pos[0]][1][rand-1].y)].setBackground(rnd[rand-1]);
            b[centralx+(prof[pos[0]][2][rand-1].x)][centraly+(prof[pos[0]][2][rand-1].y)].setBackground(rnd[rand-1]);
            b[centralx+(prof[pos[0]][3][rand-1].x)][centraly+(prof[pos[0]][3][rand-1].y)].setBackground(rnd[rand-1]);
        } else {
            if (pos[1] > 0){
                pos[0] = pos[1];
                pos[1]--;
            } else if (pos[1] == 0){
                pos[0] = 0;
                pos[1] = 3;
            }
        }
    }
 
 
    public int getxs(){
        int xs = 0;
        int[] xf = {-1, -1, -1, -1};
        for (int d = 0;d<4;d++){
            if ((xf[0] != prof[pos[0]][d][rand-1].x) || (xf[1] != prof[pos[0]][d][rand-1].x) || (xf[2] != prof[pos[0]][d][rand-1].x) || (xf[3] != prof[pos[0]][d][rand-1].x)){
                xf[d] = prof[pos[0]][d][rand-1].x;
            }
        }
        for (int d = 0;d<4;d++){
            if (xf[d] != -1){
                xs++;
            }
        }
        return xs;
    }
 
 
    public void movedown(){
        int[] m2 = {-1, -1, -1, -1};
        int[] m1 = {-1, -1, -1, -1};
        int[] zero = {-1, -1, -1, -1};
        int[] one = {-1, -1, -1, -1};
        int[] two = {-1, -1, -1, -1};
        for (int d = 0;d<4;d++){
            if (prof[pos[0]][d][rand-1].x == -2){
                m2[d] = d;
            } else if (prof[pos[0]][d][rand-1].x == -1){
                m1[d] = d;
            } else if (prof[pos[0]][d][rand-1].x == 0){
                zero[d] = d;
            } else if (prof[pos[0]][d][rand-1].x == 1){
                one[d] = d;
            } else if (prof[pos[0]][d][rand-1].x == 2){
                two[d] = d;
            }
        }
        int tmpm2 = -5;
        int tmpm1 = -5;
        int tmpzero = -5;
        int tmpone = -5;
        int tmptwo = -5;
        for (int d = 0;d<4;d++){
            if (m2[d] != -1){
                if (tmpm2<prof[pos[0]][m2[d]][rand-1].y){
                    tmpm2 = prof[pos[0]][m2[d]][rand-1].y;
                }
            }
            if (m1[d] != -1){
                if (tmpm1<prof[pos[0]][m1[d]][rand-1].y){
                    tmpm1 = prof[pos[0]][m1[d]][rand-1].y;
                }
            }
            if (zero[d] != -1){
                if (tmpzero<prof[pos[0]][zero[d]][rand-1].y){
                    tmpzero = prof[pos[0]][zero[d]][rand-1].y;
                }
            }
            if (one[d] != -1){
                if (tmpone<prof[pos[0]][one[d]][rand-1].y){
                    tmpone = prof[pos[0]][one[d]][rand-1].y;
                }
            }
            if (two[d] != -1){
                if (tmptwo<prof[pos[0]][two[d]][rand-1].y){
                    tmptwo = prof[pos[0]][two[d]][rand-1].y;
                }
            }
        }
        int total = 0;
        for (int d = 0;d<4;d++){
            if (prof[pos[0]][d][rand-1].x == -2){
                if (perim[2+centralx+-2][2+centraly+tmpm2+1] != 4){
                    if(b[centralx+-2][centraly+tmpm2+1].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            } else if (prof[pos[0]][d][rand-1].x == -1){
                if (perim[2+centralx+-1][2+centraly+tmpm1+1] != 4){
                    if (b[centralx+-1][centraly+tmpm1+1].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            } else if (prof[pos[0]][d][rand-1].x == 0){
                if (perim[2+centralx][2+centraly+tmpzero+1] != 4){
                    if (b[centralx][centraly+tmpzero+1].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            } else if (prof[pos[0]][d][rand-1].x == 1){
                if (perim[2+centralx+1][2+centraly+tmpone+1] != 4){
                    if (b[centralx+1][centraly+tmpone+1].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            } else if (prof[pos[0]][d][rand-1].x == 2){
                if (perim[2+centralx+2][2+centraly+tmptwo+1] != 4){
                    if (b[centralx+2][centraly+tmptwo+1].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            }
        }
        if (total == 4){
            b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(Color.DARK_GRAY);
            centraly++;
            b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
            b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
            b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
            b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
        } else {
 
            bottom = true;
        }
    }
 
 
    public void go(){
        do{
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            movedown();
            rowcheck();
        }
        while(bottom == false);
        bottom = false;
        blockgen();
    }
 
    public void rowcheck(){
        int row = 0;
        for (int y = 0;y<20;y++){
            for (int x = 0;x<10;x++){
                if (b[x][y].getBackground() != Color.DARK_GRAY){
                    row++;
                }
                if (row == 10){
                    rowsclrd++;
                    rowclear(y);
                }
            }
            row = 0;
        }
    }
 
    public void rowclear(int y){
        int inc = 0;
        for (int x = 0;x<10;x++){
            b[x][y].setBackground(Color.DARK_GRAY);
        }
        for (int c = y-1;c>-1;c--){
            for (int x = 0;x<10;x++){
                tmp[x][y-inc] = b[x][c].getBackground();
            }
            inc++;
        }
        for (int c = y;c>-1;c--){
            for (int x = 0;x<10;x++){
                b[x][c].setBackground(tmp[x][c]);
            }
        }
    }
 
    public void movelr(){
        int[] m2 = {-1, -1, -1, -1};
        int[] m1 = {-1, -1, -1, -1};
        int[] zero = {-1, -1, -1, -1};
        int[] one = {-1, -1, -1, -1};
        int[] two = {-1, -1, -1, -1};
        for (int d = 0;d<4;d++){
            if (prof[pos[0]][d][rand-1].y == -2){
                m2[d] = d;
            } else if (prof[pos[0]][d][rand-1].y == -1){
                m1[d] = d;
            } else if (prof[pos[0]][d][rand-1].y == 0){
                zero[d] = d;
            } else if (prof[pos[0]][d][rand-1].y == 1){
                one[d] = d;
            } else if (prof[pos[0]][d][rand-1].y == 2){
                two[d] = d;
            }
        }
        int tmpm2 = -5;
        int tmpm1 = -5;
        int tmpzero = -5;
        int tmpone = -5;
        int tmptwo = -5;
        if (deltax == 1){
            for (int d = 0;d<4;d++){
                if (m2[d] != -1){
                    if (tmpm2<prof[pos[0]][m2[d]][rand-1].x){
                        tmpm2 = prof[pos[0]][m2[d]][rand-1].x;
                    }
                }
                if (m1[d] != -1){
                    if (tmpm1<prof[pos[0]][m1[d]][rand-1].x){
                        tmpm1 = prof[pos[0]][m1[d]][rand-1].x;
                    }
                }
                if (zero[d] != -1){
                    if (tmpzero<prof[pos[0]][zero[d]][rand-1].x){
                        tmpzero = prof[pos[0]][zero[d]][rand-1].x;
                    }
                }
                if (one[d] != -1){
                    if (tmpone<prof[pos[0]][one[d]][rand-1].x){
                        tmpone = prof[pos[0]][one[d]][rand-1].x;
                    }
                }
                if (two[d] != -1){
                    if (tmptwo<prof[pos[0]][two[d]][rand-1].x){
                        tmptwo = prof[pos[0]][two[d]][rand-1].x;
                    }
                }
            }
        } else if (deltax == -1){
            tmpm2 = 5;
            tmpm1 = 5;
            tmpzero = 5;
            tmpone = 5;
            tmptwo = 5;
            for (int d = 0;d<4;d++){
                if (m2[d] != -1){
                    if (tmpm2>prof[pos[0]][m2[d]][rand-1].x){
                        tmpm2 = prof[pos[0]][m2[d]][rand-1].x;
                    }
                }
                if (m1[d] != -1){
                    if (tmpm1>prof[pos[0]][m1[d]][rand-1].x){
                        tmpm1 = prof[pos[0]][m1[d]][rand-1].x;
                    }
                }
                if (zero[d] != -1){
                    if (tmpzero>prof[pos[0]][zero[d]][rand-1].x){
                        tmpzero = prof[pos[0]][zero[d]][rand-1].x;
                    }
                }
                if (one[d] != -1){
                    if (tmpone>prof[pos[0]][one[d]][rand-1].x){
                        tmpone = prof[pos[0]][one[d]][rand-1].x;
                    }
                }
                if (two[d] != -1){
                    if (tmptwo>prof[pos[0]][two[d]][rand-1].x){
                        tmptwo = prof[pos[0]][two[d]][rand-1].x;
                    }
                }
            }
        }
        int total = 0;
        for (int d = 0;d<4;d++){
            if (prof[pos[0]][d][rand-1].y == -2){
                if (perim[2+centralx+deltax+tmpm2][2+centraly-2] != 2){
                    if(b[centralx+deltax+tmpm2][centraly-2].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            } else if (prof[pos[0]][d][rand-1].y == -1){
                if (perim[2+centralx+deltax+tmpm1][2+centraly-1] != 2){
                    if (b[centralx+deltax+tmpm1][centraly-1].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            } else if (prof[pos[0]][d][rand-1].y == 0){
                if (perim[2+centralx+deltax+tmpzero][2+centraly] != 2){
                    if (b[centralx+deltax+tmpzero][centraly].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            } else if (prof[pos[0]][d][rand-1].y == 1){
                if (perim[2+centralx+deltax+tmpone][2+centraly+1] != 2){
                    if (b[centralx+deltax+tmpone][centraly+1].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            } else if (prof[pos[0]][d][rand-1].y == 2){
                if (perim[2+centralx+deltax+tmptwo][2+centraly+2] != 2){
                    if (b[centralx+deltax+tmptwo][centraly+2].getBackground() == Color.DARK_GRAY){
                        total++;
                    }
                }
            }
        } if (total == 4){
            b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(Color.DARK_GRAY);
            b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(Color.DARK_GRAY);
            centralx = centralx+deltax;
            b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
            b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
            b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
            b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
        }
    }
 
    public static void main (String[] args){
        new Tetris();
    }
 
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT){
                deltax = 1;
                movelr();
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT){
            deltax = -1;
            movelr();
        }
        if (e.getKeyCode() == KeyEvent.VK_UP){
            rotate();
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN){
            movedown();
        }
    }
 
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    }
}



Comentarios sobre la versión: Versión 1 (18)

Imágen de perfil
10 de Junio del 2015
estrellaestrellaestrellaestrellaestrella
muy buen código ya lo pase y funciona bien , oye me podrías explicar algo del código , tengo unas cuantas preguntas, para aprender como usar esos paquetes del javax.swing y el awt soy nuevo en java y apenas ando empezando aprender a usarlo.

Quiero saber como generas los cuadritos y todo los bucles y todo porfa
Responder
Cristian
15 de Febrero del 2016
estrellaestrellaestrellaestrellaestrella
Muy buen codigo funciona perfectamente
Responder
puedes embiar tu trabajo plis
23 de Noviembre del 2016
estrellaestrellaestrellaestrellaestrella
muy buena
Responder
diana carolina cifuentes micolta
20 de Septiembre del 2018
estrellaestrellaestrellaestrellaestrella
Hola, Me quieres ayudar A Organizar Los Errores Que Hay que Acomodar En El Juego
Responder
esteban monjeló
15 de Octubre del 2020
estrellaestrellaestrellaestrellaestrella
como puedo hacer para ejecutarlo??
Responder
BUG
15 de Junio del 2016
estrellaestrellaestrellaestrellaestrella
El programa tiene un bug, a la hora de girar una ficha si choca con alguna que ya esta colocada desaparecen cuadrados coloreados que ya estaban colocados.
Responder
js_tates
27 de Marzo del 2017
estrellaestrellaestrellaestrellaestrella
Excelente trabajo, de gran ayuda... sigue así amigo¡¡¡
Responder
Rodrigo
13 de Marzo del 2018
estrellaestrellaestrellaestrellaestrella
Hola buenas tardes, una preguntota, eso va en el cuerpo principal o en un metodo?
Responder
:v
13 de Diciembre del 2018
estrellaestrellaestrellaestrellaestrella
principal
Responder
Carlos
7 de Febrero del 2019
estrellaestrellaestrellaestrellaestrella
Se puede ejecutar en android?
Responder
Jesus
18 de Agosto del 2019
estrellaestrellaestrellaestrellaestrella
interesante este codigo, aunque esta escaso de comentarios se nota que se hizo buen trabajo, y como dice otro comentario por ahi, tiene el bug de reemplazar el espacios de figuras al rotar cerca de estas
Responder
llll
16 de Septiembre del 2021
estrellaestrellaestrellaestrellaestrella
hola mundo
Responder
wena
1 de Octubre del 2019
estrellaestrellaestrellaestrellaestrella
Lo traté de hacer llamar mediante un botón desde otro jframe y solo se queda en blanco
Responder
19 de Noviembre del 2019
estrellaestrellaestrellaestrellaestrella
Apenas soy principiante en la programación Java pero esto es lo mejor de que he visto hasta ahora. Muy inpirador
Responder
ANTONIO
2 de Diciembre del 2020
estrellaestrellaestrellaestrellaestrella
donde lo ejecutais?? porque no puedo mover las fichas
Responder
luis
14 de Septiembre del 2021
estrellaestrellaestrellaestrellaestrella
No ha dejado ningún comentario
Responder
osgodperu
29 de Marzo del 2023
estrellaestrellaestrellaestrellaestrella
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Tetris extends JFrame implements KeyListener {
int pos[] = {0,1};
boolean bottom = false;
int n = 20;
int m = 10;
JButton b[][];
Color tmp[][] = new Color[m][n];
int rand = 0;
int centralx = 0;
int centraly = 0;
int deltax = 0;
int perim[][] = new int[m+4][n+4];
or[][][] prof = new or[4][4][7];
Color rnd[] = {Color.red, Color.yellow, Color.cyan, Color.green, Color.white, Color.blue, Color.orange};
int rowsclrd = 0;
public Tetris(){
for(int a = 0;a<4;a++){
for(int b = 0;b<4;b++){
for(int c = 0;c<7;c++){
prof[a][b][c] = new or();
}
}
}
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
prof[0][0][0].x = -1;
prof[0][1][0].x = 0;
prof[0][2][0].x = 0;
prof[0][3][0].x = 1;
prof[1][0][0].x = 0;
prof[1][1][0].x = 0;
prof[1][2][0].x = -1;
prof[1][3][0].x = -1;
prof[2][0][0].x = 1;
prof[2][1][0].x = 0;
prof[2][2][0].x = 0;
prof[2][3][0].x = -1;
prof[3][0][0].x = 0;
prof[3][1][0].x = 0;
prof[3][2][0].x = 1;
prof[3][3][0].x = 1;
prof[0][0][1].x = -1;
prof[0][1][1].x = -1;
prof[0][2][1].x = 0;
prof[0][3][1].x = 1;
prof[1][0][1].x = -1;
prof[1][1][1].x = 0;
prof[1][2][1].x = 0;
prof[1][3][1].x = 0;
prof[2][0][1].x = 1;
prof[2][1][1].x = 1;
prof[2][2][1].x = 0;
prof[2][3][1].x = -1;
prof[3][0][1].x = 1;
prof[3][1][1].x = 0;
prof[3][2][1].x = 0;
prof[3][3][1].x = 0;
prof[0][0][2].x = -1;
prof[0][1][2].x = 0;
prof[0][2][2].x = 1;
prof[0][3][2].x = 1;
prof[1][0][2].x = 0;
prof[1][1][2].x = 0;
prof[1][2][2].x = 0;
prof[1][3][2].x = -1;
prof[2][0][2].x = 1;
prof[2][1][2].x = 0;
prof[2][2][2].x = -1;
prof[2][3][2].x = -1;
prof[3][0][2].x = 0;
prof[3][1][2].x = 0;
prof[3][2][2].x = 0;
prof[3][3][2].x = 1;
prof[0][0][3].x = -1;
prof[0][1][3].x = 0;
prof[0][2][3].x = 0;
prof[0][3][3].x = 1;
prof[1][0][3].x = -1;
prof[1][1][3].x = -1;
prof[1][2][3].x = 0;
prof[1][3][3].x = 0;
prof[2][0][3].x = 1;
prof[2][1][3].x = 0;
prof[2][2][3].x = 0;
prof[2][3][3].x = -1;
prof[3][0][3].x = 1;
prof[3][1][3].x = 1;
prof[3][2][3].x = 0;
prof[3][3][3].x = 0;
prof[0][0][4].x = -1;
prof[0][1][4].x = 0;
prof[0][2][4].x = 0;
prof[0][3][4].x = 1;
prof[1][0][4].x = 0;
prof[1][1][4].x = 0;
prof[1][2][4].x = -1;
prof[1][3][4].x = 0;
prof[2][0][4].x = 1;
prof[2][1][4].x = 0;
prof[2][2][4].x = 0;
prof[2][3][4].x = -1;
prof[3][0][4].x = 0;
prof[3][1][4].x = 0;
prof[3][2][4].x = 1;
prof[3][3][4].x = 0;
prof[0][0][5].x = 0;
prof[0][1][5].x = 0;
prof[0][2][5].x = 1;
prof[0][3][5].x = 1;
prof[1][0][5].x = 0;
prof[1][1][5].x = 0;
prof[1][2][5].x = 1;
prof[1][3][5].x = 1;
prof[2][0][5].x = 0;
prof[2][1][5].x = 0;
prof[2][2][5].x = 1;
prof[2][3][5].x = 1;
prof[3][0][5].x = 0;
prof[3][1][5].x = 0;
prof[3][2][5].x = 1;
prof[3][3][5].x = 1;
prof[0][0][6].x = -1;
prof[0][1][6].x = 0;
prof[0][2][6].x = 1;
prof[0][3][6].x = 2;
prof[1][0][6].x = 0;
prof[1][1][6].x = 0;
prof[1][2][6].x = 0;
prof[1][3][6].x = 0;
prof[2][0][6].x = 1;
prof[2][1][6].x = 0;
prof[2][2][6].x = -1;
prof[2][3][6].x = -2;
prof[3][0][6].x = 0;
prof[3][1][6].x = 0;
prof[3][2][6].x = 0;
prof[3][3][6].x = 0;
prof[0][0][0].y = 0;
prof[0][1][0].y = 0;
prof[0][2][0].y = 1;
prof[0][3][0].y = 1;
prof[1][0][0].y = -1;
prof[1][1][0].y = 0;
prof[1][2][0].y = 0;
prof[1][3][0].y = 1;
prof[2][0][0].y = 0;
prof[2][1][0].y = 0;
prof[2][2][0].y = -1;
prof[2][3][0].y = -1;
prof[3][0][0].y = 1;
prof[3][1][0].y = 0;
prof[3][2][0].y = 0;
prof[3][3][0].y = -1;
prof[0][0][1].y = 0;
prof[0][1][1].y = 1;
prof[0][2][1].y = 0;
prof[0][3][1].y = 0;
prof[1][0][1].y = -1;
prof[1][1][1].y = -1;
prof[1][2][1].y = 0;
prof[1][3][1].y = 1;
prof[2][0][1].y = -1;
prof[2][1][1].y = 0;
prof[2][2][1].y = 0;
prof[2][3][1].y = 0;
prof[3][0][1].y = 1;
prof[3][1][1].y = 1;
prof[3][2][1].y = 0;
prof[3][3][1].y = -1;
prof[0][0][2].y = 0;
prof[0][1][2].y = 0;
prof[0][2][2].y = 0;
prof[0][3][2].y = 1;
prof[1][0][2].y = -1;
prof[1][1][2].y = 0;
prof[1][2][2].y = 1;
prof[1][3][2].y = 1;
prof[2][0][2].y = 0;
prof[2][1][2].y = 0;
prof[2][2][2].y = 0;
prof[2][3][2].y = -1;
prof[3][0][2].y = 1;
prof[3][1][2].y = 0;
prof[3][2][2].y = -1;
prof[3][3][2].y = -1;
prof[0][0][3].y = 1;
prof[0][1][3].y = 1;
prof[0][2][3].y = 0;
prof[0][3][3].y = 0;
prof[1][0][3].y = -1;
prof[1][1][3].y = 0;
prof[1][2][3].y = 0;
prof[1][3][3].y = 1;
prof[2][0][3].y = -1;
prof[2][1][3].y = -1;
prof[2][2][3].y = 0;
prof[2][3][3].y = 0;
prof[3][0][3].y = 1;
prof[3][1][3].y = 0;
prof[3][2][3].y = 0;
prof[3][3][3].y = -1;
prof[0][0][4].y = 0;
prof[0][1][4].y = 0;
prof[0][2][4].y = 1;
prof[0][3][4].y = 0;
prof[1][0][4].y = -1;
prof[1][1][4].y = 0;
prof[1][2][4].y = 0;
prof[1][3][4].y = 1;
prof[2][0][4].y = 0;
prof[2][1][4].y = 0;
prof[2][2][4].y = -1;
prof[2][3][4].y = 0;
prof[3][0][4].y = 1;
prof[3][1][4].y = 0;
prof[3][2][4].y = 0;
prof[3][3][4].y = -1;
prof[0][0][5].y = 0;
prof[0][1][5].y = 1;
prof[0][2][5].y = 0;
prof[0][3][5].y = 1;
prof[1][0][5].y = 0;
prof[1][1][5].y = 1;
prof[1][2][5].y = 0;
prof[1][3][5].y = 1;
prof[2][0][5].y = 0;
prof[2][1][5].y = 1;
prof[2][2][5].y = 0;
prof[2][3][5].y = 1;
prof[3][0][5].y = 0;
prof[3][1][5].y = 1;
prof[3][2][5].y = 0;
prof[3][3][5].y = 1;
prof[0][0][6].y = 0;
prof[0][1][6].y = 0;
prof[0][2][6].y = 0;
prof[0][3][6].y = 0;
prof[1][0][6].y = -1;
prof[1][1][6].y = 0;
prof[1][2][6].y = 1;
prof[1][3][6].y = 2;
prof[2][0][6].y = 0;
prof[2][1][6].y = 0;
prof[2][2][6].y = 0;
prof[2][3][6].y = 0;
prof[3][0][6].y = -1;
prof[3][1][6].y = 0;
prof[3][2][6].y = 1;
prof[3][3][6].y = 2;
for (int y = 0;y<2;y++){
for (int x = 0;x<m+4;x++){
perim[x][y]= 1;
}
}
for (int y = n+2;y<n+4;y++){
for (int x = 0;x<m+4;x++){
perim[x][y]= 4;
}
}
for (int y = 2;y<n+2;y++){
for (int x = 0;x<2;x++){
perim[x][y]= 2;
}
}
for (int y = 2;y<n+2;y++){
for (int x = m+2;x<m+4;x++){
perim[x][y]= 2;
}
}
for(int y = 0;y<n+4;y++){
for (int x = 0;x<m+4;x++){
System.out.print(perim[x][y]);
}
System.out.println("");
}
b = new JButton [m][n];
setLayout(new GridLayout(n,m));//Grid layouts x and y are SWAPPED!
for (int y = 0;y<n;y++){
for (int x = 0;x<m;x++){
b[x][y] = new JButton(" ");
tmp[x][y] = Color.DARK_GRAY;
b[x][y].setBackground(Color.DARK_GRAY);
add(b[x][y]);
b[x][y].setEnabled(true);
}//end inner for
}
setFocusable(true);
addKeyListener(this);
pack();
setVisible(true);
blockgen();

}//end constructor Mine()

class or {
int x;
int y;
}

public void blockgen(){
Component temporaryLostComponent = null;
pos[0] = 0;
pos[1] = 1;
rand = (int) (Math.floor(Math.random()*7+1));
centralx = 4;
centraly = 0;
System.out.print(rand);
if ((b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].getBackground() == Color.DARK_GRAY) &&
(b[4+prof[pos[0]][1][rand-1].x][prof[pos[0]][1][rand-1].y].getBackground() == Color.DARK_GRAY) &&
(b[4+prof[pos[0]][2][rand-1].x][prof[pos[0]][2][rand-1].y].getBackground() == Color.DARK_GRAY) &&
(b[4+prof[pos[0]][3][rand-1].x][prof[pos[0]][3][rand-1].y].getBackground() == Color.DARK_GRAY)){
b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
b[4+prof[pos[0]][1][rand-1].x][prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
b[4+prof[pos[0]][2][rand-1].x][prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
b[4+prof[pos[0]][3][rand-1].x][prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
go();
} else {
JOptionPane.showMessageDialog(temporaryLostComponent, "Game Over! You cleared "+rowsclrd+" rows, well done!");
System.exit(0);
}
}

public void rotate(){
if (pos[0] < 3){
pos[1] = pos[0];
pos[0]++;
} else if (pos[0] == 3){
pos[0] = 0;
pos[1] = 3;
} else {
System.out.println("error");
}
if ((perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 4) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 1) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 2) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 3)
&& (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 4) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 1) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 2) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 3)
&& (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 4) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 1) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 2) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 3)
&& (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 4) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 1) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 2) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 3)){
b[centralx+prof[pos[1]][0][rand-1].x][centraly+prof[pos[1]][0][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[1]][1][rand-1].x][centraly+prof[pos[1]][1][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[1]][2][rand-1].x][centraly+prof[pos[1]][2][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[1]][3][rand-1].x][centraly+prof[pos[1]][3][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+(prof[pos[0]][0][rand-1].x)][centraly+(prof[pos[0]][0][rand-1].y)].setBackground(rnd[rand-1]);
b[centralx+(prof[pos[0]][1][rand-1].x)][centraly+(prof[pos[0]][1][rand-1].y)].setBackground(rnd[rand-1]);
b[centralx+(prof[pos[0]][2][rand-1].x)][centraly+(prof[pos[0]][2][rand-1].y)].setBackground(rnd[rand-1]);
b[centralx+(prof[pos[0]][3][rand-1].x)][centraly+(prof[pos[0]][3][rand-1].y)].setBackground(rnd[rand-1]);
} else {
if (pos[1] > 0){
pos[0] = pos[1];
pos[1]--;
} else if (pos[1] == 0){
pos[0] = 0;
pos[1] = 3;
}
}
}


public int getxs(){
int xs = 0;
int[] xf = {-1, -1, -1, -1};
for (int d = 0;d<4;d++){
if ((xf[0] != prof[pos[0]][d][rand-1].x) || (xf[1] != prof[pos[0]][d][rand-1].x) || (xf[2] != prof[pos[0]][d][rand-1].x) || (xf[3] != prof[pos[0]][d][rand-1].x)){
xf[d] = prof[pos[0]][d][rand-1].x;
}
}
for (int d = 0;d<4;d++){
if (xf[d] != -1){
xs++;
}
}
return xs;
}


public void movedown(){
int[] m2 = {-1, -1, -1, -1};
int[] m1 = {-1, -1, -1, -1};
int[] zero = {-1, -1, -1, -1};
int[] one = {-1, -1, -1, -1};
int[] two = {-1, -1, -1, -1};
for (int d = 0;d<4;d++){
if (prof[pos[0]][d][rand-1].x == -2){
m2[d] = d;
} else if (prof[pos[0]][d][rand-1].x == -1){
m1[d] = d;
} else if (prof[pos[0]][d][rand-1].x == 0){
zero[d] = d;
} else if (prof[pos[0]][d][rand-1].x == 1){
one[d] = d;
} else if (prof[pos[0]][d][rand-1].x == 2){
two[d] = d;
}
}
int tmpm2 = -5;
int tmpm1 = -5;
int tmpzero = -5;
int tmpone = -5;
int tmptwo = -5;
for (int d = 0;d<4;d++){
if (m2[d] != -1){
if (tmpm2<prof[pos[0]][m2[d]][rand-1].y){
tmpm2 = prof[pos[0]][m2[d]][rand-1].y;
}
}
if (m1[d] != -1){
if (tmpm1<prof[pos[0]][m1[d]][rand-1].y){
tmpm1 = prof[pos[0]][m1[d]][rand-1].y;
}
}
if (zero[d] != -1){
if (tmpzero<prof[pos[0]][zero[d]][rand-1].y){
tmpzero = prof[pos[0]][zero[d]][rand-1].y;
}
}
if (one[d] != -1){
if (tmpone<prof[pos[0]][one[d]][rand-1].y){
tmpone = prof[pos[0]][one[d]][rand-1].y;
}
}
if (two[d] != -1){
if (tmptwo<prof[pos[0]][two[d]][rand-1].y){
tmptwo = prof[pos[0]][two[d]][rand-1].y;
}
}
}
int total = 0;
for (int d = 0;d<4;d++){
if (prof[pos[0]][d][rand-1].x == -2){
if (perim[2+centralx+-2][2+centraly+tmpm2+1] != 4){
if(b[centralx+-2][centraly+tmpm2+1].getBackground() == Color.DARK_GRAY){
total++;
}
}
} else if (prof[pos[0]][d][rand-1].x == -1){
if (perim[2+centralx+-1][2+centraly+tmpm1+1] != 4){
if (b[centralx+-1][centraly+tmpm1+1].getBackground() == Color.DARK_GRAY){
total++;
}
}
} else if (prof[pos[0]][d][rand-1].x == 0){
if (perim[2+centralx][2+centraly+tmpzero+1] != 4){
if (b[centralx][centraly+tmpzero+1].getBackground() == Color.DARK_GRAY){
total++;
}
}
} else if (prof[pos[0]][d][rand-1].x == 1){
if (perim[2+centralx+1][2+centraly+tmpone+1] != 4){
if (b[centralx+1][centraly+tmpone+1].getBackground() == Color.DARK_GRAY){
total++;
}
}
} else if (prof[pos[0]][d][rand-1].x == 2){
if (perim[2+centralx+2][2+centraly+tmptwo+1] != 4){
if (b[centralx+2][centraly+tmptwo+1].getBackground() == Color.DARK_GRAY){
total++;
}
}
}
}
if (total == 4){
b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(Color.DARK_GRAY);
centraly++;
b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
} else {

bottom = true;
}
}


public void go(){
do{
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
movedown();
rowcheck();
}
while(bottom == false);
bottom = false;
blockgen();
}

public void rowcheck(){
int row = 0;
for (int y = 0;y<20;y++){
for (int x = 0;x<10;x++){
if (b[x][y].getBackground() != Color.DARK_GRAY){
row++;
}
if (row == 10){
rowsclrd++;
rowclear(y);
}
}
row = 0;
}
}

public void rowclear(int y){
int inc = 0;
for (int x = 0;x<10;x++){
b[x][y].setBackground(Color.DARK_GRAY);
}
for (int c = y-1;c>-1;c--){
for (int x = 0;x<10;x++){
tmp[x][y-inc] = b[x][c].getBackground();
}
inc++;
}
for (int c = y;c>-1;c--){
for (int x = 0;x<10;x++){
b[x][c].setBackground(tmp[x][c]);
}
}
}

public void movelr(){
int[] m2 = {-1, -1, -1, -1};
int[] m1 = {-1, -1, -1, -1};
int[] zero = {-1, -1, -1, -1};
int[] one = {-1, -1, -1, -1};
int[] two = {-1, -1, -1, -1};
for (int d = 0;d<4;d++){
if (prof[pos[0]][d][rand-1].y == -2){
m2[d] = d;
} else if (prof[pos[0]][d][rand-1].y == -1){
m1[d] = d;
} else if (prof[pos[0]][d][rand-1].y == 0){
zero[d] = d;
} else if (prof[pos[0]][d][rand-1].y == 1){
one[d] = d;
} else if (prof[pos[0]][d][rand-1].y == 2){
two[d] = d;
}
}
int tmpm2 = -5;
int tmpm1 = -5;
int tmpzero = -5;
int tmpone = -5;
int tmptwo = -5;
if (deltax == 1){
for (int d = 0;d<4;d++){
if (m2[d] != -1){
if (tmpm2<prof[pos[0]][m2[d]][rand-1].x){
tmpm2 = prof[pos[0]][m2[d]][rand-1].x;
}
}
if (m1[d] != -1){
if (tmpm1<prof[pos[0]][m1[d]][rand-1].x){
tmpm1 = prof[pos[0]][m1[d]][rand-1].x;
}
}
if (zero[d] != -1){
if (tmpzero<prof[pos[0]][zero[d]][rand-1].x){
tmpzero = prof[pos[0]][zero[d]][rand-1].x;
}
}
if (one[d] != -1){
if (tmpone<prof[pos[0]][one[d]][rand-1].x){
tmpone = prof[pos[0]][one[d]][rand-1].x;
}
}
if (two[d] != -1){
if (tmptwo<prof[pos[0]][two[d]][rand-1].x){
tmptwo = prof[pos[0]][two[d]][rand-1].x;
}
}
}
} else if (deltax == -1){
tmpm2 = 5;
tmpm1 = 5;
tmpzero = 5;
tmpone = 5;
tmptwo = 5;
for (int d = 0;d<4;d++){
if (m2[d] != -1){
if (tmpm2>prof[pos[0]][m2[d]][rand-1].x){
tmpm2 = prof[pos[0]][m2[d]][rand-1].x;
}
}
if (m1[d] != -1){
if (tmpm1>prof[pos[0]][m1[d]][rand-1].x){
tmpm1 = prof[pos[0]][m1[d]][rand-1].x;
}
}
if (zero[d] != -1){
if (tmpzero>prof[pos[0]][zero[d]][rand-1].x){
tmpzero = prof[pos[0]][zero[d]][rand-1].x;
}
}
if (one[d] != -1){
if (tmpone>prof[pos[0]][one[d]][rand-1].x){
tmpone = prof[pos[0]][one[d]][rand-1].x;
}
}
if (two[d] != -1){
if (tmptwo>prof[pos[0]][two[d]][rand-1].x){
tmptwo = prof[pos[0]][two[d]][rand-1].x;
}
}
}
}
int total = 0;
for (int d = 0;d<4;d++){
if (prof[pos[0]][d][rand-1].y == -2){
if (perim[2+centralx+deltax+tmpm2][2+centraly-2] != 2){
if(b[centralx+deltax+tmpm2][centraly-2].getBackground() == Color.DARK_GRAY){
total++;
}
}
} else if (prof[pos[0]][d][rand-1].y == -1){
if (perim[2+centralx+deltax+tmpm1][2+centraly-1] != 2){
if (b[centralx+deltax+tmpm1][centraly-1].getBackground() == Color.DARK_GRAY){
total++;
}
}
} else if (prof[pos[0]][d][rand-1].y == 0){
if (perim[2+centralx+deltax+tmpzero][2+centraly] != 2){
if (b[centralx+deltax+tmpzero][centraly].getBackground() == Color.DARK_GRAY){
total++;
}
}
} else if (prof[pos[0]][d][rand-1].y == 1){
if (perim[2+centralx+deltax+tmpone][2+centraly+1] != 2){
if (b[centralx+deltax+tmpone][centraly+1].getBackground() == Color.DARK_GRAY){
total++;
}
}
} else if (prof[pos[0]][d][rand-1].y == 2){
if (perim[2+centralx+deltax+tmptwo][2+centraly+2] != 2){
if (b[centralx+deltax+tmptwo][centraly+2].getBackground() == Color.DARK_GRAY){
total++;
}
}
}
} if (total == 4){
b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(Color.DARK_GRAY);
b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(Color.DARK_GRAY);
centralx = centralx+deltax;
b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
}
}

public static void main (String[] args){
new Tetris();
}

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
deltax = 1;
movelr();
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
deltax = -1;
movelr();
}
if (e.getKeyCode() == KeyEvent.VK_UP){
rotate();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN){
movedown();
}
}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Responder
8 de Diciembre del 2021
estrellaestrellaestrellaestrellaestrella
No ha dejado ningún comentario
Responder

Comentar la versión: Versión 1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s2410