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}}
=={{header|Java}}==
===Code===
{{works with|java|8}}
<syntaxhighlight lang ="java">importpackage java.awt.*tetris;
 
import java.awt.*;
import java.awt.event.*;
import static java.lang.Math.*;
import static java.lang.String.format;
import java.util.Arrays*;
import javax.swing.*;
import static tetris.Config.*;
 
public class Tetris extends JPanel implements Runnable {
enum Dir {
{{0, -right(1}, {0), down(0}, {-1), 0}, {left(-1, 1}},0);
 
Dir(int x, int y) {
this.x = x;
this.y = y;
}
final int[][][] shapes =x, {y;
};
 
public static final int EMPTY = -1;
public static final int BORDER = -2;
 
int[][] grid;
 
Shape fallingShape;
Shape preSelectedShapenextShape;
 
// position of falling shape
Line 20 ⟶ 33:
int fallingShapeCol;
 
final int[][] blockSizegrid = Config.blockSizenew int[nRows][nCols];
int nRows = Config.nRows;
int nCols = Config.nCols;
int topMargin = Config.topMargin;
int leftMargin = Config.leftMargin;
 
Thread fallingThread;
final Scoreboard scoreboard = new Scoreboard();
static final Random rand = new Random();
 
public Tetris() {
setPreferredSize(Config.dim);
setBackground(Config.bgColor);
setFocusable(true);
 
scoreboard = new Scoreboard();
grid = new int[nRows][nCols];
initGrid();
selectShape();
Line 66 ⟶ 74:
 
case KeyEvent.VK_LEFT:
if (canMove(fallingShape, -1, 0Dir.left))
move(fallingShape, -1, 0Dir.left);
break;
 
case KeyEvent.VK_RIGHT:
if (canMove(fallingShape, 1, 0Dir.right))
move(fallingShape, 1, 0Dir.right);
break;
 
Line 78 ⟶ 86:
if (!fastDown) {
fastDown = true;
while (canMove(fallingShape, 0, 1Dir.down)) {
move(fallingShape, 0, 1Dir.down);
repaint();
}
Line 98 ⟶ 106:
fallingShapeRow = 1;
fallingShapeCol = 5;
fallingShape = preSelectedShapenextShape;
Shape[] shapes = Shape.values();
preSelectedShapenextShape = shapes[(int) (Mathrand.randomnextInt() * shapes.length)];
if (fallingShape != null)
fallingShape.reset();
Line 138 ⟶ 146:
try {
Thread.sleep(scoreboard.getSpeed());
} catch (InterruptedException ignorede) {
return;
}
 
if (!scoreboard.isGameOver()) {
if (canMove(fallingShape, 0, 1Dir.down)) {
move(fallingShape, 0, 1Dir.down);
} else {
shapeHasLanded();
Line 153 ⟶ 162:
 
void drawStartScreen(Graphics2D g) {
g.setFont(Config.mainFont);
 
g.setColor(Config.titlebgColor);
g.fill(Config.titleRect);
g.fill(Config.clickRect);
 
g.setColor(Config.textColor);
g.drawString("Tetris", Config.titleX, Config.titleY);
 
g.setFont(Config.smallFont);
g.drawString("click to start", Config.clickX, Config.clickY);
}
 
void drawSquare(Graphics2D g, int colorIndex, int r, int c) {
g.setColor(Config.colors[colorIndex]);
g.fillRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
 
g.setStroke(Config.smallStroke);
g.setColor(Config.squareBorder);
g.drawRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
Line 179 ⟶ 188:
void drawUI(Graphics2D g) {
// grid background
g.setColor(Config.gridColor);
g.fill(Config.gridRect);
 
// the blocks dropped in the grid
Line 192 ⟶ 201:
 
// the borders of grid and preview panel
g.setStroke(Config.largeStroke);
g.setColor(Config.gridBorderColor);
g.draw(Config.gridRect);
g.draw(Config.previewRect);
 
// scoreboard
int x = Config.scoreX;
int y = Config.scoreY;
g.setColor(Config.textColor);
g.setFont(Config.smallFont);
g.drawString(format("hiscore %6d", scoreboard.getTopscore()), x, y);
g.drawString(format("level %6d", scoreboard.getLevel()), x, y + 30);
Line 208 ⟶ 217:
 
// preview
int idxminX = preSelectedShape.ordinal()5, minY = 5, maxX = 0, maxY = 0;
for (int[] p : preSelectedShapenextShape.shapes[idx]pos) {
int offsetX = Config.previewOffets[idx][0] + 15 * blockSize;
int offsetY = Config.previewOffets[idx][1] +minX 2= *min(minX, blockSizep[0]);
minY = min(minY, p[1]);
 
g.translate maxX = max(offsetXmaxX, offsetYp[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 : preSelectedShape.shapes[idx])
for drawSquare(g, idx, pint[1], p[0] : nextShape.shape);
drawSquare(g, nextShape.ordinal(), p[1], p[0]);
 
g.translate(-offsetXcx, -offsetYcy);
}
 
Line 278 ⟶ 291:
}
 
void move(ShapeDir s, int xIncr, int yIncrdir) {
fallingShapeRow += yIncrdir.y;
fallingShapeCol += xIncrdir.x;
}
 
boolean canMove(Shape s, intDir xIncr, int yIncrdir) {
for (int[] p : s.pos) {
int newCol = fallingShapeCol + xIncrdir.x + p[0];
int newRow = fallingShapeRow + yIncrdir.y + p[1];
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, SShape-1}, Straight{0, TShape0}, Square{-1, LShape0}, JShape;{-1, 1}}),
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 466 ⟶ 490:
void reset() {
for (int i = 0; i < pos.length; i++) {
pos[i] = shapes[ordinal()]shape[i].clone();
}
}
 
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};
 
// used for centering shapes in the preview pane
final static int[][] previewOffets = {{16, 15}, {-15, 15}, {0, 0},
{0, 0}, {-15, 5}, {16, 15}, {-15, 15}};
 
final static Font mainFont = new Font("Monospaced", Font.BOLD, 48);
Line 494 ⟶ 509:
 
final static Dimension dim = new Dimension(640, 640);
 
final static Rectangle previewRectgridRect = new Rectangle(38746, 47, 200308, 200517);
final static Rectangle titleRectpreviewRect = new Rectangle(100387, 8547, 252200, 100200);
final static Rectangle clickRecttitleRect = new Rectangle(50100, 37585, 252, 40100);
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 Rectangleint gridRectpreviewCenterY = new Rectangle(46, 47, 308, 517)97;
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);
}</langsyntaxhighlight>
9,476

edits