Tetris/Java: Difference between revisions
m
→Code: Fixed syntax highlighting.
(→{{headerJava}}: added Java) |
m (→Code: Fixed syntax highlighting.) |
||
(9 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 {
right(1, 0), down(0, 1), left(-1, 0);
Dir(int x, int y) {
this.x = x;
this.y = y;
}
final int x, y;
};
public static final int EMPTY = -1;
public static final int BORDER = -2;
Shape fallingShape;
Shape nextShape;
// position of falling shape
int fallingShapeRow;
int fallingShapeCol;
final int[][] grid = new int[nRows][nCols];
Thread fallingThread;
final Scoreboard scoreboard = new Scoreboard();
static final Random rand = new Random();
public Tetris() {
setPreferredSize(
setBackground(
setFocusable(true);
initGrid();
selectShape();
Line 75 ⟶ 69:
case KeyEvent.VK_UP:
if (canRotate(
rotate(
break;
case KeyEvent.VK_LEFT:
if (canMove(
move(
break;
case KeyEvent.VK_RIGHT:
if (canMove(
move(
break;
Line 92 ⟶ 86:
if (!fastDown) {
fastDown = true;
while (canMove(
move(
repaint();
}
Line 110 ⟶ 104:
void selectShape() {
nextShape = shapes[rand.nextInt(shapes.length)];
if (fallingShape !=
fallingShape.reset();
}
Line 151 ⟶ 146:
try {
Thread.sleep(scoreboard.getSpeed());
} catch (InterruptedException
return;
}
if (!scoreboard.isGameOver()) {
if (canMove(
move(
} else {
shapeHasLanded();
Line 166 ⟶ 162:
void drawStartScreen(Graphics2D g) {
g.setFont(
g.setColor(
g.
g.
g.setColor(
g.drawString("Tetris",
g.setFont(smallFont);
g.drawString("click to start",
}
void drawSquare(Graphics2D g,
g.
g.fillRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
g.
g.setColor(squareBorder);
g.drawRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
Line 192 ⟶ 187:
void drawUI(Graphics2D g) {
g.setColor(
g.fill(gridRect);
// the blocks dropped in the grid
for (int r = 0; r < nRows; r++) {
for (int c = 0; c < nCols; c++) {
int
if (
drawSquare(g,
}
}
// the borders of grid and preview panel
g.setStroke(largeStroke);
g.setColor(gridBorderColor);
g.draw(gridRect);
g.draw(previewRect);
// scoreboard
int x = scoreX;
int y = scoreY;
g.setColor(textColor);
g.setFont(smallFont);
g.drawString(format("hiscore %6d", scoreboard.getTopscore()),
g.drawString(format("level %6d", scoreboard.getLevel()),
g.drawString(format("lines %6d", scoreboard.getLines()),
g.drawString(format("score %6d", scoreboard.getScore()),
// preview
int
for (int[] p : nextShape.pos) {
maxX = max(maxX, p[0]);
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 : nextShape.shape)
drawSquare(g, nextShape.ordinal(), p[1], p[0]);
g.translate(-cx, -cy);
}
void drawFallingShape(Graphics2D g) {
for (int[] p :
drawSquare(g,
}
Line 240 ⟶ 248:
drawUI(g);
if (
drawStartScreen(g);
} else {
drawFallingShape(g);
}
}
Line 265 ⟶ 271:
for (int[] p : pos) {
int newCol =
int newRow =
if (grid[newRow][newCol] != EMPTY) {
return false;
Line 285 ⟶ 291:
}
void move(
}
boolean canMove(Shape s,
for (int[] p : s.pos) {
int newCol =
int newRow =
if (grid[newRow][newCol] != EMPTY)
return false;
Line 301 ⟶ 307:
void shapeHasLanded() {
addShape(
if (
scoreboard.setGameOver();
scoreboard.setTopscore();
Line 339 ⟶ 345:
void addShape(Shape s) {
for (int[] p : s.pos)
grid[
}
Line 354 ⟶ 360:
});
}
}</syntaxhighlight>
<syntaxhighlight lang="java">package tetris;
class Scoreboard {
Line 461 ⟶ 469:
return score;
}
}</syntaxhighlight>
<syntaxhighlight lang="java">package tetris;
enum Shape {
ZShape(new int[][]{{0,
SShape(new int[][]{{0, -1}, {0, 0}, {1, 0}, {1, 1}}),
IShape(new int[][]{{0, -1}, {0, 0}, {0, 1}, {0, 2}}),
TShape(new int[][]{{-1, 0}, {0, 0}, {1, 0}, {0, 1}}),
Square(new int[][]{{0, 0}, {1, 0}, {0, 1}, {1, 1}}),
LShape(new int[][]{{-1, -1}, {0, -1}, {0, 0}, {0, 1}}),
JShape(new int[][]{{1, -1}, {0, -1}, {0, 0}, {0, 1}});
private Shape(int[][] shape) {
this.shape = shape;
pos = new int[4][2];
reset();
Line 473 ⟶ 490:
void reset() {
for (int i = 0; i < pos.length; i++) {
pos[i] =
}
}
final int[][] pos, shape;
}</syntaxhighlight>
<syntaxhighlight lang="java">package tetris;
import java.awt.*;
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);
final static Font smallFont = mainFont.deriveFont(Font.BOLD, 18);
final static Dimension dim = new Dimension(640, 640);
final static Rectangle gridRect = new Rectangle(46, 47, 308, 517);
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 int blockSize = 30;
final static int nRows = 18;
final static int nCols = 12;
final static int topMargin = 50;
final static int leftMargin = 20;
final static int scoreX = 400;
final static int scoreY = 330;
final static int titleX = 130;
final static int titleY = 150;
final static int clickX = 120;
final static int clickY = 400;
final static int previewCenterX = 467;
final static int previewCenterY = 97;
final static Stroke largeStroke = new BasicStroke(5);
final static Stroke smallStroke = new BasicStroke(2);
final
final static Color titlebgColor = Color.white;
final static Color textColor = Color.black;
final static Color bgColor = new Color(0xDDEEFF);
final static Color gridColor = new Color(0xBECFEA);
final static Color gridBorderColor = new Color(0x7788AA);
}</syntaxhighlight>
|