質問編集履歴

2

2018/01/25 11:40

投稿

Kokkoo
Kokkoo

スコア7

test CHANGED
@@ -1 +1 @@
1
- Java クラスの追加方法について(テトリス)
1
+ Java解決。。。。。。。。
test CHANGED
@@ -1,623 +1 @@
1
- Javaでテトリスの実装をさせているのですが機能として次にくるブロックを表示させる機能追加をしようとしていて別途でnextblockpaenlを作成したのですが(コード2)、コード1の中にどのようにして組み込めばいいのか分かりません。
2
-
3
- どうすれば動くようになるのでしょうか?
4
-
5
- ```Java
6
-
7
- import java.awt.Color;
8
-
9
- import java.awt.Font;
10
-
11
- import java.awt.Graphics;
12
-
13
- import java.awt.Point;
14
-
15
- import java.awt.event.KeyEvent;
16
-
17
- import java.awt.event.KeyListener;
18
-
19
- import java.util.ArrayList;
20
-
21
- import java.util.Collections;
22
-
23
- import javax.swing.JFrame;
24
-
25
- import javax.swing.JPanel;
26
-
27
- public class Tetris extends JPanel {
28
-
29
- private static final long serialVersionUID = -8715353373678321308L;
30
-
31
- private final Point[][][]Tetraminos = {
32
-
33
- // I-Piece
34
-
35
- {
36
-
37
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
38
-
39
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
40
-
41
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
42
-
43
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
44
-
45
- },
46
-
47
- // J-Piece
48
-
49
- {
50
-
51
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
52
-
53
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
54
-
55
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
56
-
57
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
58
-
59
- },
60
-
61
- // L-Piece
62
-
63
- {
64
-
65
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
66
-
67
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
68
-
69
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
70
-
71
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
72
-
73
- },
74
-
75
- // O-Piece
76
-
77
- {
78
-
79
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
80
-
81
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
82
-
83
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
84
-
85
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
86
-
87
- },
88
-
89
- // S-Piece
90
-
91
- {
92
-
93
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
94
-
95
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
96
-
97
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
98
-
99
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
100
-
101
- },
102
-
103
- // T-Piece
104
-
105
- {
106
-
107
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
108
-
109
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
110
-
111
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
112
-
113
- { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
114
-
115
- },
116
-
117
- // Z-Piece
118
-
119
- {
120
-
121
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
122
-
123
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) },
124
-
125
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
126
-
127
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }
128
-
129
- }
130
-
131
- };
132
-
133
- private final Color[] tetraminoColors = {
134
-
135
- Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.magenta, Color.red
136
-
137
- };
138
-
139
- private Point pieceOrigin;
140
-
141
- private int currentPiece;
142
-
143
- private int NextPiece;
144
-
145
- private int rotation;
146
-
147
- private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
148
-
149
- private ArrayList<Integer> nextnextPieces = new ArrayList<Integer>();
150
-
151
- private long score;
152
-
153
- private Color[][] well;
154
-
155
- // Creates a border around the well and initializes the dropping piece
156
-
157
- private void init() {
158
-
159
- well = new Color[19][24];
160
-
161
- for (int i = 0; i < 18; i++) {
162
-
163
- for (int j = 0; j < 23; j++) {
164
-
165
- if (i == 0 || i == 11 || j == 22) {
166
-
167
- well[i][j] = Color.GRAY;
168
-
169
- } else {
170
-
171
- well[i][j] = Color.BLACK;
172
-
173
- }
174
-
175
- }
176
-
177
- }
178
-
179
- newPiece();
180
-
181
- }
182
-
183
- // Put a new, random piece into the dropping position
184
-
185
- public void newPiece() {
186
-
187
- pieceOrigin = new Point(5, 2);
188
-
189
- rotation = 0;
190
-
191
- if (nextPieces.isEmpty()) {
192
-
193
- Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
194
-
195
- Collections.shuffle(nextPieces);
196
-
197
- Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
198
-
199
- Collections.shuffle(nextnextPieces);
200
-
201
- currentPiece = nextPieces.get(0);
202
-
203
- NextPiece = nextnextPieces.get(0);
204
-
205
- nextnextPieces.remove(0);
206
-
207
- }else if(!nextPieces.isEmpty()) {
208
-
209
- Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
210
-
211
- Collections.shuffle(nextnextPieces);
212
-
213
- currentPiece = NextPiece;
214
-
215
- }
216
-
217
- NextPiece = nextnextPieces.get(0);
218
-
219
- nextnextPieces.remove(0);
220
-
221
- }
222
-
223
- // Collision test for the dropping piece
224
-
225
- private boolean collidesAt(int x, int y, int rotation) {
226
-
227
- for (Point p : Tetraminos[currentPiece][rotation]) {
228
-
229
- if (well[p.x + x][p.y + y] != Color.BLACK) {
230
-
231
- return true;
232
-
233
- }
234
-
235
- }
236
-
237
- return false;
238
-
239
- }
240
-
241
- // Rotate the piece clockwise or counterclockwise
242
-
243
- public void rotate(int i) {
244
-
245
- int newRotation = (rotation + i) % 4;
246
-
247
- if (newRotation < 0) {
248
-
249
- newRotation = 3;
250
-
251
- }
252
-
253
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
254
-
255
- rotation = newRotation;
256
-
257
- }
258
-
259
- repaint();
260
-
261
- }
262
-
263
- // Move the piece left or right
264
-
265
- public void move(int i) {
266
-
267
- if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
268
-
269
- pieceOrigin.x += i;
270
-
271
- }
272
-
273
- repaint();
274
-
275
- }
276
-
277
- // Drops the piece one line or fixes it to the well if it can't drop
1
+ …解決……………………………………。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
278
-
279
- public void dropDown() {
280
-
281
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
282
-
283
- pieceOrigin.y += 1;
284
-
285
- } else {
286
-
287
- fixToWell();
288
-
289
- }
290
-
291
- repaint();
292
-
293
- }
294
-
295
- // Make the dropping piece part of the well, so it is available for
296
-
297
- // collision detection.
298
-
299
- public void fixToWell() {
300
-
301
- for (Point p : Tetraminos[currentPiece][rotation]) {
302
-
303
- well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
304
-
305
- }
306
-
307
- clearRows();
308
-
309
- newPiece();
310
-
311
- }
312
-
313
- public void deleteRow(int row) {
314
-
315
- for (int j = row-1; j > 0; j--) {
316
-
317
- for (int i = 1; i < 11; i++) {
318
-
319
- well[i][j+1] = well[i][j];
320
-
321
- }
322
-
323
- }
324
-
325
- }
326
-
327
- // Clear completed rows from the field and award score according to
328
-
329
- // the number of simultaneously cleared rows.
330
-
331
- public void clearRows() {
332
-
333
- boolean gap;
334
-
335
- int numClears = 0;
336
-
337
- for (int j = 21; j > 0; j--) {
338
-
339
- gap = false;
340
-
341
- for (int i = 1; i < 11; i++) {
342
-
343
- if (well[i][j] == Color.BLACK) {
344
-
345
- gap = true;
346
-
347
- break;
348
-
349
- }
350
-
351
- }
352
-
353
- if (!gap) {
354
-
355
- deleteRow(j);
356
-
357
- j += 1;
358
-
359
- numClears += 1;
360
-
361
- }
362
-
363
- }
364
-
365
- switch (numClears) {
366
-
367
- case 1:
368
-
369
- score += 100;
370
-
371
- break;
372
-
373
- case 2:
374
-
375
- score += 300;
376
-
377
- break;
378
-
379
- case 3:
380
-
381
- score += 500;
382
-
383
- break;
384
-
385
- case 4:
386
-
387
- score += 800;
388
-
389
- break;
390
-
391
- }
392
-
393
- }
394
-
395
- // Draw the falling piece
396
-
397
- private void drawPiece(Graphics g) {
398
-
399
- g.setColor(tetraminoColors[currentPiece]);
400
-
401
- for (Point p : Tetraminos[currentPiece][rotation]) {
402
-
403
- g.fillRect((p.x + pieceOrigin.x) * 26,
404
-
405
- (p.y + pieceOrigin.y) * 26,
406
-
407
- 25, 25);
408
-
409
- }
410
-
411
- }
412
-
413
- private void drawNextPiece(Graphics g) {
414
-
415
- g.setColor(tetraminoColors[NextPiece]);
416
-
417
- g.fillRect(5 * 68, 2 * 26, 25, 25);
418
-
419
- }
420
-
421
- @Override
422
-
423
- public void paintComponent(Graphics g)
424
-
425
- {
426
-
427
- // Paint the well
428
-
429
- g.fillRect(0, 0, 26*19, 26*23);
430
-
431
- for (int i = 0; i < 19; i++) {
432
-
433
- for (int j = 0; j < 23; j++) {
434
-
435
- g.setColor(well[i][j]);
436
-
437
- g.fillRect(26*i, 26*j, 25, 25);
438
-
439
- }
440
-
441
- }
442
-
443
- // Display the score
444
-
445
- Font font1 = new Font("Arial", Font.PLAIN, 20);
446
-
447
- g.setFont(font1);
448
-
449
- g.setColor(Color.RED);
450
-
451
- g.drawString("SCORE : " + score, 19*12+85, 500);
452
-
453
- g.drawString("NEXT", 5 * 63, 2 * 18);
454
-
455
- // Draw the currently falling piece
456
-
457
- drawPiece(g);
458
-
459
- drawNextPiece(g);
460
-
461
- }
462
-
463
- public static void main(String[] args) {
464
-
465
- JFrame f = new JFrame("Tetris");
466
-
467
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
468
-
469
- f.setSize(12*40, 26*23+25);
470
-
471
- f.setVisible(true);
472
-
473
- final Tetris game = new Tetris();
474
-
475
- game.init();
476
-
477
- f.add(game);
478
-
479
- // Keyboard controls
480
-
481
- f.addKeyListener(new KeyListener() {
482
-
483
- public void keyTyped(KeyEvent e) {
484
-
485
- }
486
-
487
- public void keyPressed(KeyEvent e) {
488
-
489
- switch (e.getKeyCode()) {
490
-
491
- case KeyEvent.VK_UP:
492
-
493
- game.rotate(-1);
494
-
495
- break;
496
-
497
- case KeyEvent.VK_DOWN:
498
-
499
- game.rotate(+1);
500
-
501
- break;
502
-
503
- case KeyEvent.VK_LEFT:
504
-
505
- game.move(-1);
506
-
507
- break;
508
-
509
- case KeyEvent.VK_RIGHT:
510
-
511
- game.move(+1);
512
-
513
- break;
514
-
515
- case KeyEvent.VK_SPACE:
516
-
517
- game.dropDown();
518
-
519
- game.score += 1;
520
-
521
- break;
522
-
523
- }
524
-
525
- }
526
-
527
- public void keyReleased(KeyEvent e) {
528
-
529
- }
530
-
531
- });
532
-
533
- // Make the falling piece drop every second
534
-
535
- new Thread() {
536
-
537
- @Override public void run() {
538
-
539
- while (true) {
540
-
541
- try {
542
-
543
- Thread.sleep(1000);
544
-
545
- game.dropDown();
546
-
547
- } catch ( InterruptedException e ) {}
548
-
549
- }
550
-
551
- }
552
-
553
- }.start();
554
-
555
- }
556
-
557
- }
558
-
559
- ```
560
-
561
- ```Java
562
-
563
- import java.awt.Color;
564
-
565
- import java.awt.Dimension;
566
-
567
- import java.awt.Graphics;
568
-
569
- import java.awt.Image;
570
-
571
- import javax.swing.JPanel;
572
-
573
- /*
574
-
575
- * Created on 2006/12/09
576
-
577
- */
578
-
579
- public class NextBlockPanel extends JPanel {
580
-
581
- public static final int WIDTH = 96;
582
-
583
- public static final int HEIGHT = 400;
584
-
585
- private Block nextBlock;
586
-
587
- private Image blockImage;
588
-
589
- public NextBlockPanel() {
590
-
591
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
592
-
593
- }
594
-
595
- public void paintComponent(Graphics g) {
596
-
597
- g.setColor(Color.BLACK);
598
-
599
- g.fillRect(0, 0, WIDTH, HEIGHT);
600
-
601
- // 次のブロックを描画
602
-
603
- if (nextBlock != null) {
604
-
605
- nextBlock.drawInPanel(g, blockImage);
606
-
607
- }
608
-
609
- }
610
-
611
- public void set(Block nextBlock, Image blockImage) {
612
-
613
- this.nextBlock = nextBlock;
614
-
615
- this.blockImage = blockImage;
616
-
617
- repaint();
618
-
619
- }
620
-
621
- }
622
-
623
- ```

