Anonymous user
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;
int[][] grid;
Shape
Shape preSelectedShape;
int
int
int
int
int
int topMargin = Config.topMargin;
int leftMargin = Config.leftMargin;
Thread fallingThread;
Scoreboard scoreboard;
public Tetris() {
setPreferredSize(
setBackground(
setFocusable(true);
scoreboard = new Scoreboard();
Line 75 ⟶ 61:
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 ⟶ 78:
if (!fastDown) {
fastDown = true;
while (canMove(
move(
repaint();
}
Line 110 ⟶ 96:
void selectShape() {
preSelectedShape = shapes[(int) (Math.random() * shapes.length)];
if (fallingShape !=
fallingShape.reset();
}
Line 155 ⟶ 142:
if (!scoreboard.isGameOver()) {
if (canMove(
move(
} else {
shapeHasLanded();
Line 166 ⟶ 153:
void drawStartScreen(Graphics2D g) {
g.setFont(
g.setColor(
g.
g.
g.setColor(
g.drawString("Tetris",
g.setFont(Config.smallFont);
g.drawString("click to start",
}
void drawSquare(Graphics2D g,
g.
g.fillRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
g.
g.setColor(Config.squareBorder);
g.drawRect(leftMargin + c * blockSize, topMargin + r * blockSize,
blockSize, blockSize);
Line 192 ⟶ 178:
void drawUI(Graphics2D g) {
g.setColor(
g.fill(Config.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.
g.setColor(Config.gridBorderColor);
g.draw(Config.gridRect);
g.draw(Config.previewRect);
// scoreboard
int
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);
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) {
for (int[] p :
drawSquare(g,
}
Line 245 ⟶ 240:
drawFallingShape(g);
}
}
Line 265 ⟶ 258:
for (int[] p : pos) {
int newCol =
int newRow =
if (grid[newRow][newCol] != EMPTY) {
return false;
Line 286 ⟶ 279:
void move(Shape s, int xIncr, int yIncr) {
}
boolean canMove(Shape s, int xIncr, int yIncr) {
for (int[] p : s.pos) {
int newCol =
int newRow =
if (grid[newRow][newCol] != EMPTY)
return false;
Line 301 ⟶ 294:
void shapeHasLanded() {
addShape(
if (
scoreboard.setGameOver();
scoreboard.setTopscore();
Line 339 ⟶ 332:
void addShape(Shape s) {
for (int[] p : s.pos)
grid[
}
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>
|