Java俄罗斯方块小游戏

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class TetrisGame extends JFrame implements ActionListener, KeyListener {
private final int BOARD_WIDTH = 10;
private final int BOARD_HEIGHT = 20;
private final int CELL_SIZE = 30;
private Timer timer;
private Board board;
private Shape currentShape;
public TetrisGame() {
initUI();
}
private void initUI() {
board = new Board();
add(board);
timer = new Timer(500, this);
timer.start();
setTitle("俄罗斯方块游戏");
setSize(BOARD_WIDTH * CELL_SIZE, BOARD_HEIGHT * CELL_SIZE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addKeyListener(this);
setFocusable(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TetrisGame tetrisGame = new TetrisGame();
tetrisGame.setVisible(true);
});
}
@Override
public void actionPerformed(ActionEvent e) {
// 定时器触发,尝试向下移动当前方块
if (board.canMoveDown(currentShape)) {
currentShape.moveDown();
} else {
// 如果无法继续下移,将当前方块加入游戏板并生成新的方块
board.addShape(currentShape);
currentShape = new Shape();
if (!board.canMoveDown(currentShape)) {
// 如果新生成的方块无法下移,游戏结束
timer.stop();
JOptionPane.showMessageDialog(this, "游戏结束!");
System.exit(0);
}
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
// 根据按键事件执行相应操作
if (e.getKeyCode() == KeyEvent.VK_LEFT && board.canMoveLeft(currentShape)) {
// 左移
currentShape.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT && board.canMoveRight(currentShape)) {
// 右移
currentShape.moveRight();
} else if (e.getKeyCode() == KeyEvent.VK_DOWN && board.canMoveDown(currentShape)) {
// 下移
currentShape.moveDown();
} else if (e.getKeyCode() == KeyEvent.VK_SPACE && board.canRotate(currentShape)) {
// 旋转
currentShape.rotate();
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
// 游戏板
class Board extends JPanel {
private boolean[][] filledCells;
public Board() {
filledCells = new boolean[BOARD_HEIGHT][BOARD_WIDTH];
currentShape = new Shape();
}
// 判断是否可以左移
public boolean canMoveLeft(Shape shape) {
for (Cell cell : shape.getCells()) {
if (cell.getX() <= 0 || filledCells[cell.getY()][cell.getX() - 1]) {
return false;
}
}
return true;
}
// 判断是否可以右移
public boolean canMoveRight(Shape shape) {
for (Cell cell : shape.getCells()) {
if (cell.getX() >= BOARD_WIDTH - 1 || filledCells[cell.getY()][cell.getX() + 1]) {
return false;
}
}
return true;
}
// 判断是否可以下移
public boolean canMoveDown(Shape shape) {
for (Cell cell : shape.getCells()) {
if (cell.getY() >= BOARD_HEIGHT - 1 || filledCells[cell.getY() + 1][cell.getX()]) {
return false;
}
}
return true;
}
// 判断是否可以旋转
public boolean canRotate(Shape shape) {
Shape rotatedShape = shape.getRotatedShape();
for (Cell cell : rotatedShape.getCells()) {
if (cell.getX() < 0 || cell.getX() >= BOARD_WIDTH || cell.getY() >= BOARD_HEIGHT || filledCells[cell.getY()][cell.getX()]) {
return false;
}
}
return true;
}
// 将方块加入已填充单元格
public void addShape(Shape shape) {
for (Cell cell : shape.getCells()) {
filledCells[cell.getY()][cell.getX()] = true;
}
clearLines();
}
// 消除满行
private void clearLines() {
for (int i = BOARD_HEIGHT - 1; i >= 0; i--) {
boolean isFullLine = true;
for (int j = 0; j < BOARD_WIDTH; j++) {
if (!filledCells[i][j]) {
isFullLine = false;
break;
}
}
if (isFullLine) {
moveLinesDown(i);
}
}
}
// 将上方所有行向下移动
private void moveLinesDown(int row) {
for (int i = row; i > 0; i--) {
for (int j = 0; j < BOARD_WIDTH; j++) {
filledCells[i][j] = filledCells[i - 1][j];
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawBoard(g);
drawShape(g, currentShape);
}
// 绘制游戏板
private void drawBoard(Graphics g) {
for (int i = 0; i < BOARD_HEIGHT; i++) {
for (int j = 0; j < BOARD_WIDTH; j++) {
if (filledCells[i][j]) {
g.setColor(Color.BLUE);
g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
g.setColor(Color.BLACK);
g.drawRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
}
// 绘制方块
private void drawShape(Graphics g, Shape shape) {
for (Cell cell : shape.getCells()) {
g.setColor(Color.RED);
g.fillRect(cell.getX() * CELL_SIZE, cell.getY() * CELL_SIZE, CELL_SIZE, CELL_SIZE);
g.setColor(Color.BLACK);
g.drawRect(cell.getX() * CELL_SIZE, cell.getY() * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
// 单元格
class Cell {
private int x;
private int y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
// 方块
class Shape {
private List cells;
public Shape() {
cells = new ArrayList<>();
// 初始化形状
Random random = new Random();
int shapeType = random.nextInt(7); // 0 到 6
switch (shapeType) {
case 0: // I
cells.add(new Cell(3, 0));
cells.add(new Cell(3, 1));
cells.add(new Cell(3, 2));
cells.add(new Cell(3, 3));
break;
case 1: // J
cells.add(new Cell(4, 0));
cells.add(new Cell(4, 1));
cells.add(new Cell(4, 2));
cells.add(new Cell(3, 2));
break;
case 2: // L
cells.add(new Cell(3, 0));
cells.add(new Cell(3, 1));
cells.add(new Cell(3, 2));
cells.add(new Cell(4, 2));
break;
case 3: // O
cells.add(new Cell(4, 0));
cells.add(new Cell(4, 1));
cells.add(new Cell(3, 0));
cells.add(new Cell(3, 1));
break;
case 4: // S
cells.add(new Cell(4, 1));
cells.add(new Cell(3, 1));
cells.add(new Cell(3, 0));
cells.add(new Cell(2, 0));
break;
case 5: // T
cells.add(new Cell(3, 0));
cells.add(new Cell(3, 1));
cells.add(new Cell(3, 2));
cells.add(new Cell(4, 1));
break;
case 6: // Z
cells.add(new Cell(2, 1));
cells.add(new Cell(3, 1));
cells.add(new Cell(3, 0));
cells.add(new Cell(4, 0));
break;
}
}
public List| getCells() {
return cells;
}
// 左移
public void moveLeft() {
for (Cell cell : cells) {
cell.x--;
}
}
// 右移
public void moveRight() {
for (Cell cell : cells) {
cell.x++;
}
}
// 下移
public void moveDown() {
for (Cell cell : cells) {
cell.y++;
}
}
// 旋转
public void rotate() {
int centerX = cells.get(1).getX();
int centerY = cells.get(1).getY();
for (Cell cell : cells) {
int x = cell.getX() - centerX;
int y = cell.getY() - centerY;
cell.x = centerX - y;
cell.y = centerY + x;
}
}
// 获取旋转后的形状
public Shape getRotatedShape() {
Shape rotatedShape = new Shape();
rotatedShape.cells.clear();
rotatedShape.cells.addAll(this.cells);
rotatedShape.rotate();
return rotatedShape;
}
}
}
| | 
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332