Tetris/Java: Difference between revisions

→‎{{header|Java}}: cleaned up code a bit
(→‎{{headerJava}}: added Java)
 
(→‎{{header|Java}}: cleaned up code a bit)
Line 10:
public static final int EMPTY = -1;
public static final int BORDER = -2;
 
final Color[] colors = {Color.green, Color.red, Color.blue, Color.pink,
Color.orange, Color.cyan, Color.magenta};
 
// for centering the shapes in the preview window
final int[][] previewOffets = {{16, 15}, {-15, 15}, {0, 0}, {0, 0},
{-15, 5}, {16, 15}, {-15, 15}};
 
int[][] grid;
 
Shape selectedShapefallingShape;
Shape preSelectedShape;
 
int// blockSizeposition =of 30;falling shape
int nRows = 18fallingShapeRow;
int nCols = 12fallingShapeCol;
 
int shapeRow;
int shapeColblockSize = Config.blockSize;
int topMarginnRows = 50Config.nRows;
int leftMarginnCols = 20Config.nCols;
int topMargin = Config.topMargin;
int leftMargin = Config.leftMargin;
 
Thread fallingThread;
Scoreboard scoreboard;
 
Font smallFont;
Rectangle gridRect, previewRect;
 
public Tetris() {
setPreferredSize(new Dimension(640, 640)Config.dim);
setBackground(new Color(0xDDEEFF)Config.bgColor);
setFont(new Font("Monospaced", Font.BOLD, 48));
setFocusable(true);
 
smallFont = getFont().deriveFont(Font.BOLD, 18);
 
gridRect = new Rectangle(46, 47, 308, 517);
previewRect = new Rectangle(387, 47, 200, 200);
 
scoreboard = new Scoreboard();
Line 75 ⟶ 61:
 
case KeyEvent.VK_UP:
if (canRotate(selectedShapefallingShape))
rotate(selectedShapefallingShape);
break;
 
case KeyEvent.VK_LEFT:
if (canMove(selectedShapefallingShape, -1, 0))
move(selectedShapefallingShape, -1, 0);
break;
 
case KeyEvent.VK_RIGHT:
if (canMove(selectedShapefallingShape, 1, 0))
move(selectedShapefallingShape, 1, 0);
break;
 
Line 92 ⟶ 78:
if (!fastDown) {
fastDown = true;
while (canMove(selectedShapefallingShape, 0, 1)) {
move(selectedShapefallingShape, 0, 1);
repaint();
}
Line 110 ⟶ 96:
 
void selectShape() {
shapeRowfallingShapeRow = 1;
shapeColfallingShapeCol = 5;
selectedShapefallingShape = preSelectedShape;
preSelectedShapeShape[] shapes = Shape.values()[(int) (Math.random() * colors.length)];
preSelectedShape = shapes[(int) (Math.random() * shapes.length)];
if (selectedShape != null)
if (fallingShape != selectedShape.reset(null);
fallingShape.reset();
}
 
Line 155 ⟶ 142:
 
if (!scoreboard.isGameOver()) {
if (canMove(selectedShapefallingShape, 0, 1)) {
move(selectedShapefallingShape, 0, 1);
} else {
shapeHasLanded();
Line 166 ⟶ 153:
 
void drawStartScreen(Graphics2D g) {
g.setFont(getFont()Config.mainFont);
 
g.setColor(new Color(0xFFFFFF)Config.titlebgColor);
g.fillRectfill(leftMargin + 80, topMargin + 35, 252, 100Config.titleRect);
g.fillRectfill(leftMargin + 30, topMargin + 325, 252, 40Config.clickRect);
 
g.setColor(ColorConfig.blacktextColor);
g.drawString("Tetris", leftMargin + 110Config.titleX, topMargin + 100Config.titleY);
 
g.setFont(Config.smallFont);
g.drawString("click to start", leftMargin + 100Config.clickX, topMargin + 350Config.clickY);
}
 
void drawSquare(Graphics2D g, Colorint colorcolorIndex, int r, int c) {
g.setStrokesetColor(new BasicStroke(2)Config.colors[colorIndex]);
 
g.setColor(color);
g.fillRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
 
g.setColorsetStroke(ColorConfig.whitesmallStroke);
g.setColor(Config.squareBorder);
g.drawRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
Line 192 ⟶ 178:
 
void drawUI(Graphics2D g) {
g.setStroke(new// BasicStroke(5));grid background
g.setColor(new Color(0xBECFEA)Config.gridColor);
g.fill(Config.gridRect);
 
g.setColor(new Color(0x7788AA));
g.draw(gridRect);
g.draw(previewRect);
 
// the blocks dropped in the grid
for (int r = 0; r < nRows; r++) {
for (int c = 0; c < nCols; c++) {
int ididx = grid[r][c];
if (ididx > EMPTY)
drawSquare(g, colors[id]idx, r, c);
}
}
 
// the borders of grid and preview panel
g.setColor(Color.black);
g.setFontsetStroke(smallFontConfig.largeStroke);
g.setColor(Config.gridBorderColor);
g.drawString(format("hiscore %6d", scoreboard.getTopscore()),400, 330);
g.draw(Config.gridRect);
g.drawString(format("level %6d", scoreboard.getLevel()), 400, 360);
g.draw(Config.previewRect);
g.drawString(format("lines %6d", scoreboard.getLines()), 400, 390);
g.drawString(format("score %6d", scoreboard.getScore()), 400, 420);
}
 
// scoreboard
void drawPreview(Graphics2D g) {
int ordx = preSelectedShapeConfig.ordinal()scoreX;
int y = Config.scoreY;
g.translate(previewOffets[ord][0], previewOffets[ord][1]);
g.setColor(Config.textColor);
for (int[] p : preSelectedShape.shapes[ord]) {
g.setFont(Config.smallFont);
drawSquare(g, colors[ord], 2 + p[1], 15 + p[0]);
g.drawString(format("hiscore %6d", scoreboard.getTopscore()), x, y);
}
g.drawString(format("level %6d", scoreboard.getLevel()), x, y + 30);
g.translate(-previewOffets[ord][0], -previewOffets[ord][1]);
g.drawString(format("lines %6d", scoreboard.getLines()), x, y + 60);
g.drawString(format("score %6d", scoreboard.getScore()), x, y + 90);
 
// preview
int idx = preSelectedShape.ordinal();
int offsetX = Config.previewOffets[idx][0] + 15 * blockSize;
int offsetY = Config.previewOffets[idx][1] + 2 * blockSize;
 
g.translate(offsetX, offsetY);
 
for (int[] p : preSelectedShape.shapes[idx])
drawSquare(g, idx, p[1], p[0]);
 
g.translate(-offsetX, -offsetY);
}
 
void drawFallingShape(Graphics2D g) {
Colorint cidx = colors[selectedShapefallingShape.ordinal()];
for (int[] p : selectedShapefallingShape.pos)
drawSquare(g, cidx, shapeRowfallingShapeRow + p[1], shapeColfallingShapeCol + p[0]);
}
 
Line 245 ⟶ 240:
drawFallingShape(g);
}
 
drawPreview(g);
}
 
Line 265 ⟶ 258:
 
for (int[] p : pos) {
int newCol = shapeColfallingShapeCol + p[0];
int newRow = shapeRowfallingShapeRow + p[1];
if (grid[newRow][newCol] != EMPTY) {
return false;
Line 286 ⟶ 279:
 
void move(Shape s, int xIncr, int yIncr) {
shapeRowfallingShapeRow += yIncr;
shapeColfallingShapeCol += xIncr;
}
 
boolean canMove(Shape s, int xIncr, int yIncr) {
for (int[] p : s.pos) {
int newCol = shapeColfallingShapeCol + xIncr + p[0];
int newRow = shapeRowfallingShapeRow + yIncr + p[1];
if (grid[newRow][newCol] != EMPTY)
return false;
Line 301 ⟶ 294:
 
void shapeHasLanded() {
addShape(selectedShapefallingShape);
if (shapeRowfallingShapeRow < 2) {
scoreboard.setGameOver();
scoreboard.setTopscore();
Line 339 ⟶ 332:
void addShape(Shape s) {
for (int[] p : s.pos)
grid[shapeRowfallingShapeRow + p[1]][shapeColfallingShapeCol + p[0]] = s.ordinal();
}
 
Line 487 ⟶ 480:
{{-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);
final static Font smallFont = mainFont.deriveFont(Font.BOLD, 18);
 
final static Dimension dim = new Dimension(640, 640);
 
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 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 Stroke largeStroke = new BasicStroke(5);
final static Stroke smallStroke = new BasicStroke(2);
 
final static Color squareBorder = Color.white;
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);
}</lang>
Anonymous user