写一个java俄罗斯方块
时间: 2023-06-13 15:07:18 浏览: 60
以下是一个简单的 Java 俄罗斯方块游戏的实现:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tetris extends JPanel {
private static final long serialVersionUID = -8715353373678321308L;
private final Point[][][] Tetraminos = {
// I-Piece
{
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
{ new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
{ new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
},
// J-Piece
{
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
{ new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
{ new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
},
// L-Piece
{
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
{ new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
{ new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
},
// O-Piece
{
{ new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
{ new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
{ new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
{ new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
},
// S-Piece
{
{ new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
{ new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
{ new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
{ new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
},
// T-Piece
{
{ new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
{ new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
{ new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
},
// Z-Piece
{
{ new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
{ new Point(1, 1), new Point(0, 2), new Point(1, 2), new Point(0, 3) },
{ new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
{ new Point(1, 1), new Point(0, 2), new Point(1, 2), new Point(0, 3) }
}
};
private final Color[] tetraminoColors = {
Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red
};
private Point pieceOrigin;
private int currentPiece;
private int rotation;
private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
private long score;
private Color[][] well;
private final int ROWS = 20;
private final int COLUMNS = 10;
private final int BLOCK_SIZE = 30;
private boolean gameOver = false;
private JFrame parent;
public Tetris(JFrame parent) {
this.parent = parent;
setPreferredSize(new Dimension(COLUMNS * BLOCK_SIZE, ROWS * BLOCK_SIZE));
setBackground(Color.BLACK);
well = new Color[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
well[i][j] = Color.BLACK;
}
}
newPiece();
}
public void newPiece() {
pieceOrigin = new Point(5, 2);
rotation = 0;
if (nextPieces.isEmpty()) {
Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
Collections.shuffle(nextPieces, new Random());
}
currentPiece = nextPieces.get(0);
nextPieces.remove(0);
}
public boolean collidesAt(int x, int y, int rotation) {
for (Point p : Tetraminos[currentPiece][rotation]) {
if (well[p.y + y][p.x + x] != Color.BLACK) {
return true;
}
}
return false;
}
public void rotate(int i) {
int newRotation = (rotation + i) % 4;
if (newRotation < 0) {
newRotation = 3;
}
if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
rotation = newRotation;
}
repaint();
}
public void move(int i) {
if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
pieceOrigin.x += i;
}
repaint();
}
public void dropDown() {
if (gameOver) {
return;
}
if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
pieceOrigin.y += 1;
} else {
fixToWell();
}
repaint();
}
public void fixToWell() {
for (Point p : Tetraminos[currentPiece][rotation]) {
well[pieceOrigin.y + p.y][pieceOrigin.x + p.x] = tetraminoColors[currentPiece];
}
clearRows();
newPiece();
if (collidesAt(pieceOrigin.x, pieceOrigin.y, rotation)) {
gameOver = true;
parent.setTitle("Game Over");
}
}
public void deleteRow(int row) {
for (int j = row-1; j > 0; j--) {
for (int i = 0; i < COLUMNS; i++) {
well[j+1][i] = well[j][i];
}
}
}
public void clearRows() {
boolean gap;
int numClears = 0;
for (int j = ROWS-1; j >= 0; j--) {
gap = false;
for (int i = 0; i < COLUMNS; i++) {
if (well[j][i] == Color.BLACK) {
gap = true;
break;
}
}
if (!gap) {
deleteRow(j);
j += 1;
numClears += 1;
}
}
switch (numClears) {
case 1:
score += 100;
break;
case 2:
score += 300;
break;
case 3:
score += 500;
break;
case 4:
score += 800;
break;
}
}
private void drawPiece(Graphics g) {
g.setColor(tetraminoColors[currentPiece]);
for (Point p : Tetraminos[currentPiece][rotation]) {
g.fillRect((p.x + pieceOrigin.x) * BLOCK_SIZE,
(p.y + pieceOrigin.y) * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawPiece(g);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
g.setColor(well[i][j]);
g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Tetris");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(12 * 30 + 10, 26 * 30 + 25);
f.setVisible(true);
final Tetris game = new Tetris(f);
game.init();
f.add(game);
new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
game.dropDown();
} catch ( InterruptedException e ) {}
}
}
}.start();
}
}
```
这只是一个简单的实现,您可以根据需要进行修改和扩展。
阅读全文