用Java写一个俄罗斯方块游戏
时间: 2023-09-24 19:02:40 浏览: 42
实现俄罗斯方块游戏可以使用Java的AWT或者Swing图形库。下面是一个简单的实现,仅供参考:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tetris extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 10;
private final int HEIGHT = 20;
private final int BLOCK_SIZE = 30;
private int[][] board = new int[HEIGHT][WIDTH];
private int[][][] shapes = new int[][][] {
{{1, 1, 1}, {0, 1, 0}},
{{0, 2, 2}, {2, 2, 0}},
{{3, 3, 0}, {0, 3, 3}},
{{4, 0, 0}, {4, 4, 4}},
{{0, 0, 5, 0}, {0, 5, 5, 5}},
{{6, 6}, {6, 6}}
};
private int[][] currentShape;
private int currentX = 0;
private int currentY = 0;
private boolean gameOver = false;
private Random random = new Random();
public Tetris() {
setPreferredSize(new java.awt.Dimension(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE));
setBackground(Color.BLACK);
addKeyListener(this);
newShape();
}
private void newShape() {
int index = random.nextInt(shapes.length);
currentShape = shapes[index];
currentX = WIDTH / 2 - currentShape[0].length / 2;
currentY = 0;
if (isColliding()) {
gameOver = true;
}
}
private boolean isColliding() {
for (int i = 0; i < currentShape.length; i++) {
for (int j = 0; j < currentShape[0].length; j++) {
if (currentShape[i][j] != 0) {
int x = currentX + j;
int y = currentY + i;
if (x < 0 || x >= WIDTH || y >= HEIGHT || board[y][x] != 0) {
return true;
}
}
}
}
return false;
}
private void drop() {
while (!isColliding()) {
currentY++;
}
currentY--;
placeCurrentShape();
newShape();
}
private void placeCurrentShape() {
for (int i = 0; i < currentShape.length; i++) {
for (int j = 0; j < currentShape[0].length; j++) {
if (currentShape[i][j] != 0) {
int x = currentX + j;
int y = currentY + i;
board[y][x] = currentShape[i][j];
}
}
}
clearFullRows();
}
private void clearFullRows() {
for (int i = HEIGHT - 1; i >= 0; i--) {
boolean full = true;
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
full = false;
break;
}
}
if (full) {
for (int k = i; k > 0; k--) {
System.arraycopy(board[k - 1], 0, board[k], 0, WIDTH);
}
i++;
}
}
}
private void left() {
currentX--;
if (isColliding()) {
currentX++;
}
}
private void right() {
currentX++;
if (isColliding()) {
currentX--;
}
}
private void rotate() {
int[][] rotated = new int[currentShape[0].length][currentShape.length];
for (int i = 0; i < currentShape.length; i++) {
for (int j = 0; j < currentShape[0].length; j++) {
rotated[j][currentShape.length - i - 1] = currentShape[i][j];
}
}
if (!isColliding(rotated, currentX, currentY)) {
currentShape = rotated;
}
}
private boolean isColliding(int[][] shape, int x, int y) {
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[0].length; j++) {
if (shape[i][j] != 0) {
int px = x + j;
int py = y + i;
if (px < 0 || px >= WIDTH || py >= HEIGHT || board[py][px] != 0) {
return true;
}
}
}
}
return false;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] != 0) {
g.setColor(getColor(board[i][j]));
g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
if (!gameOver) {
g.setColor(getColor(currentShape));
for (int i = 0; i < currentShape.length; i++) {
for (int j = 0; j < currentShape[0].length; j++) {
if (currentShape[i][j] != 0) {
g.fillRect((currentX + j) * BLOCK_SIZE, (currentY + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
} else {
g.setColor(Color.WHITE);
g.drawString("GAME OVER", BLOCK_SIZE * 4, BLOCK_SIZE * 10);
}
}
private Color getColor(int shape) {
switch (shape) {
case 1: return Color.RED;
case 2: return Color.GREEN;
case 3: return Color.BLUE;
case 4: return Color.YELLOW;
case 5: return Color.ORANGE;
case 6: return Color.MAGENTA;
default: return Color.WHITE;
}
}
private Color getColor(int[][] shape) {
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[0].length; j++) {
if (shape[i][j] != 0) {
return getColor(shape[i][j]);
}
}
}
return Color.WHITE;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Tetris");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new Tetris());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_DOWN:
drop();
break;
case KeyEvent.VK_UP:
rotate();
break;
default:
break;
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
}
```
这个实现仅提供了基本的游戏功能,还有很多可以改进的地方,比如增加计分系统、添加游戏音效等。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)