1

解決しました

2018/01/25 11:40

投稿

Kokkoo
Kokkoo

スコア7

test CHANGED
File without changes
test CHANGED
@@ -2,815 +2,621 @@
2
2
 
3
3
  どうすれば動くようになるのでしょうか?
4
4
 
5
-
6
-
7
5
  ```Java
8
6
 
9
- import java.awt.Color;
10
-
11
- import java.awt.Font;
12
-
13
- import java.awt.Graphics;
14
-
15
- import java.awt.Point;
16
-
17
- import java.awt.event.KeyEvent;
18
-
19
- import java.awt.event.KeyListener;
20
-
21
- import java.util.ArrayList;
22
-
23
- import java.util.Collections;
24
-
25
-
26
-
27
- import javax.swing.JFrame;
28
-
29
- import javax.swing.JPanel;
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
- public class Tetris extends JPanel {
38
-
39
-
40
-
41
- private static final long serialVersionUID = -8715353373678321308L;
42
-
43
-
44
-
45
- private final Point[][][]Tetraminos = {
46
-
47
- // I-Piece
48
-
49
- {
50
-
51
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
52
-
53
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
54
-
55
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
56
-
57
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
58
-
59
- },
60
-
61
-
62
-
63
- // J-Piece
64
-
65
- {
66
-
67
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
68
-
69
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
70
-
71
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
72
-
73
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
74
-
75
- },
76
-
77
-
78
-
79
- // L-Piece
80
-
81
- {
82
-
83
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
84
-
85
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
86
-
87
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
88
-
89
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
90
-
91
- },
92
-
93
-
94
-
95
- // O-Piece
96
-
97
- {
98
-
99
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
100
-
101
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
102
-
103
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
104
-
105
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
106
-
107
- },
108
-
109
-
110
-
111
- // S-Piece
112
-
113
- {
114
-
115
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
116
-
117
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
118
-
119
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
120
-
121
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
122
-
123
- },
124
-
125
-
126
-
127
- // T-Piece
128
-
129
- {
130
-
131
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
132
-
133
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
134
-
135
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
136
-
137
- { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
138
-
139
- },
140
-
141
-
142
-
143
- // Z-Piece
144
-
145
- {
146
-
147
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
148
-
149
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) },
150
-
151
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
152
-
153
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }
154
-
155
- }
156
-
157
- };
158
-
159
-
160
-
161
-
162
-
163
- private final Color[] tetraminoColors = {
164
-
165
- Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.magenta, Color.red
166
-
167
- };
168
-
169
-
170
-
171
- private Point pieceOrigin;
172
-
173
-
174
-
175
- private int currentPiece;
176
-
177
- private int NextPiece;
178
-
179
-
180
-
181
- private int rotation;
182
-
183
-
184
-
185
- private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
186
-
187
- private ArrayList<Integer> nextnextPieces = new ArrayList<Integer>();
188
-
189
-
190
-
191
- private long score;
192
-
193
-
194
-
195
- private Color[][] well;
196
-
197
-
198
-
199
- // Creates a border around the well and initializes the dropping piece
200
-
201
- private void init() {
202
-
203
- well = new Color[19][24];
204
-
205
- for (int i = 0; i < 18; i++) {
206
-
207
- for (int j = 0; j < 23; j++) {
208
-
209
- if (i == 0 || i == 11 || j == 22) {
210
-
211
- well[i][j] = Color.GRAY;
212
-
213
- } else {
214
-
215
- well[i][j] = Color.BLACK;
216
-
217
- }
218
-
219
- }
220
-
221
- }
222
-
223
- newPiece();
224
-
225
- }
226
-
227
-
228
-
229
-
230
-
231
- // Put a new, random piece into the dropping position
232
-
233
- public void newPiece() {
234
-
235
- pieceOrigin = new Point(5, 2);
236
-
237
- rotation = 0;
238
-
239
- if (nextPieces.isEmpty()) {
240
-
241
- Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
242
-
243
- Collections.shuffle(nextPieces);
244
-
245
- Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
246
-
247
- Collections.shuffle(nextnextPieces);
248
-
249
- currentPiece = nextPieces.get(0);
250
-
251
- NextPiece = nextnextPieces.get(0);
252
-
253
- nextnextPieces.remove(0);
254
-
255
- }else if(!nextPieces.isEmpty()) {
256
-
257
- Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
258
-
259
- Collections.shuffle(nextnextPieces);
260
-
261
- currentPiece = NextPiece;
262
-
263
- }
264
-
265
- NextPiece = nextnextPieces.get(0);
266
-
267
- nextnextPieces.remove(0);
268
-
269
- }
270
-
271
-
272
-
273
-
274
-
275
- // Collision test for the dropping piece
276
-
277
- private boolean collidesAt(int x, int y, int rotation) {
278
-
279
- for (Point p : Tetraminos[currentPiece][rotation]) {
280
-
281
- if (well[p.x + x][p.y + y] != Color.BLACK) {
282
-
283
- return true;
284
-
285
- }
286
-
287
- }
288
-
289
- return false;
290
-
291
- }
292
-
293
-
294
-
295
-
296
-
297
- // Rotate the piece clockwise or counterclockwise
298
-
299
- public void rotate(int i) {
300
-
301
- int newRotation = (rotation + i) % 4;
302
-
303
- if (newRotation < 0) {
304
-
305
- newRotation = 3;
306
-
307
- }
308
-
309
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
310
-
311
- rotation = newRotation;
312
-
313
- }
314
-
315
- repaint();
316
-
317
- }
318
-
319
-
320
-
321
-
322
-
323
- // Move the piece left or right
324
-
325
- public void move(int i) {
326
-
327
- if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
328
-
329
- pieceOrigin.x += i;
330
-
331
- }
332
-
333
- repaint();
334
-
335
- }
336
-
337
-
338
-
339
-
340
-
341
- // Drops the piece one line or fixes it to the well if it can't drop
342
-
343
- public void dropDown() {
344
-
345
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
346
-
347
- pieceOrigin.y += 1;
348
-
349
- } else {
350
-
351
- fixToWell();
352
-
353
- }
354
-
355
- repaint();
356
-
357
- }
358
-
359
-
360
-
361
-
362
-
363
- // Make the dropping piece part of the well, so it is available for
364
-
365
- // collision detection.
366
-
367
- public void fixToWell() {
368
-
369
- for (Point p : Tetraminos[currentPiece][rotation]) {
370
-
371
- well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
372
-
373
- }
374
-
375
- clearRows();
376
-
377
- newPiece();
378
-
379
- }
380
-
381
-
382
-
383
-
384
-
385
- public void deleteRow(int row) {
386
-
387
- for (int j = row-1; j > 0; j--) {
388
-
389
- for (int i = 1; i < 11; i++) {
390
-
391
- well[i][j+1] = well[i][j];
392
-
393
- }
394
-
395
- }
396
-
397
- }
398
-
399
-
400
-
401
-
402
-
403
- // Clear completed rows from the field and award score according to
404
-
405
- // the number of simultaneously cleared rows.
406
-
407
- public void clearRows() {
408
-
409
- boolean gap;
410
-
411
- int numClears = 0;
412
-
413
- for (int j = 21; j > 0; j--) {
414
-
415
- gap = false;
416
-
417
- for (int i = 1; i < 11; i++) {
418
-
419
- if (well[i][j] == Color.BLACK) {
420
-
421
- gap = true;
422
-
423
- break;
424
-
425
- }
426
-
427
- }
428
-
429
- if (!gap) {
430
-
431
- deleteRow(j);
432
-
433
- j += 1;
434
-
435
- numClears += 1;
436
-
437
- }
438
-
439
- }
440
-
441
- switch (numClears) {
442
-
443
- case 1:
444
-
445
- score += 100;
446
-
447
- break;
448
-
449
- case 2:
450
-
451
- score += 300;
452
-
453
- break;
454
-
455
- case 3:
456
-
457
- score += 500;
458
-
459
- break;
460
-
461
- case 4:
462
-
463
- score += 800;
464
-
465
- break;
466
-
467
- }
468
-
469
- }
470
-
471
-
472
-
473
-
474
-
475
- // Draw the falling piece
476
-
477
- private void drawPiece(Graphics g) {
478
-
479
- g.setColor(tetraminoColors[currentPiece]);
480
-
481
- for (Point p : Tetraminos[currentPiece][rotation]) {
482
-
483
- g.fillRect((p.x + pieceOrigin.x) * 26,
484
-
485
- (p.y + pieceOrigin.y) * 26,
486
-
487
- 25, 25);
488
-
489
- }
490
-
491
- }
492
-
493
-
494
-
495
- private void drawNextPiece(Graphics g) {
496
-
497
- g.setColor(tetraminoColors[NextPiece]);
498
-
499
- g.fillRect(5 * 68, 2 * 26, 25, 25);
500
-
501
- }
502
-
503
-
504
-
505
- @Override
506
-
507
- public void paintComponent(Graphics g)
508
-
509
- {
510
-
511
- // Paint the well
512
-
513
- g.fillRect(0, 0, 26*19, 26*23);
514
-
515
- for (int i = 0; i < 19; i++) {
516
-
517
- for (int j = 0; j < 23; j++) {
518
-
519
- g.setColor(well[i][j]);
520
-
521
- g.fillRect(26*i, 26*j, 25, 25);
522
-
523
- }
524
-
525
- }
526
-
527
-
528
-
529
- // Display the score
530
-
531
- Font font1 = new Font("Arial", Font.PLAIN, 20);
532
-
533
- g.setFont(font1);
534
-
535
- g.setColor(Color.RED);
536
-
537
- g.drawString("SCORE : " + score, 19*12+85, 500);
538
-
539
- g.drawString("NEXT", 5 * 63, 2 * 18);
540
-
541
-
542
-
543
- // Draw the currently falling piece
544
-
545
- drawPiece(g);
546
-
547
-
548
-
549
- drawNextPiece(g);
550
-
551
- }
552
-
553
-
554
-
555
-
556
-
557
- public static void main(String[] args) {
558
-
559
- JFrame f = new JFrame("Tetris");
560
-
561
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
562
-
563
- f.setSize(12*40, 26*23+25);
564
-
565
- f.setVisible(true);
566
-
567
-
568
-
569
- final Tetris game = new Tetris();
570
-
571
- game.init();
572
-
573
- f.add(game);
574
-
575
-
576
-
577
- // Keyboard controls
578
-
579
- f.addKeyListener(new KeyListener() {
580
-
581
- public void keyTyped(KeyEvent e) {
582
-
583
- }
584
-
585
-
586
-
587
- public void keyPressed(KeyEvent e) {
588
-
589
- switch (e.getKeyCode()) {
590
-
591
- case KeyEvent.VK_UP:
592
-
593
- game.rotate(-1);
594
-
595
- break;
596
-
597
- case KeyEvent.VK_DOWN:
598
-
599
- game.rotate(+1);
600
-
601
- break;
602
-
603
- case KeyEvent.VK_LEFT:
604
-
605
- game.move(-1);
606
-
607
- break;
608
-
609
- case KeyEvent.VK_RIGHT:
610
-
611
- game.move(+1);
612
-
613
- break;
614
-
615
- case KeyEvent.VK_SPACE:
616
-
617
- game.dropDown();
618
-
619
- game.score += 1;
620
-
621
- break;
622
-
623
- }
624
-
625
- }
626
-
627
-
628
-
629
- public void keyReleased(KeyEvent e) {
630
-
631
- }
632
-
633
- });
634
-
635
-
636
-
637
-
638
-
639
- // Make the falling piece drop every second
640
-
641
- new Thread() {
642
-
643
- @Override public void run() {
644
-
645
- while (true) {
646
-
647
- try {
648
-
649
- Thread.sleep(1000);
650
-
651
- game.dropDown();
652
-
653
- } catch ( InterruptedException e ) {}
654
-
655
- }
656
-
657
- }
658
-
659
- }.start();
660
-
661
- }
7
+ import java.awt.Color;
8
+
9
+ import java.awt.Font;
10
+
11
+ import java.awt.Graphics;
12
+
13
+ import java.awt.Point;
14
+
15
+ import java.awt.event.KeyEvent;
16
+
17
+ import java.awt.event.KeyListener;
18
+
19
+ import java.util.ArrayList;
20
+
21
+ import java.util.Collections;
22
+
23
+ import javax.swing.JFrame;
24
+
25
+ import javax.swing.JPanel;
26
+
27
+ public class Tetris extends JPanel {
28
+
29
+ private static final long serialVersionUID = -8715353373678321308L;
30
+
31
+ private final Point[][][]Tetraminos = {
32
+
33
+ // I-Piece
34
+
35
+ {
36
+
37
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
38
+
39
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
40
+
41
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
42
+
43
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
44
+
45
+ },
46
+
47
+ // J-Piece
48
+
49
+ {
50
+
51
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
52
+
53
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
54
+
55
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
56
+
57
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
58
+
59
+ },
60
+
61
+ // L-Piece
62
+
63
+ {
64
+
65
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
66
+
67
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
68
+
69
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
70
+
71
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
72
+
73
+ },
74
+
75
+ // O-Piece
76
+
77
+ {
78
+
79
+ { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
80
+
81
+ { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
82
+
83
+ { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
84
+
85
+ { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
86
+
87
+ },
88
+
89
+ // S-Piece
90
+
91
+ {
92
+
93
+ { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
94
+
95
+ { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
96
+
97
+ { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
98
+
99
+ { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
100
+
101
+ },
102
+
103
+ // T-Piece
104
+
105
+ {
106
+
107
+ { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
108
+
109
+ { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
110
+
111
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
112
+
113
+ { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
114
+
115
+ },
116
+
117
+ // Z-Piece
118
+
119
+ {
120
+
121
+ { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
122
+
123
+ { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) },
124
+
125
+ { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
126
+
127
+ { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }
128
+
129
+ }
130
+
131
+ };
132
+
133
+ private final Color[] tetraminoColors = {
134
+
135
+ Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.magenta, Color.red
136
+
137
+ };
138
+
139
+ private Point pieceOrigin;
140
+
141
+ private int currentPiece;
142
+
143
+ private int NextPiece;
144
+
145
+ private int rotation;
146
+
147
+ private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
148
+
149
+ private ArrayList<Integer> nextnextPieces = new ArrayList<Integer>();
150
+
151
+ private long score;
152
+
153
+ private Color[][] well;
154
+
155
+ // Creates a border around the well and initializes the dropping piece
156
+
157
+ private void init() {
158
+
159
+ well = new Color[19][24];
160
+
161
+ for (int i = 0; i < 18; i++) {
162
+
163
+ for (int j = 0; j < 23; j++) {
164
+
165
+ if (i == 0 || i == 11 || j == 22) {
166
+
167
+ well[i][j] = Color.GRAY;
168
+
169
+ } else {
170
+
171
+ well[i][j] = Color.BLACK;
172
+
173
+ }
174
+
175
+ }
176
+
177
+ }
178
+
179
+ newPiece();
180
+
181
+ }
182
+
183
+ // Put a new, random piece into the dropping position
184
+
185
+ public void newPiece() {
186
+
187
+ pieceOrigin = new Point(5, 2);
188
+
189
+ rotation = 0;
190
+
191
+ if (nextPieces.isEmpty()) {
192
+
193
+ Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
194
+
195
+ Collections.shuffle(nextPieces);
196
+
197
+ Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
198
+
199
+ Collections.shuffle(nextnextPieces);
200
+
201
+ currentPiece = nextPieces.get(0);
202
+
203
+ NextPiece = nextnextPieces.get(0);
204
+
205
+ nextnextPieces.remove(0);
206
+
207
+ }else if(!nextPieces.isEmpty()) {
208
+
209
+ Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
210
+
211
+ Collections.shuffle(nextnextPieces);
212
+
213
+ currentPiece = NextPiece;
214
+
215
+ }
216
+
217
+ NextPiece = nextnextPieces.get(0);
218
+
219
+ nextnextPieces.remove(0);
220
+
221
+ }
222
+
223
+ // Collision test for the dropping piece
224
+
225
+ private boolean collidesAt(int x, int y, int rotation) {
226
+
227
+ for (Point p : Tetraminos[currentPiece][rotation]) {
228
+
229
+ if (well[p.x + x][p.y + y] != Color.BLACK) {
230
+
231
+ return true;
232
+
233
+ }
234
+
235
+ }
236
+
237
+ return false;
238
+
239
+ }
240
+
241
+ // Rotate the piece clockwise or counterclockwise
242
+
243
+ public void rotate(int i) {
244
+
245
+ int newRotation = (rotation + i) % 4;
246
+
247
+ if (newRotation < 0) {
248
+
249
+ newRotation = 3;
250
+
251
+ }
252
+
253
+ if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
254
+
255
+ rotation = newRotation;
256
+
257
+ }
258
+
259
+ repaint();
260
+
261
+ }
262
+
263
+ // Move the piece left or right
264
+
265
+ public void move(int i) {
266
+
267
+ if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
268
+
269
+ pieceOrigin.x += i;
270
+
271
+ }
272
+
273
+ repaint();
274
+
275
+ }
276
+
277
+ // Drops the piece one line or fixes it to the well if it can't drop
278
+
279
+ public void dropDown() {
280
+
281
+ if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
282
+
283
+ pieceOrigin.y += 1;
284
+
285
+ } else {
286
+
287
+ fixToWell();
288
+
289
+ }
290
+
291
+ repaint();
292
+
293
+ }
294
+
295
+ // Make the dropping piece part of the well, so it is available for
296
+
297
+ // collision detection.
298
+
299
+ public void fixToWell() {
300
+
301
+ for (Point p : Tetraminos[currentPiece][rotation]) {
302
+
303
+ well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
304
+
305
+ }
306
+
307
+ clearRows();
308
+
309
+ newPiece();
310
+
311
+ }
312
+
313
+ public void deleteRow(int row) {
314
+
315
+ for (int j = row-1; j > 0; j--) {
316
+
317
+ for (int i = 1; i < 11; i++) {
318
+
319
+ well[i][j+1] = well[i][j];
320
+
321
+ }
322
+
323
+ }
324
+
325
+ }
326
+
327
+ // Clear completed rows from the field and award score according to
328
+
329
+ // the number of simultaneously cleared rows.
330
+
331
+ public void clearRows() {
332
+
333
+ boolean gap;
334
+
335
+ int numClears = 0;
336
+
337
+ for (int j = 21; j > 0; j--) {
338
+
339
+ gap = false;
340
+
341
+ for (int i = 1; i < 11; i++) {
342
+
343
+ if (well[i][j] == Color.BLACK) {
344
+
345
+ gap = true;
346
+
347
+ break;
348
+
349
+ }
350
+
351
+ }
352
+
353
+ if (!gap) {
354
+
355
+ deleteRow(j);
356
+
357
+ j += 1;
358
+
359
+ numClears += 1;
360
+
361
+ }
362
+
363
+ }
364
+
365
+ switch (numClears) {
366
+
367
+ case 1:
368
+
369
+ score += 100;
370
+
371
+ break;
372
+
373
+ case 2:
374
+
375
+ score += 300;
376
+
377
+ break;
378
+
379
+ case 3:
380
+
381
+ score += 500;
382
+
383
+ break;
384
+
385
+ case 4:
386
+
387
+ score += 800;
388
+
389
+ break;
390
+
391
+ }
392
+
393
+ }
394
+
395
+ // Draw the falling piece
396
+
397
+ private void drawPiece(Graphics g) {
398
+
399
+ g.setColor(tetraminoColors[currentPiece]);
400
+
401
+ for (Point p : Tetraminos[currentPiece][rotation]) {
402
+
403
+ g.fillRect((p.x + pieceOrigin.x) * 26,
404
+
405
+ (p.y + pieceOrigin.y) * 26,
406
+
407
+ 25, 25);
408
+
409
+ }
410
+
411
+ }
412
+
413
+ private void drawNextPiece(Graphics g) {
414
+
415
+ g.setColor(tetraminoColors[NextPiece]);
416
+
417
+ g.fillRect(5 * 68, 2 * 26, 25, 25);
418
+
419
+ }
420
+
421
+ @Override
422
+
423
+ public void paintComponent(Graphics g)
424
+
425
+ {
426
+
427
+ // Paint the well
428
+
429
+ g.fillRect(0, 0, 26*19, 26*23);
430
+
431
+ for (int i = 0; i < 19; i++) {
432
+
433
+ for (int j = 0; j < 23; j++) {
434
+
435
+ g.setColor(well[i][j]);
436
+
437
+ g.fillRect(26*i, 26*j, 25, 25);
438
+
439
+ }
440
+
441
+ }
442
+
443
+ // Display the score
444
+
445
+ Font font1 = new Font("Arial", Font.PLAIN, 20);
446
+
447
+ g.setFont(font1);
448
+
449
+ g.setColor(Color.RED);
450
+
451
+ g.drawString("SCORE : " + score, 19*12+85, 500);
452
+
453
+ g.drawString("NEXT", 5 * 63, 2 * 18);
454
+
455
+ // Draw the currently falling piece
456
+
457
+ drawPiece(g);
458
+
459
+ drawNextPiece(g);
460
+
461
+ }
462
+
463
+ public static void main(String[] args) {
464
+
465
+ JFrame f = new JFrame("Tetris");
466
+
467
+ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
468
+
469
+ f.setSize(12*40, 26*23+25);
470
+
471
+ f.setVisible(true);
472
+
473
+ final Tetris game = new Tetris();
474
+
475
+ game.init();
476
+
477
+ f.add(game);
478
+
479
+ // Keyboard controls
480
+
481
+ f.addKeyListener(new KeyListener() {
482
+
483
+ public void keyTyped(KeyEvent e) {
484
+
485
+ }
486
+
487
+ public void keyPressed(KeyEvent e) {
488
+
489
+ switch (e.getKeyCode()) {
490
+
491
+ case KeyEvent.VK_UP:
492
+
493
+ game.rotate(-1);
494
+
495
+ break;
496
+
497
+ case KeyEvent.VK_DOWN:
498
+
499
+ game.rotate(+1);
500
+
501
+ break;
502
+
503
+ case KeyEvent.VK_LEFT:
504
+
505
+ game.move(-1);
506
+
507
+ break;
508
+
509
+ case KeyEvent.VK_RIGHT:
510
+
511
+ game.move(+1);
512
+
513
+ break;
514
+
515
+ case KeyEvent.VK_SPACE:
516
+
517
+ game.dropDown();
518
+
519
+ game.score += 1;
520
+
521
+ break;
522
+
523
+ }
524
+
525
+ }
526
+
527
+ public void keyReleased(KeyEvent e) {
528
+
529
+ }
530
+
531
+ });
532
+
533
+ // Make the falling piece drop every second
534
+
535
+ new Thread() {
536
+
537
+ @Override public void run() {
538
+
539
+ while (true) {
540
+
541
+ try {
542
+
543
+ Thread.sleep(1000);
544
+
545
+ game.dropDown();
546
+
547
+ } catch ( InterruptedException e ) {}
548
+
549
+ }
550
+
551
+ }
552
+
553
+ }.start();
554
+
555
+ }
662
556
 
663
557
  }
664
558
 
665
559
  ```
