用Java写一个俄罗斯方块
时间: 2023-08-23 09:05:53 浏览: 95
好的,以下是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.JPanel;
public class Tetris extends JPanel implements Runnable {
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(2, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
{ new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
{ new Point(2, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
}
};
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 boolean gameOver = false;
private Thread gameThread;
private final Object pauseLock = new Object();
private boolean isPaused = false;
public Tetris() {
setPreferredSize(new Dimension(200, 400));
setBackground(Color.BLACK);
well = new Color[12][24];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 23; j++) {
if (i == 0 || i == 11 || j == 22) {
well[i][j] = Color.GRAY;
} else {
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);
}
currentPiece = nextPieces.get(0);
nextPieces.remove(0);
}
private boolean collidesAt(int x, int y, int rotation) {
for (Point p : Tetraminos[currentPiece][rotation]) {
if (well[p.x + x][p.y + y] != 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 (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
pieceOrigin.y += 1;
} else {
fixToWell();
}
repaint();
}
public void fixToWell() {
for (Point p : Tetraminos[currentPiece][rotation]) {
well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
}
clearRows();
newPiece();
if (collidesAt(pieceOrigin.x, pieceOrigin.y, rotation)) {
gameOver = true;
pause();
}
}
public void deleteRow(int row) {
for (int j = row-1; j > 0; j--) {
for (int i = 1; i < 11; i++) {
well[i][j+1] = well[i][j];
}
}
}
public void clearRows() {
boolean gap;
int numClears = 0;
for (int j = 21; j > 0; j--) {
gap = false;
for (int i = 1; i < 11; i++) {
if (well[i][j] == 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;
}
}
public void drawPiece(Graphics g) {
g.setColor(tetraminoColors[currentPiece]);
for (Point p : Tetraminos[currentPiece][rotation]) {
g.fillRect((p.x + pieceOrigin.x) * 26,
(p.y + pieceOrigin.y) * 26,
25, 25);
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// Draw the well
g.fillRect(0, 0, 26*12, 26*23);
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 23; j++) {
g.setColor(well[i][j]);
g.fillRect(26*i, 26*j, 25, 25);
}
}
// Display the score
g.setColor(Color.WHITE);
g.drawString("" + score, 19*12, 25);
// Draw the currently falling piece
drawPiece(g);
}
public void pause() {
isPaused = true;
}
public void resume() {
synchronized (pauseLock) {
isPaused = false;
pauseLock.notifyAll();
}
}
public void run() {
while (!gameOver) {
synchronized (pauseLock) {
while (isPaused) {
try {
pauseLock.wait();
} catch (InterruptedException e) {
}
}
}
dropDown();
try {
Thread.sleep(400);
} catch ( InterruptedException e ) {}
}
}
public static void main(String[] args) {
Tetris game = new Tetris();
JFrame f = new JFrame("Tetris");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(12*26+10, 26*23+25);
f.add(game);
f.setVisible(true);
game.start();
}
public void start() {
gameOver = false;
score = 0;
newPiece();
gameThread = new Thread(this);
gameThread.start();
}
}
```
这个示例中,我们使用了Java的Swing来实现游戏界面,使用了多线程来控制游戏的运行。具体实现中,我们定义了方块的类型和颜色,以及游戏区域。游戏区域由一个二维数组表示,每一种方块都是由自己所在位置和相对位置组成的一个数组,通过不停地调整方块的位置和旋转来达到消除行的目的。
阅读全文