Tetris/Java: Difference between revisions
m
→Code: Fixed syntax highlighting.
m (→Code: Fixed syntax highlighting.) |
|||
(7 intermediate revisions by 2 users not shown) | |||
Line 1:
{{collection|Tetris}}
===Code===
{{works with|java|8}}
<syntaxhighlight lang
import java.awt.*;
import java.awt.event.*;
import static java.lang.Math.*;
import static java.lang.String.format;
import java.util.
import javax.swing.*;
import static tetris.Config.*;
public class Tetris extends JPanel implements Runnable {
enum Dir {
Dir(int x, int y) {
this.x = x;
this.y = y;
}
};
public static final int EMPTY = -1;
public static final int BORDER = -2;
Shape fallingShape;
Shape
// position of falling shape
Line 20 ⟶ 33:
int fallingShapeCol;
final int[][]
Thread fallingThread;
final Scoreboard scoreboard = new Scoreboard();
static final Random rand = new Random();
public Tetris() {
setPreferredSize(
setBackground(
setFocusable(true);
initGrid();
selectShape();
Line 66 ⟶ 74:
case KeyEvent.VK_LEFT:
if (canMove(fallingShape,
move(
break;
case KeyEvent.VK_RIGHT:
if (canMove(fallingShape,
move(
break;
Line 78 ⟶ 86:
if (!fastDown) {
fastDown = true;
while (canMove(fallingShape,
move(
repaint();
}
Line 98 ⟶ 106:
fallingShapeRow = 1;
fallingShapeCol = 5;
fallingShape =
Shape[] shapes = Shape.values();
if (fallingShape != null)
fallingShape.reset();
Line 138 ⟶ 146:
try {
Thread.sleep(scoreboard.getSpeed());
} catch (InterruptedException
return;
}
if (!scoreboard.isGameOver()) {
if (canMove(fallingShape,
move(
} else {
shapeHasLanded();
Line 153 ⟶ 162:
void drawStartScreen(Graphics2D g) {
g.setFont(
g.setColor(
g.fill(
g.fill(
g.setColor(
g.drawString("Tetris",
g.setFont(
g.drawString("click to start",
}
void drawSquare(Graphics2D g, int colorIndex, int r, int c) {
g.setColor(
g.fillRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
g.setStroke(
g.setColor(
g.drawRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
Line 179 ⟶ 188:
void drawUI(Graphics2D g) {
// grid background
g.setColor(
g.fill(
// the blocks dropped in the grid
Line 192 ⟶ 201:
// the borders of grid and preview panel
g.setStroke(
g.setColor(
g.draw(
g.draw(
// scoreboard
int x =
int y =
g.setColor(
g.setFont(
g.drawString(format("hiscore %6d", scoreboard.getTopscore()), x, y);
g.drawString(format("level %6d", scoreboard.getLevel()), x, y + 30);
Line 208 ⟶ 217:
// preview
int
minY = min(minY, p[1]);
maxY = max(maxY, p[1]);
}
double cx = previewCenterX - ((minX + maxX + 1) / 2.0 * blockSize);
double cy = previewCenterY - ((minY + maxY + 1) / 2.0 * blockSize);
g.translate(cx, cy);
▲ for (int[] p : preSelectedShape.shapes[idx])
for
drawSquare(g, nextShape.ordinal(), p[1], p[0]);
g.translate(-
}
Line 278 ⟶ 291:
}
void move(
fallingShapeRow +=
fallingShapeCol +=
}
boolean canMove(Shape s,
for (int[] p : s.pos) {
int newCol = fallingShapeCol +
int newRow = fallingShapeRow +
if (grid[newRow][newCol] != EMPTY)
return false;
Line 347 ⟶ 360:
});
}
}</syntaxhighlight>
<syntaxhighlight lang="java">package tetris;
class Scoreboard {
Line 454 ⟶ 469:
return score;
}
}</syntaxhighlight>
<syntaxhighlight lang="java">package tetris;
enum Shape {
ZShape(new int[][]{{0,
private Shape(int[][] shape) {
this.shape = shape;
pos = new int[4][2];
reset();
Line 466 ⟶ 490:
void reset() {
for (int i = 0; i < pos.length; i++) {
pos[i] =
}
}
final int[][] pos, shape;
}</syntaxhighlight>
<syntaxhighlight lang="java">package tetris;
▲ final int[][][] shapes = {
▲ {{0, -1}, {0, 0}, {-1, 0}, {-1, 1}},
import java.awt.*;
▲ {{0, -1}, {0, 0}, {1, 0}, {1, 1}},
▲ {{0, -1}, {0, 0}, {0, 1}, {0, 2}},
▲ {{-1, 0}, {0, 0}, {1, 0}, {0, 1}},
▲ {{0, 0}, {1, 0}, {0, 1}, {1, 1}},
▲ {{-1, -1}, {0, -1}, {0, 0}, {0, 1}},
▲ {{1, -1}, {0, -1}, {0, 0}, {0, 1}}};
final class Config {
final static Color[] colors = {Color.green, Color.red, Color.blue,
Color.pink, Color.orange, Color.cyan, Color.magenta};
final static Font mainFont = new Font("Monospaced", Font.BOLD, 48);
Line 494 ⟶ 509:
final static Dimension dim = new Dimension(640, 640);
final static Rectangle clickRect = new Rectangle(50, 375, 252, 40);
final static int blockSize = 30;
Line 506 ⟶ 526:
final static int clickX = 120;
final static int clickY = 400;
final static int previewCenterX = 467;
final static
▲ final static Rectangle previewRect = new Rectangle(387, 47, 200, 200);
▲ final static Rectangle titleRect = new Rectangle(100, 85, 252, 100);
▲ final static Rectangle clickRect = new Rectangle(50, 375, 252, 40);
final static Stroke largeStroke = new BasicStroke(5);
Line 521 ⟶ 538:
final static Color gridColor = new Color(0xBECFEA);
final static Color gridBorderColor = new Color(0x7788AA);
}</
|