質問するログイン新規登録

質問編集履歴

2

2018/01/25 11:40

投稿

Kokkoo
Kokkoo

スコア7

title CHANGED
@@ -1,1 +1,1 @@
1
- Java クラスの追加方法について(テトリス)
1
+ Java解決。。。。。。。。
body CHANGED
@@ -1,312 +1,1 @@
1
- Javaでテトリスの実装をさせているのですが機能として次にくるブロックを表示させる機能追加をしようとしていて別途でnextblockpaenlを作成したのですが(コード2)、コード1の中にどのようにして組み込めばいいのか分かりません。
2
- どうすれば動くようになるのでしょうか?
3
- ```Java
4
- import java.awt.Color;
5
- import java.awt.Font;
6
- import java.awt.Graphics;
7
- import java.awt.Point;
8
- import java.awt.event.KeyEvent;
9
- import java.awt.event.KeyListener;
10
- import java.util.ArrayList;
11
- import java.util.Collections;
12
- import javax.swing.JFrame;
13
- import javax.swing.JPanel;
14
- public class Tetris extends JPanel {
15
- private static final long serialVersionUID = -8715353373678321308L;
16
- private final Point[][][]Tetraminos = {
17
- // I-Piece
18
- {
19
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
20
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
21
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
22
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
23
- },
24
- // J-Piece
25
- {
26
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
27
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
28
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
29
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
30
- },
31
- // L-Piece
32
- {
33
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
34
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
35
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
36
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
37
- },
38
- // O-Piece
39
- {
40
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
41
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
42
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
43
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
44
- },
45
- // S-Piece
46
- {
47
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
48
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
49
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
50
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
51
- },
52
- // T-Piece
53
- {
54
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
55
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
56
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
57
- { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
58
- },
59
- // Z-Piece
60
- {
61
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
62
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) },
63
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
64
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }
65
- }
66
- };
67
- private final Color[] tetraminoColors = {
68
- Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.magenta, Color.red
69
- };
70
- private Point pieceOrigin;
71
- private int currentPiece;
72
- private int NextPiece;
73
- private int rotation;
74
- private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
75
- private ArrayList<Integer> nextnextPieces = new ArrayList<Integer>();
76
- private long score;
77
- private Color[][] well;
78
- // Creates a border around the well and initializes the dropping piece
79
- private void init() {
80
- well = new Color[19][24];
81
- for (int i = 0; i < 18; i++) {
82
- for (int j = 0; j < 23; j++) {
83
- if (i == 0 || i == 11 || j == 22) {
84
- well[i][j] = Color.GRAY;
85
- } else {
86
- well[i][j] = Color.BLACK;
87
- }
88
- }
89
- }
90
- newPiece();
91
- }
92
- // Put a new, random piece into the dropping position
93
- public void newPiece() {
94
- pieceOrigin = new Point(5, 2);
95
- rotation = 0;
96
- if (nextPieces.isEmpty()) {
97
- Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
98
- Collections.shuffle(nextPieces);
99
- Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
100
- Collections.shuffle(nextnextPieces);
101
- currentPiece = nextPieces.get(0);
102
- NextPiece = nextnextPieces.get(0);
103
- nextnextPieces.remove(0);
104
- }else if(!nextPieces.isEmpty()) {
105
- Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
106
- Collections.shuffle(nextnextPieces);
107
- currentPiece = NextPiece;
108
- }
109
- NextPiece = nextnextPieces.get(0);
110
- nextnextPieces.remove(0);
111
- }
112
- // Collision test for the dropping piece
113
- private boolean collidesAt(int x, int y, int rotation) {
114
- for (Point p : Tetraminos[currentPiece][rotation]) {
115
- if (well[p.x + x][p.y + y] != Color.BLACK) {
116
- return true;
117
- }
118
- }
119
- return false;
120
- }
121
- // Rotate the piece clockwise or counterclockwise
122
- public void rotate(int i) {
123
- int newRotation = (rotation + i) % 4;
124
- if (newRotation < 0) {
125
- newRotation = 3;
126
- }
127
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
128
- rotation = newRotation;
129
- }
130
- repaint();
131
- }
132
- // Move the piece left or right
133
- public void move(int i) {
134
- if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
135
- pieceOrigin.x += i;
136
- }
137
- repaint();
138
- }
139
- // Drops the piece one line or fixes it to the well if it can't drop
1
+ …解決……………………………………。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
140
- public void dropDown() {
141
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
142
- pieceOrigin.y += 1;
143
- } else {
144
- fixToWell();
145
- }
146
- repaint();
147
- }
148
- // Make the dropping piece part of the well, so it is available for
149
- // collision detection.
150
- public void fixToWell() {
151
- for (Point p : Tetraminos[currentPiece][rotation]) {
152
- well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
153
- }
154
- clearRows();
155
- newPiece();
156
- }
157
- public void deleteRow(int row) {
158
- for (int j = row-1; j > 0; j--) {
159
- for (int i = 1; i < 11; i++) {
160
- well[i][j+1] = well[i][j];
161
- }
162
- }
163
- }
164
- // Clear completed rows from the field and award score according to
165
- // the number of simultaneously cleared rows.
166
- public void clearRows() {
167
- boolean gap;
168
- int numClears = 0;
169
- for (int j = 21; j > 0; j--) {
170
- gap = false;
171
- for (int i = 1; i < 11; i++) {
172
- if (well[i][j] == Color.BLACK) {
173
- gap = true;
174
- break;
175
- }
176
- }
177
- if (!gap) {
178
- deleteRow(j);
179
- j += 1;
180
- numClears += 1;
181
- }
182
- }
183
- switch (numClears) {
184
- case 1:
185
- score += 100;
186
- break;
187
- case 2:
188
- score += 300;
189
- break;
190
- case 3:
191
- score += 500;
192
- break;
193
- case 4:
194
- score += 800;
195
- break;
196
- }
197
- }
198
- // Draw the falling piece
199
- private void drawPiece(Graphics g) {
200
- g.setColor(tetraminoColors[currentPiece]);
201
- for (Point p : Tetraminos[currentPiece][rotation]) {
202
- g.fillRect((p.x + pieceOrigin.x) * 26,
203
- (p.y + pieceOrigin.y) * 26,
204
- 25, 25);
205
- }
206
- }
207
- private void drawNextPiece(Graphics g) {
208
- g.setColor(tetraminoColors[NextPiece]);
209
- g.fillRect(5 * 68, 2 * 26, 25, 25);
210
- }
211
- @Override
212
- public void paintComponent(Graphics g)
213
- {
214
- // Paint the well
215
- g.fillRect(0, 0, 26*19, 26*23);
216
- for (int i = 0; i < 19; i++) {
217
- for (int j = 0; j < 23; j++) {
218
- g.setColor(well[i][j]);
219
- g.fillRect(26*i, 26*j, 25, 25);
220
- }
221
- }
222
- // Display the score
223
- Font font1 = new Font("Arial", Font.PLAIN, 20);
224
- g.setFont(font1);
225
- g.setColor(Color.RED);
226
- g.drawString("SCORE : " + score, 19*12+85, 500);
227
- g.drawString("NEXT", 5 * 63, 2 * 18);
228
- // Draw the currently falling piece
229
- drawPiece(g);
230
- drawNextPiece(g);
231
- }
232
- public static void main(String[] args) {
233
- JFrame f = new JFrame("Tetris");
234
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
235
- f.setSize(12*40, 26*23+25);
236
- f.setVisible(true);
237
- final Tetris game = new Tetris();
238
- game.init();
239
- f.add(game);
240
- // Keyboard controls
241
- f.addKeyListener(new KeyListener() {
242
- public void keyTyped(KeyEvent e) {
243
- }
244
- public void keyPressed(KeyEvent e) {
245
- switch (e.getKeyCode()) {
246
- case KeyEvent.VK_UP:
247
- game.rotate(-1);
248
- break;
249
- case KeyEvent.VK_DOWN:
250
- game.rotate(+1);
251
- break;
252
- case KeyEvent.VK_LEFT:
253
- game.move(-1);
254
- break;
255
- case KeyEvent.VK_RIGHT:
256
- game.move(+1);
257
- break;
258
- case KeyEvent.VK_SPACE:
259
- game.dropDown();
260
- game.score += 1;
261
- break;
262
- }
263
- }
264
- public void keyReleased(KeyEvent e) {
265
- }
266
- });
267
- // Make the falling piece drop every second
268
- new Thread() {
269
- @Override public void run() {
270
- while (true) {
271
- try {
272
- Thread.sleep(1000);
273
- game.dropDown();
274
- } catch ( InterruptedException e ) {}
275
- }
276
- }
277
- }.start();
278
- }
279
- }
280
- ```
281
- ```Java
282
- import java.awt.Color;
283
- import java.awt.Dimension;
284
- import java.awt.Graphics;
285
- import java.awt.Image;
286
- import javax.swing.JPanel;
287
- /*
288
- * Created on 2006/12/09
289
- */
290
- public class NextBlockPanel extends JPanel {
291
- public static final int WIDTH = 96;
292
- public static final int HEIGHT = 400;
293
- private Block nextBlock;
294
- private Image blockImage;
295
- public NextBlockPanel() {
296
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
297
- }
298
- public void paintComponent(Graphics g) {
299
- g.setColor(Color.BLACK);
300
- g.fillRect(0, 0, WIDTH, HEIGHT);
301
- // 次のブロックを描画
302
- if (nextBlock != null) {
303
- nextBlock.drawInPanel(g, blockImage);
304
- }
305
- }
306
- public void set(Block nextBlock, Image blockImage) {
307
- this.nextBlock = nextBlock;
308
- this.blockImage = blockImage;
309
- repaint();
310
- }
311
- }
312
- ```

