java俄罗斯方块游戏代码
时间: 2023-07-10 20:34:59 浏览: 98
以下是一个简单的 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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Tetris extends JPanel implements ActionListener {
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<>();
private long score;
private Color[][] well;
private Timer timer;
private final int ROWS = 22;
private final int COLUMNS = 10;
public void actionPerformed(ActionEvent e) {
if (fallingFinished) {
newPiece();
fallingFinished = false;
} else {
oneLineDown();
}
}
private int squareWidth() { return (int) getSize().getWidth() / COLUMNS; }
private int squareHeight() { return (int) getSize().getHeight() / ROWS; }
private Color shapeColor(int shape) { return tetraminoColors[shape - 1]; }
public void start() {
score = 0;
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();
timer.start();
}
private void newPiece() {
pieceOrigin = new Point(5, 2);
rotation = 0;
if (nextPieces.isEmpty()) {
Collections.addAll(nextPieces, 1, 2, 3, 4, 5, 6, 7);
Collections.shuffle(nextPieces, new Random());
}
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.y + y][p.x + x] != Color.BLACK) {
return true;
}
}
return false;
}
private void rotate(int i) {
int newRotation = (rotation + i) % 4;
if (newRotation < 0) {
newRotation = 3;
}
if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
rotation = newRotation;
}
repaint();
}
private void move(int i) {
if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
pieceOrigin.x += i;
}
repaint();
}
private void dropDown() {
if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
pieceOrigin.y += 1;
} else {
fixToWell();
}
repaint();
}
private void oneLineDown() {
if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
pieceOrigin.y += 1;
} else {
fixToWell();
if (checkGameOver()) {
timer.stop();
JOptionPane.showMessageDialog(this, "Game Over! Your score: " + score);
}
}
repaint();
}
private void fixToWell() {
for (Point p : Tetraminos[currentPiece][rotation]) {
well[pieceOrigin.y + p.y][pieceOrigin.x + p.x] = shapeColor(currentPiece);
}
clearRows();
newPiece();
}
private boolean checkGameOver() {
for (int i = 0; i < COLUMNS; i++) {
if (well[2][i] != Color.BLACK) {
return true;
}
}
return false;
}
private void clearRows() {
boolean gap;
int numClears = 0;
for (int i = ROWS - 1; i >= 0; i--) {
gap = false;
for (int j = 0; j < COLUMNS; j++) {
if (well[i][j] == Color.BLACK) {
gap = true;
break;
}
}
if (!gap) {
removeRow(i);
i++;
numClears++;
}
}
switch (numClears) {
case 1:
score += 100;
break;
case 2:
score += 300;
break;
case 3:
score += 500;
break;
case 4:
score += 800;
break;
}
}
private void removeRow(int row) {
for (int j = 0; j < COLUMNS; j++) {
for (int i = row-1; i >= 0; i--) {
well[i+1][j] = well[i][j];
}
}
}
@Override
public void paint(Graphics g) {
g.fillRect(0, 0, getWidth(), getHeight());
for (Point p : Tetraminos[currentPiece][rotation]) {
drawSquare(g, (p.x + pieceOrigin.x) * squareWidth(),
(p.y + pieceOrigin.y) * squareHeight(),
shapeColor(currentPiece));
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
drawSquare(g, j * squareWidth(), i * squareHeight(), well[i][j]);
}
}
}
private void drawSquare(Graphics g, int x, int y, Color color) {
g.setColor(color);
g.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2);
g.setColor(color.brighter());
g.drawLine(x, y + squareHeight() - 1, x, y);
g.drawLine(x, y, x + squareWidth() - 1, y);
g.setColor(color.darker());
g.drawLine(x + 1, y + squareHeight() - 1,
x + squareWidth() - 1, y + squareHeight() - 1);
g.drawLine(x + squareWidth() - 1,
y + squareHeight() - 1,
x + squareWidth() - 1, y + 1);
}
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);
f.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
switch (e.getKeyCode()) {
case 37:
game.move(-1);
break;
case 38:
game.rotate(-1);
break;
case 39:
game.move(+1);
break;
case 40:
game.dropDown();
game.score += 1;
break;
case 32:
game.fallingFinished = true;
game.score += 1;
break;
}
}
});
f.setVisible(true);
game.start();
}
private boolean fallingFinished = false;
private void init() {
timer = new Timer(400, this);
nextPieces.add(1);
nextPieces.add(2);
nextPieces.add(3);
nextPieces.add(4);
nextPieces.add(5);
nextPieces.add(6);
nextPieces.add(7);
Collections.shuffle(nextPieces);
}
}
```
这个代码示例使用 Java Swing 组件来实现基本的图形化用户界面,同时使用计时器来控制方块的下落。您可以根据需要对其进行修改和定制。
阅读全文