Java俄罗斯方块代码
时间: 2023-08-29 18:12:17 浏览: 97
以下是一个简单的Java俄罗斯方块游戏示例代码:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Tetris extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
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 nextPiece;
private Point[][] currentRotation;
private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
private long score;
private Color[][] well;
// Creates a border around the well and initializes the dropping piece
private void init() {
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();
}
// Put a new, random piece into the dropping position
public void newPiece() {
pieceOrigin = new Point(5, 2);
if (nextPieces.isEmpty()) {
Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
Collections.shuffle(nextPieces);
}
currentPiece = nextPieces.get(0);
nextPieces.remove(0);
nextPiece = nextPieces.get(0);
currentRotation = Tetraminos[currentPiece][0];
// If the game is over, reset the score and well
if (collision()) {
score = 0;
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;
}
}
}
}
}
// Collision test for the dropping piece
private boolean collision() {
for (Point p : currentRotation) {
if (well[p.x + pieceOrigin.x][p.y + pieceOrigin.y] != Color.BLACK) {
return true;
}
}
return false;
}
// Rotate the piece clockwise or counterclockwise
public void rotatePiece(int i) {
int newRotation = (i == 1) ? 0 : 3;
Point[][] rotated = Tetraminos[currentPiece][newRotation];
if (collidesAt(rotated, pieceOrigin.x, pieceOrigin.y)) {
// Do nothing if the piece is rotating into a collision
return;
}
currentRotation = rotated;
}
// Move the piece left or right
public void movePiece(int i) {
if (!collidesAt(currentRotation, pieceOrigin.x + i, pieceOrigin.y)) {
pieceOrigin.x += i;
}
}
// Drops the piece one line or fixes it to the well if it can't drop
public void dropPiece() {
if (!collidesAt(currentRotation, pieceOrigin.x, pieceOrigin.y + 1)) {
pieceOrigin.y += 1;
} else {
fixToWell();
}
}
// Make the dropping piece part of the well, so it is available for
// collision detection.
public void fixToWell() {
for (Point p : currentRotation) {
well[p.x + pieceOrigin.x][p.y + pieceOrigin.y] = tetraminoColors[currentPiece];
}
clearRows();
newPiece();
}
public boolean collidesAt(Point[][] piece, int x, int y) {
for (Point p : piece) {
if (well[p.x + x][p.y + y] != Color.BLACK) {
return true;
}
}
return false;
}
// Clear completed rows from the field and award score according to
// the number of simultaneously cleared rows.
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) {
removeRow(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;
}
}
// Remove a given row from the field
public void removeRow(int row) {
for (int j = row-1; j > 0; j--) {
for (int i = 1; i < 11; i++) {
well[i][j+1] = well[i][j];
}
}
}
@Override
public void paintComponent(Graphics g)
{
// Paint 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
for (Point p : currentRotation) {
g.setColor(tetraminoColors[currentPiece]);
g.fillRect((p.x + pieceOrigin.x) * 26,
(p.y + pieceOrigin.y) * 26,
25, 25);
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Tetris");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(12*26+10, 26*23+25);
f.setVisible(true);
final Tetris game = new Tetris();
game.init();
f.add(game);
// Keyboard controls
f.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
switch (e.getKeyCode()) {
case java.awt.event.KeyEvent.VK_UP:
game.rotatePiece(1);
break;
case java.awt.event.KeyEvent.VK_DOWN:
game.dropPiece();
game.score += 1;
break;
case java.awt.event.KeyEvent.VK_LEFT:
game.movePiece(-1);
break;
case java.awt.event.KeyEvent.VK_RIGHT:
game.movePiece(+1);
break;
case java.awt.event.KeyEvent.VK_SPACE:
game.dropPiece();
game.score += 2;
break;
}
}
});
// Make the falling piece drop every second
new Timer(1000, game).start();
}
@Override
public void actionPerformed(ActionEvent e) {
dropPiece();
}
}
```
这个示例代码实现了一个基本的俄罗斯方块游戏,包括旋转、移动、下落、消除、得分等功能。您可以尝试运行它并自行扩展改进。
阅读全文