666
560
 
667
-
668
-
669
561
  ```Java
670
562
 
671
563
  import java.awt.Color;
672
564
 
673
-
674
-
675
565
  import java.awt.Dimension;
676
566
 
677
-
678
-
679
567
  import java.awt.Graphics;
680
568
 
681
-
682
-
683
569
  import java.awt.Image;
684
570
 
685
-
686
-
687
-
688
-
689
-
690
-
691
571
  import javax.swing.JPanel;
692
572
 
693
-
694
-
695
-
696
-
697
-
698
-
699
573
  /*
700
574
 
701
-
702
-
703
- * Created on 2006/12/09
575
+ * Created on 2006/12/09
704
-
705
-
706
-
576
+
707
- */
577
+ */
708
-
709
-
710
-
711
-
712
-
713
-
714
578
 
715
579
  public class NextBlockPanel extends JPanel {
716
580
 
717
-
718
-
719
- public static final int WIDTH = 96;
720
-
721
-
722
-
723
- public static final int HEIGHT = 400;
724
-
725
-
726
-
727
-
728
-
729
-
730
-
731
- private Block nextBlock;
732
-
733
-
734
-
735
- private Image blockImage;
736
-
737
-
738
-
739
-
740
-
741
-
742
-
743
- public NextBlockPanel() {
744
-
745
-
746
-
747
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
748
-
749
-
750
-
751
- }
752
-
753
-
754
-
755
-
756
-
757
-
758
-
759
- public void paintComponent(Graphics g) {
760
-
761
-
762
-
763
- g.setColor(Color.BLACK);
764
-
765
-
766
-
767
- g.fillRect(0, 0, WIDTH, HEIGHT);
768
-
769
-
770
-
771
-
772
-
773
-
774
-
775
- // 次のブロックを描画
776
-
777
-
778
-
779
- if (nextBlock != null) {
780
-
781
-
782
-
783
- nextBlock.drawInPanel(g, blockImage);
784
-
785
-
786
-
787
- }
788
-
789
-
790
-
791
- }
792
-
793
-
794
-
795
- public void set(Block nextBlock, Image blockImage) {
796
-
797
-
798
-
799
- this.nextBlock = nextBlock;
800
-
801
-
802
-
803
- this.blockImage = blockImage;
804
-
805
-
806
-
807
- repaint();
808
-
809
-
810
-
811
- }
812
-
813
-
581
+ public static final int WIDTH = 96;
582
+
583
+ public static final int HEIGHT = 400;
584
+
585
+ private Block nextBlock;
586
+
587
+ private Image blockImage;
588
+
589
+ public NextBlockPanel() {
590
+
591
+ setPreferredSize(new Dimension(WIDTH, HEIGHT));
592
+
593
+ }
594
+
595
+ public void paintComponent(Graphics g) {
596
+
597
+ g.setColor(Color.BLACK);
598
+
599
+ g.fillRect(0, 0, WIDTH, HEIGHT);
600
+
601
+ // 次のブロックを描画
602
+
603
+ if (nextBlock != null) {
604
+
605
+ nextBlock.drawInPanel(g, blockImage);
606
+
607
+ }
608
+
609
+ }
610
+
611
+ public void set(Block nextBlock, Image blockImage) {
612
+
613
+ this.nextBlock = nextBlock;
614
+
615
+ this.blockImage = blockImage;
616
+
617
+ repaint();
618
+
619
+ }
814
620
 
815
621
  }
816
622