温馨提示×

java实现简单的俄罗斯方块

小云
125
2023-08-10 14:06:54
栏目: 编程语言

以下是使用Java实现的简单俄罗斯方块游戏的示例代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tetris extends JFrame {
private final int BOARD_WIDTH = 10;
private final int BOARD_HEIGHT = 22;
private Timer timer;
private boolean isFallingFinished = false;
private boolean isStarted = false;
private boolean isPaused = false;
private int numLinesRemoved = 0;
private int curX = 0;
private int curY = 0;
private JLabel statusBar;
private Shape curPiece;
private Shape.Tetrominoes[] board;
public Tetris() {
initUI();
}
private void initUI() {
statusBar = new JLabel(" 0");
add(statusBar, BorderLayout.SOUTH);
board = new Shape.Tetrominoes[BOARD_WIDTH * BOARD_HEIGHT];
addKeyListener(new TAdapter());
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(400, new GameCycle());
timer.start();
clearBoard();
}
private int squareWidth() {
return (int) getContentPane().getSize().getWidth() / BOARD_WIDTH;
}
private int squareHeight() {
return (int) getContentPane().getSize().getHeight() / BOARD_HEIGHT;
}
private Shape.Tetrominoes shapeAt(int x, int y) {
return board[(y * BOARD_WIDTH) + x];
}
private void start() {
if (isPaused) {
return;
}
isStarted = true;
isFallingFinished = false;
numLinesRemoved = 0;
clearBoard();
newPiece();
timer.start();
}
private void pause() {
if (!isStarted) {
return;
}
isPaused = !isPaused;
if (isPaused) {
timer.stop();
statusBar.setText(" paused");
} else {
timer.start();
statusBar.setText(String.valueOf(numLinesRemoved));
}
repaint();
}
private void doDrawing(Graphics g) {
Dimension size = getSize();
int boardTop = (int) size.getHeight() - BOARD_HEIGHT * squareHeight();
for (int i = 0; i < BOARD_HEIGHT; ++i) {
for (int j = 0; j < BOARD_WIDTH; ++j) {
Shape.Tetrominoes shape = shapeAt(j, BOARD_HEIGHT - i - 1);
if (shape != Shape.Tetrominoes.NoShape) {
drawSquare(g, j * squareWidth(), boardTop + i * squareHeight(), shape);
}
}
}
if (curPiece.getShape() != Shape.Tetrominoes.NoShape) {
for (int i = 0; i < 4; ++i) {
int x = curX + curPiece.x(i);
int y = curY - curPiece.y(i);
drawSquare(g, x * squareWidth(), boardTop + (BOARD_HEIGHT - y - 1) * squareHeight(), curPiece.getShape());
}
}
}
private void dropDown() {
int newY = curY;
while (newY > 0) {
if (!tryMove(curPiece, curX, newY - 1)) {
break;
}
--newY;
}
pieceDropped();
}
private void oneLineDown() {
if (!tryMove(curPiece, curX, curY - 1)) {
pieceDropped();
}
}
private void removeFullLines() {
int numFullLines = 0;
for (int i = BOARD_HEIGHT - 1; i >= 0; --i) {
boolean lineIsFull = true;
for (int j = 0; j < BOARD_WIDTH; ++j) {
if (shapeAt(j, i) == Shape.Tetrominoes.NoShape) {
lineIsFull = false;
break;
}
}
if (lineIsFull) {
++numFullLines;
for (int k = i; k < BOARD_HEIGHT - 1; ++k) {
for (int j = 0; j < BOARD_WIDTH; ++j) {
board[(k * BOARD_WIDTH) + j] = shapeAt(j, k + 1);
}
}
}
}
if (numFullLines > 0) {
numLinesRemoved += numFullLines;
statusBar.setText(String.valueOf(numLinesRemoved));
isFallingFinished = true;
curPiece.setShape(Shape.Tetrominoes.NoShape);
repaint();
}
}
private void newPiece() {
curPiece = new Shape();

0