Tetris/Java: Difference between revisions
< Tetris
Content added Content deleted
m (change header) |
mNo edit summary |
||
Line 5: | Line 5: | ||
import static java.lang.String.format; |
import static java.lang.String.format; |
||
import java.util.Arrays; |
import java.util.Arrays; |
||
import java.util.Random; |
|||
import javax.swing.*; |
import javax.swing.*; |
||
Line 10: | Line 11: | ||
public static final int EMPTY = -1; |
public static final int EMPTY = -1; |
||
public static final int BORDER = -2; |
public static final int BORDER = -2; |
||
int[][] grid; |
|||
Shape fallingShape; |
Shape fallingShape; |
||
Line 20: | Line 19: | ||
int fallingShapeCol; |
int fallingShapeCol; |
||
int blockSize = Config.blockSize; |
final int blockSize = Config.blockSize; |
||
int nRows = Config.nRows; |
final int nRows = Config.nRows; |
||
int nCols = Config.nCols; |
final int nCols = Config.nCols; |
||
int topMargin = Config.topMargin; |
final int topMargin = Config.topMargin; |
||
int leftMargin = Config.leftMargin; |
final int leftMargin = Config.leftMargin; |
||
⚫ | |||
Thread fallingThread; |
Thread fallingThread; |
||
Scoreboard scoreboard; |
final Scoreboard scoreboard = new Scoreboard(); |
||
static final Random rand = new Random(); |
|||
public Tetris() { |
public Tetris() { |
||
Line 34: | Line 36: | ||
setFocusable(true); |
setFocusable(true); |
||
scoreboard = new Scoreboard(); |
|||
⚫ | |||
initGrid(); |
initGrid(); |
||
selectShape(); |
selectShape(); |
||
Line 100: | Line 100: | ||
fallingShape = preSelectedShape; |
fallingShape = preSelectedShape; |
||
Shape[] shapes = Shape.values(); |
Shape[] shapes = Shape.values(); |
||
preSelectedShape = shapes[ |
preSelectedShape = shapes[rand.nextInt(shapes.length)]; |
||
if (fallingShape != null) |
if (fallingShape != null) |
||
fallingShape.reset(); |
fallingShape.reset(); |
Revision as of 03:59, 15 April 2016
Code
<lang java>import java.awt.*; import java.awt.event.*; import static java.lang.String.format; import java.util.Arrays; import java.util.Random; import javax.swing.*;
public class Tetris extends JPanel implements Runnable {
public static final int EMPTY = -1; public static final int BORDER = -2;
Shape fallingShape; Shape preSelectedShape;
// position of falling shape int fallingShapeRow; int fallingShapeCol;
final int blockSize = Config.blockSize; final int nRows = Config.nRows; final int nCols = Config.nCols; final int topMargin = Config.topMargin; final int leftMargin = Config.leftMargin;
final int[][] grid = new int[nRows][nCols];
Thread fallingThread; final Scoreboard scoreboard = new Scoreboard(); static final Random rand = new Random();
public Tetris() { setPreferredSize(Config.dim); setBackground(Config.bgColor); setFocusable(true);
initGrid(); selectShape();
addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (scoreboard.isGameOver()) { startNewGame(); repaint(); } } });
addKeyListener(new KeyAdapter() { boolean fastDown;
@Override public void keyPressed(KeyEvent e) {
if (scoreboard.isGameOver()) return;
switch (e.getKeyCode()) {
case KeyEvent.VK_UP: if (canRotate(fallingShape)) rotate(fallingShape); break;
case KeyEvent.VK_LEFT: if (canMove(fallingShape, -1, 0)) move(fallingShape, -1, 0); break;
case KeyEvent.VK_RIGHT: if (canMove(fallingShape, 1, 0)) move(fallingShape, 1, 0); break;
case KeyEvent.VK_DOWN: if (!fastDown) { fastDown = true; while (canMove(fallingShape, 0, 1)) { move(fallingShape, 0, 1); repaint(); } shapeHasLanded(); } } repaint(); }
@Override public void keyReleased(KeyEvent e) { fastDown = false; } }); }
void selectShape() { fallingShapeRow = 1; fallingShapeCol = 5; fallingShape = preSelectedShape; Shape[] shapes = Shape.values(); preSelectedShape = shapes[rand.nextInt(shapes.length)]; if (fallingShape != null) fallingShape.reset(); }
void startNewGame() { stop(); initGrid(); selectShape(); scoreboard.reset(); (fallingThread = new Thread(this)).start(); }
void stop() { if (fallingThread != null) { Thread tmp = fallingThread; fallingThread = null; tmp.interrupt(); } }
void initGrid() { for (int r = 0; r < nRows; r++) { Arrays.fill(grid[r], EMPTY); for (int c = 0; c < nCols; c++) { if (c == 0 || c == nCols - 1 || r == nRows - 1) grid[r][c] = BORDER; } } }
@Override public void run() {
while (Thread.currentThread() == fallingThread) {
try { Thread.sleep(scoreboard.getSpeed()); } catch (InterruptedException ignored) { }
if (!scoreboard.isGameOver()) { if (canMove(fallingShape, 0, 1)) { move(fallingShape, 0, 1); } else { shapeHasLanded(); } repaint(); } } }
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); }
void drawUI(Graphics2D g) { // grid background g.setColor(Config.gridColor); 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 idx = grid[r][c]; if (idx > EMPTY) drawSquare(g, idx, r, c); } }
// 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); 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) { int idx = fallingShape.ordinal(); for (int[] p : fallingShape.pos) drawSquare(g, idx, fallingShapeRow + p[1], fallingShapeCol + p[0]); }
@Override public void paintComponent(Graphics gg) { super.paintComponent(gg); Graphics2D g = (Graphics2D) gg; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawUI(g);
if (scoreboard.isGameOver()) { drawStartScreen(g); } else { drawFallingShape(g); } }
boolean canRotate(Shape s) { if (s == Shape.Square) return false;
int[][] pos = new int[4][2]; for (int i = 0; i < pos.length; i++) { pos[i] = s.pos[i].clone(); }
for (int[] row : pos) { int tmp = row[0]; row[0] = row[1]; row[1] = -tmp; }
for (int[] p : pos) { int newCol = fallingShapeCol + p[0]; int newRow = fallingShapeRow + p[1]; if (grid[newRow][newCol] != EMPTY) { return false; } } return true; }
void rotate(Shape s) { if (s == Shape.Square) return;
for (int[] row : s.pos) { int tmp = row[0]; row[0] = row[1]; row[1] = -tmp; } }
void move(Shape s, int xIncr, int yIncr) { fallingShapeRow += yIncr; fallingShapeCol += xIncr; }
boolean canMove(Shape s, int xIncr, int yIncr) { for (int[] p : s.pos) { int newCol = fallingShapeCol + xIncr + p[0]; int newRow = fallingShapeRow + yIncr + p[1]; if (grid[newRow][newCol] != EMPTY) return false; } return true; }
void shapeHasLanded() { addShape(fallingShape); if (fallingShapeRow < 2) { scoreboard.setGameOver(); scoreboard.setTopscore(); stop(); } else { scoreboard.addLines(removeLines()); } selectShape(); }
int removeLines() { int count = 0; for (int r = 0; r < nRows - 1; r++) { for (int c = 1; c < nCols - 1; c++) { if (grid[r][c] == EMPTY) break; if (c == nCols - 2) { count++; removeLine(r); } } } return count; }
void removeLine(int line) { for (int c = 0; c < nCols; c++) grid[line][c] = EMPTY;
for (int c = 0; c < nCols; c++) { for (int r = line; r > 0; r--) grid[r][c] = grid[r - 1][c]; } }
void addShape(Shape s) { for (int[] p : s.pos) grid[fallingShapeRow + p[1]][fallingShapeCol + p[0]] = s.ordinal(); }
public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("Tetris"); f.setResizable(false); f.add(new Tetris(), BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); }); }
}
class Scoreboard {
static final int MAXLEVEL = 9;
private int level; private int lines; private int score; private int topscore; private boolean gameOver = true;
void reset() { setTopscore(); level = lines = score = 0; gameOver = false; }
void setGameOver() { gameOver = true; }
boolean isGameOver() { return gameOver; }
void setTopscore() { if (score > topscore) topscore = score; }
int getTopscore() { return topscore; }
int getSpeed() {
switch (level) { case 0: return 700; case 1: return 600; case 2: return 500; case 3: return 400; case 4: return 350; case 5: return 300; case 6: return 250; case 7: return 200; case 8: return 150; case 9: return 100; default: return 100; } }
void addScore(int sc) { score += sc; }
void addLines(int line) {
switch (line) { case 1: addScore(10); break; case 2: addScore(20); break; case 3: addScore(30); break; case 4: addScore(40); break; default: return; }
lines += line; if (lines > 10) addLevel(); }
void addLevel() { lines %= 10; if (level < MAXLEVEL) level++; }
int getLevel() { return level; }
int getLines() { return lines; }
int getScore() { return score; }
}
enum Shape {
ZShape, SShape, Straight, TShape, Square, LShape, JShape;
private Shape() { pos = new int[4][2]; reset(); }
void reset() { for (int i = 0; i < pos.length; i++) { pos[i] = shapes[ordinal()][i].clone(); } }
final int[][] pos;
final int[][][] shapes = { {{0, -1}, {0, 0}, {-1, 0}, {-1, 1}}, {{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); 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>