1

解決しました

2018/01/25 11:40

投稿

Kokkoo
Kokkoo

スコア7

title CHANGED
File without changes
body CHANGED
@@ -1,409 +1,312 @@
1
1
  Javaでテトリスの実装をさせているのですが機能として次にくるブロックを表示させる機能追加をしようとしていて別途でnextblockpaenlを作成したのですが(コード2)、コード1の中にどのようにして組み込めばいいのか分かりません。
2
2
  どうすれば動くようになるのでしょうか?
3
-
4
3
  ```Java
5
- import java.awt.Color;
6
- import java.awt.Font;
7
- import java.awt.Graphics;
8
- import java.awt.Point;
9
- import java.awt.event.KeyEvent;
10
- import java.awt.event.KeyListener;
11
- import java.util.ArrayList;
12
- import java.util.Collections;
13
-
14
- import javax.swing.JFrame;
15
- import javax.swing.JPanel;
16
-
17
-
18
-
19
- public class Tetris extends JPanel {
20
-
21
- private static final long serialVersionUID = -8715353373678321308L;
22
-
23
- private final Point[][][]Tetraminos = {
24
- // I-Piece
25
- {
26
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
27
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
28
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
29
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
30
- },
31
-
32
- // J-Piece
33
- {
34
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
35
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
36
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
37
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
38
- },
39
-
40
- // L-Piece
41
- {
42
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
43
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
44
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
45
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
46
- },
47
-
48
- // O-Piece
49
- {
50
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
51
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
52
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
53
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
54
- },
55
-
56
- // S-Piece
57
- {
58
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
59
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
60
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
61
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
62
- },
63
-
64
- // T-Piece
65
- {
66
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
67
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
68
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
69
- { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
70
- },
71
-
72
- // Z-Piece
73
- {
74
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
75
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) },
76
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
77
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }
78
- }
79
- };
80
-
81
-
82
- private final Color[] tetraminoColors = {
83
- Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.magenta, Color.red
84
- };
85
-
86
- private Point pieceOrigin;
87
-
88
- private int currentPiece;
89
- private int NextPiece;
90
-
91
- private int rotation;
92
-
93
- private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
94
- private ArrayList<Integer> nextnextPieces = new ArrayList<Integer>();
95
-
96
- private long score;
97
-
98
- private Color[][] well;
99
-
100
- // Creates a border around the well and initializes the dropping piece
101
- private void init() {
102
- well = new Color[19][24];
103
- for (int i = 0; i < 18; i++) {
104
- for (int j = 0; j < 23; j++) {
105
- if (i == 0 || i == 11 || j == 22) {
106
- well[i][j] = Color.GRAY;
107
- } else {
108
- well[i][j] = Color.BLACK;
109
- }
110
- }
111
- }
112
- newPiece();
113
- }
114
-
115
-
116
- // Put a new, random piece into the dropping position
117
- public void newPiece() {
118
- pieceOrigin = new Point(5, 2);
119
- rotation = 0;
120
- if (nextPieces.isEmpty()) {
121
- Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
122
- Collections.shuffle(nextPieces);
123
- Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
124
- Collections.shuffle(nextnextPieces);
125
- currentPiece = nextPieces.get(0);
126
- NextPiece = nextnextPieces.get(0);
127
- nextnextPieces.remove(0);
128
- }else if(!nextPieces.isEmpty()) {
129
- Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
130
- Collections.shuffle(nextnextPieces);
131
- currentPiece = NextPiece;
132
- }
133
- NextPiece = nextnextPieces.get(0);
134
- nextnextPieces.remove(0);
135
- }
136
-
137
-
138
- // Collision test for the dropping piece
139
- private boolean collidesAt(int x, int y, int rotation) {
140
- for (Point p : Tetraminos[currentPiece][rotation]) {
141
- if (well[p.x + x][p.y + y] != Color.BLACK) {
142
- return true;
143
- }
144
- }
145
- return false;
146
- }
147
-
148
-
149
- // Rotate the piece clockwise or counterclockwise
150
- public void rotate(int i) {
151
- int newRotation = (rotation + i) % 4;
152
- if (newRotation < 0) {
153
- newRotation = 3;
154
- }
155
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
156
- rotation = newRotation;
157
- }
158
- repaint();
159
- }
160
-
161
-
162
- // Move the piece left or right
163
- public void move(int i) {
164
- if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
165
- pieceOrigin.x += i;
166
- }
167
- repaint();
168
- }
169
-
170
-
171
- // Drops the piece one line or fixes it to the well if it can't drop
172
- public void dropDown() {
173
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
174
- pieceOrigin.y += 1;
175
- } else {
176
- fixToWell();
177
- }
178
- repaint();
179
- }
180
-
181
-
182
- // Make the dropping piece part of the well, so it is available for
183
- // collision detection.
184
- public void fixToWell() {
185
- for (Point p : Tetraminos[currentPiece][rotation]) {
186
- well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
187
- }
188
- clearRows();
189
- newPiece();
190
- }
191
-
192
-
193
- public void deleteRow(int row) {
194
- for (int j = row-1; j > 0; j--) {
195
- for (int i = 1; i < 11; i++) {
196
- well[i][j+1] = well[i][j];
197
- }
198
- }
199
- }
200
-
201
-
202
- // Clear completed rows from the field and award score according to
203
- // the number of simultaneously cleared rows.
204
- public void clearRows() {
205
- boolean gap;
206
- int numClears = 0;
207
- for (int j = 21; j > 0; j--) {
208
- gap = false;
209
- for (int i = 1; i < 11; i++) {
210
- if (well[i][j] == Color.BLACK) {
211
- gap = true;
212
- break;
213
- }
214
- }
215
- if (!gap) {
216
- deleteRow(j);
217
- j += 1;
218
- numClears += 1;
219
- }
220
- }
221
- switch (numClears) {
222
- case 1:
223
- score += 100;
224
- break;
225
- case 2:
226
- score += 300;
227
- break;
228
- case 3:
229
- score += 500;
230
- break;
231
- case 4:
232
- score += 800;
233
- break;
234
- }
235
- }
236
-
237
-
238
- // Draw the falling piece
239
- private void drawPiece(Graphics g) {
240
- g.setColor(tetraminoColors[currentPiece]);
241
- for (Point p : Tetraminos[currentPiece][rotation]) {
242
- g.fillRect((p.x + pieceOrigin.x) * 26,
243
- (p.y + pieceOrigin.y) * 26,
244
- 25, 25);
245
- }
246
- }
247
-
248
- private void drawNextPiece(Graphics g) {
249
- g.setColor(tetraminoColors[NextPiece]);
250
- g.fillRect(5 * 68, 2 * 26, 25, 25);
251
- }
252
-
253
- @Override
254
- public void paintComponent(Graphics g)
255
- {
256
- // Paint the well
257
- g.fillRect(0, 0, 26*19, 26*23);
258
- for (int i = 0; i < 19; i++) {
259
- for (int j = 0; j < 23; j++) {
260
- g.setColor(well[i][j]);
261
- g.fillRect(26*i, 26*j, 25, 25);
262
- }
263
- }
264
-
265
- // Display the score
266
- Font font1 = new Font("Arial", Font.PLAIN, 20);
267
- g.setFont(font1);
268
- g.setColor(Color.RED);
269
- g.drawString("SCORE : " + score, 19*12+85, 500);
270
- g.drawString("NEXT", 5 * 63, 2 * 18);
271
-
272
- // Draw the currently falling piece
273
- drawPiece(g);
274
-
275
- drawNextPiece(g);
276
- }
277
-
278
-
279
- public static void main(String[] args) {
280
- JFrame f = new JFrame("Tetris");
281
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
282
- f.setSize(12*40, 26*23+25);
283
- f.setVisible(true);
284
-
285
- final Tetris game = new Tetris();
286
- game.init();
287
- f.add(game);
288
-
289
- // Keyboard controls
290
- f.addKeyListener(new KeyListener() {
291
- public void keyTyped(KeyEvent e) {
292
- }
293
-
294
- public void keyPressed(KeyEvent e) {
295
- switch (e.getKeyCode()) {
296
- case KeyEvent.VK_UP:
297
- game.rotate(-1);
298
- break;
299
- case KeyEvent.VK_DOWN:
300
- game.rotate(+1);
301
- break;
302
- case KeyEvent.VK_LEFT:
303
- game.move(-1);
304
- break;
305
- case KeyEvent.VK_RIGHT:
306
- game.move(+1);
307
- break;
308
- case KeyEvent.VK_SPACE:
309
- game.dropDown();
310
- game.score += 1;
311
- break;
312
- }
313
- }
314
-
315
- public void keyReleased(KeyEvent e) {
316
- }
317
- });
318
-
319
-
320
- // Make the falling piece drop every second
321
- new Thread() {
322
- @Override public void run() {
323
- while (true) {
324
- try {
325
- Thread.sleep(1000);
326
- game.dropDown();
327
- } catch ( InterruptedException e ) {}
328
- }
329
- }
330
- }.start();
331
- }
4
+ import java.awt.Color;
5
+ import java.awt.Font;
6
+ import java.awt.Graphics;
7
+ import java.awt.Point;
8
+ import java.awt.event.KeyEvent;
9
+ import java.awt.event.KeyListener;
10
+ import java.util.ArrayList;
11
+ import java.util.Collections;
12
+ import javax.swing.JFrame;
13
+ import javax.swing.JPanel;
14
+ public class Tetris extends JPanel {
15
+ private static final long serialVersionUID = -8715353373678321308L;
16
+ private final Point[][][]Tetraminos = {
17
+ // I-Piece
18
+ {
19
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
20
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
21
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
22
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
23
+ },
24
+ // J-Piece
25
+ {
26
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
27
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
28
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
29
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
30
+ },
31
+ // L-Piece
32
+ {
33
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
34
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
35
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
36
+ { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
37
+ },
38
+ // O-Piece
39
+ {
40
+ { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
41
+ { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
42
+ { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
43
+ { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
44
+ },
45
+ // S-Piece
46
+ {
47
+ { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
48
+ { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
49
+ { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
50
+ { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
51
+ },
52
+ // T-Piece
53
+ {
54
+ { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
55
+ { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
56
+ { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
57
+ { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
58
+ },
59
+ // Z-Piece
60
+ {
61
+ { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
62
+ { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) },
63
+ { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
64
+ { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }
65
+ }
66
+ };
67
+ private final Color[] tetraminoColors = {
68
+ Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.magenta, Color.red
69
+ };
70
+ private Point pieceOrigin;
71
+ private int currentPiece;
72
+ private int NextPiece;
73
+ private int rotation;
74
+ private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
75
+ private ArrayList<Integer> nextnextPieces = new ArrayList<Integer>();
76
+ private long score;
77
+ private Color[][] well;
78
+ // Creates a border around the well and initializes the dropping piece
79
+ private void init() {
80
+ well = new Color[19][24];
81
+ for (int i = 0; i < 18; i++) {
82
+ for (int j = 0; j < 23; j++) {
83
+ if (i == 0 || i == 11 || j == 22) {
84
+ well[i][j] = Color.GRAY;
85
+ } else {
86
+ well[i][j] = Color.BLACK;
87
+ }
88
+ }
89
+ }
90
+ newPiece();
332
91
  }
92
+ // Put a new, random piece into the dropping position
93
+ public void newPiece() {
94
+ pieceOrigin = new Point(5, 2);
95
+ rotation = 0;
96
+ if (nextPieces.isEmpty()) {
97
+ Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
98
+ Collections.shuffle(nextPieces);
99
+ Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
100
+ Collections.shuffle(nextnextPieces);
101
+ currentPiece = nextPieces.get(0);
102
+ NextPiece = nextnextPieces.get(0);
103
+ nextnextPieces.remove(0);
104
+ }else if(!nextPieces.isEmpty()) {
105
+ Collections.addAll(nextnextPieces, 0, 1, 2, 3, 4, 5, 6);
106
+ Collections.shuffle(nextnextPieces);
107
+ currentPiece = NextPiece;
108
+ }
109
+ NextPiece = nextnextPieces.get(0);
110
+ nextnextPieces.remove(0);
111
+ }
112
+ // Collision test for the dropping piece
113
+ private boolean collidesAt(int x, int y, int rotation) {
114
+ for (Point p : Tetraminos[currentPiece][rotation]) {
115
+ if (well[p.x + x][p.y + y] != Color.BLACK) {
116
+ return true;
117
+ }
118
+ }
119
+ return false;
120
+ }
121
+ // Rotate the piece clockwise or counterclockwise
122
+ public void rotate(int i) {
123
+ int newRotation = (rotation + i) % 4;
124
+ if (newRotation < 0) {
125
+ newRotation = 3;
126
+ }
127
+ if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
128
+ rotation = newRotation;
129
+ }
130
+ repaint();
131
+ }
132
+ // Move the piece left or right
133
+ public void move(int i) {
134
+ if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
135
+ pieceOrigin.x += i;
136
+ }
137
+ repaint();
138
+ }
139
+ // Drops the piece one line or fixes it to the well if it can't drop
140
+ public void dropDown() {
141
+ if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
142
+ pieceOrigin.y += 1;
143
+ } else {
144
+ fixToWell();
145
+ }
146
+ repaint();
147
+ }
148
+ // Make the dropping piece part of the well, so it is available for
149
+ // collision detection.
150
+ public void fixToWell() {
151
+ for (Point p : Tetraminos[currentPiece][rotation]) {
152
+ well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
153
+ }
154
+ clearRows();
155
+ newPiece();
156
+ }
157
+ public void deleteRow(int row) {
158
+ for (int j = row-1; j > 0; j--) {
159
+ for (int i = 1; i < 11; i++) {
160
+ well[i][j+1] = well[i][j];
161
+ }
162
+ }
163
+ }
164
+ // Clear completed rows from the field and award score according to
165
+ // the number of simultaneously cleared rows.
166
+ public void clearRows() {
167
+ boolean gap;
168
+ int numClears = 0;
169
+ for (int j = 21; j > 0; j--) {
170
+ gap = false;
171
+ for (int i = 1; i < 11; i++) {
172
+ if (well[i][j] == Color.BLACK) {
173
+ gap = true;
174
+ break;
175
+ }
176
+ }
177
+ if (!gap) {
178
+ deleteRow(j);
179
+ j += 1;
180
+ numClears += 1;
181
+ }
182
+ }
183
+ switch (numClears) {
184
+ case 1:
185
+ score += 100;
186
+ break;
187
+ case 2:
188
+ score += 300;
189
+ break;
190
+ case 3:
191
+ score += 500;
192
+ break;
193
+ case 4:
194
+ score += 800;
195
+ break;
196
+ }
197
+ }
198
+ // Draw the falling piece
199
+ private void drawPiece(Graphics g) {
200
+ g.setColor(tetraminoColors[currentPiece]);
201
+ for (Point p : Tetraminos[currentPiece][rotation]) {
202
+ g.fillRect((p.x + pieceOrigin.x) * 26,
203
+ (p.y + pieceOrigin.y) * 26,
204
+ 25, 25);
205
+ }
206
+ }
207
+ private void drawNextPiece(Graphics g) {
208
+ g.setColor(tetraminoColors[NextPiece]);
209
+ g.fillRect(5 * 68, 2 * 26, 25, 25);
210
+ }
211
+ @Override
212
+ public void paintComponent(Graphics g)
213
+ {
214
+ // Paint the well
215
+ g.fillRect(0, 0, 26*19, 26*23);
216
+ for (int i = 0; i < 19; i++) {
217
+ for (int j = 0; j < 23; j++) {
218
+ g.setColor(well[i][j]);
219
+ g.fillRect(26*i, 26*j, 25, 25);
220
+ }
221
+ }
222
+ // Display the score
223
+ Font font1 = new Font("Arial", Font.PLAIN, 20);
224
+ g.setFont(font1);
225
+ g.setColor(Color.RED);
226
+ g.drawString("SCORE : " + score, 19*12+85, 500);
227
+ g.drawString("NEXT", 5 * 63, 2 * 18);
228
+ // Draw the currently falling piece
229
+ drawPiece(g);
230
+ drawNextPiece(g);
231
+ }
232
+ public static void main(String[] args) {
233
+ JFrame f = new JFrame("Tetris");
234
+ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
235
+ f.setSize(12*40, 26*23+25);
236
+ f.setVisible(true);
237
+ final Tetris game = new Tetris();
238
+ game.init();
239
+ f.add(game);
240
+ // Keyboard controls
241
+ f.addKeyListener(new KeyListener() {
242
+ public void keyTyped(KeyEvent e) {
243
+ }
244
+ public void keyPressed(KeyEvent e) {
245
+ switch (e.getKeyCode()) {
246
+ case KeyEvent.VK_UP:
247
+ game.rotate(-1);
248
+ break;
249
+ case KeyEvent.VK_DOWN:
250
+ game.rotate(+1);
251
+ break;
252
+ case KeyEvent.VK_LEFT:
253
+ game.move(-1);
254
+ break;
255
+ case KeyEvent.VK_RIGHT:
256
+ game.move(+1);
257
+ break;
258
+ case KeyEvent.VK_SPACE:
259
+ game.dropDown();
260
+ game.score += 1;
261
+ break;
262
+ }
263
+ }
264
+ public void keyReleased(KeyEvent e) {
265
+ }
266
+ });
267
+ // Make the falling piece drop every second
268
+ new Thread() {
269
+ @Override public void run() {
270
+ while (true) {
271
+ try {
272
+ Thread.sleep(1000);
273
+ game.dropDown();
274
+ } catch ( InterruptedException e ) {}
275
+ }
276
+ }
277
+ }.start();
278
+ }
279
+ }
333
280
  ```
334
-
335
281
  ```Java
336
282
  import java.awt.Color;
337
-
338
283
  import java.awt.Dimension;
339
-
340
284
  import java.awt.Graphics;
341
-
342
285
  import java.awt.Image;
343
-
344
-
345
-
346
286
  import javax.swing.JPanel;
347
-
348
-
349
-
350
287
  /*
351
-
352
- * Created on 2006/12/09
288
+ * Created on 2006/12/09
353
-
354
- */
289
+ */
355
-
356
-
357
-
358
290
  public class NextBlockPanel extends JPanel {
359
-
360
- public static final int WIDTH = 96;
291
+ public static final int WIDTH = 96;
361
-
362
- public static final int HEIGHT = 400;
292
+ public static final int HEIGHT = 400;
363
-
364
-
365
-
366
- private Block nextBlock;
293
+ private Block nextBlock;
367
-
368
- private Image blockImage;
294
+ private Image blockImage;
369
-
370
-
371
-
372
- public NextBlockPanel() {
295
+ public NextBlockPanel() {
373
-
374
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
296
+ setPreferredSize(new Dimension(WIDTH, HEIGHT));
375
-
376
- }
297
+ }
377
-
378
-
379
-
380
- public void paintComponent(Graphics g) {
298
+ public void paintComponent(Graphics g) {
381
-
382
- g.setColor(Color.BLACK);
299
+ g.setColor(Color.BLACK);
383
-
384
- g.fillRect(0, 0, WIDTH, HEIGHT);
300
+ g.fillRect(0, 0, WIDTH, HEIGHT);
385
-
386
-
387
-
388
- // 次のブロックを描画
301
+ // 次のブロックを描画
389
-
390
- if (nextBlock != null) {
302
+ if (nextBlock != null) {
391
-
392
- nextBlock.drawInPanel(g, blockImage);
303
+ nextBlock.drawInPanel(g, blockImage);
393
-
394
- }
304
+ }
395
-
396
- }
305
+ }
397
-
398
- public void set(Block nextBlock, Image blockImage) {
306
+ public void set(Block nextBlock, Image blockImage) {
399
-
400
- this.nextBlock = nextBlock;
307
+ this.nextBlock = nextBlock;
401
-
402
- this.blockImage = blockImage;
308
+ this.blockImage = blockImage;
403
-
404
- repaint();
309
+ repaint();
405
-
406
- }
310
+ }
407
-
408
311
  }
409
312
  ```