这段代码是一个俄罗斯方块游戏的实现,下面是对代码的注释:

package class0809;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tetris {
    public static final int FRAME_WIDTH = 305;
    public static final int FRAME_HEIGHT = 525;
    
    public static void main(String[] args) {
        // 创建游戏窗口,并设置相关属性
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                GameWindow gameWindow = new GameWindow();    
                gameWindow.setTitle("Tetris");
                gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                gameWindow.setLocationByPlatform(true);
                gameWindow.setSize(FRAME_WIDTH, FRAME_HEIGHT);
                gameWindow.setResizable(true);
                gameWindow.setVisible(true);
                gameWindow.startGame();
            }
        });
    }
}

class GameWindow extends JFrame {
    private static final long serialVersionUID = 1L;
    public static final int BLOCK_SIZE = 20;
    public static final int HORIZON_NODES = Tetris.FRAME_WIDTH / BLOCK_SIZE;
    public static final int VERTICAL_NODES = Tetris.FRAME_HEIGHT / BLOCK_SIZE;
    public static int[][] space = new int[VERTICAL_NODES][HORIZON_NODES];
    private Canvas canvas;
    private Timer timer;
    private int score;
    // 七种方块的形状
    private final int[][][] shapes = {
        {{-1,0},{0,0},{-1,1},{0,1}},
        {{-1,0},{0,0},{1,0}, {2,0}},
        {{-1,0},{0,0},{0,1}, {1,1}},
        {{-1,1},{0,1},{0,0}, {1,0}},
        {{-1,1},{-1,0},{0,0},{1,0}},
        {{-1,0},{0,0},{1,0}, {1,1}},
        {{-1,0},{0,0},{1,0}, {0,1}}
    };
    private Point centerPos = new Point();
    private int[][] currentShape = new int[4][2];
    
    public GameWindow() {
        canvas = new Canvas();
        addKeyListener(new KeyHandler());
        add(canvas);
        pack();
    }
    
    public void startGame() {
        choseShape();
        timer = new Timer(300, new TimerHandler());
        timer.start();
    }
    
    private void choseShape() {
        // 随机选择一种形状的方块
        int index = (int) Math.round(Math.random() * 6);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 2; j++) {
                currentShape[i][j] = shapes[index][i][j];
            }
        }
        centerPos.x = HORIZON_NODES / 2;
        centerPos.y = 0;
        updateSpace(1);
    }
    
    private boolean moveDown() {
        // 判断方块是否可以向下移动
        for (int i = 0; i < 4; i++) {
            int x = centerPos.x + currentShape[i][0];
            int y = centerPos.y + currentShape[i][1] + 1;
            if (y >= VERTICAL_NODES-1 || space[y][x] == 2)
                return false;
        }
        updateSpace(0);
        centerPos.y++;
        updateSpace(1);
        return true;
    }
    
    private void transform() {
        // 旋转方块
        int[][] temp = new int[4][2];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 2; j++) {
                temp[i][j] = currentShape[i][j];
            }
        }
        for (int i = 0; i < 4; i++) {
            int t = temp[i][1];
            temp[i][1] = temp[i][0];
            temp[i][0] = -1 * t;
        }
        for (int i = 0; i < 4; i++) {
            int x = centerPos.x + temp[i][0];
            int y = centerPos.y + temp[i][1];
            if (x < 0 || x >= HORIZON_NODES || y < 0 || y >= VERTICAL_NODES || space[y][x] == 2)
                return;
        }
        updateSpace(0);
        currentShape = temp;
        updateSpace(1);
    }
    
    private void moveLeft() {
        // 将方块向左移动
        for (int i = 0; i < 4; i++) {
            int x = centerPos.x + currentShape[i][0] - 1;
            int y = centerPos.y + currentShape[i][1];
            if (x < 0 || space[y][x] == 2)
                return;
        }
        updateSpace(0);
        centerPos.x--;
        updateSpace(1);
    }
    
    private void moveRight() {
        // 将方块向右移动
        for (int i = 0; i < 4; i++) {
            int x = centerPos.x + currentShape[i][0] + 1;
            int y = centerPos.y + currentShape[i][1];
            if (x >= HORIZON_NODES || space[y][x] == 2)
                return;
        }
        updateSpace(0);
        centerPos.x++;
        updateSpace(1);
    }
    
    private void fixBox() {
        // 将方块固定在场景中
        updateSpace(2);
    }
    
    private void clearLine() {
        // 消除满行的方块
        int y = centerPos.y + currentShape[0][1];
        int minY = y, maxY = y;
        for (int i = 1; i < 4; i++) {
            y = centerPos.y + currentShape[i][1];
            if (y > maxY) maxY = y;
            else if (y < minY) minY = y;
        }
        for(y = minY; y <= maxY; y++) {
            int x;
            for (x = 0; x < HORIZON_NODES; x++) {
                if (space[y][x] == 0) break;
            }
            if (x == HORIZON_NODES) {
                for (int i = 0; i < HORIZON_NODES; i++) {
                    for (int j = y; j > 0; j--) {
                        space[j][i] = space[j-1][i];
                    }
                }
                score ++;
            }
        }
    }
    
    private void updateSpace(int flag) {
        // 更新场景中的方块状态
        for (int i = 0; i < 4; i++) {
            int x = centerPos.x + currentShape[i][0];
            int y = centerPos.y + currentShape[i][1];
            space[y][x] = flag;
        }
    }
    
    private boolean gameOver() {
        // 判断游戏是否结束
        for (int i = 0; i < 4; i++) {
            int y = centerPos.y + currentShape[i][1];
            if (y == 0) return true;
        }
        return false;
    }
    
    class TimerHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (!moveDown()) {
                fixBox();
                clearLine();
                timer.stop();
                if (gameOver())
                    JOptionPane.showMessageDialog(GameWindow.this, String.format("游戏结束,你的得分为%d", score));
                else
                    startGame();
            }
            canvas.repaint();
        }
    }
    
    private class KeyHandler extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                    transform();
                    canvas.repaint();
                    break;
                case KeyEvent.VK_DOWN:
                    timer.setDelay(30);
                    break;
                case KeyEvent.VK_LEFT:
                    moveLeft();
                    canvas.repaint();
                    break;
                case KeyEvent.VK_RIGHT:
                    moveRight();
                    canvas.repaint();
                    break;
            }
        }
    }
}
``
给代码加注释package class0809; description TODO 类描述 author peibin date 202388 import javaawt;import javaawtevent;import javaxswing;public class Tetris public static final int FRAME_WIDTH = 305; pub

原文地址: https://www.cveoy.top/t/topic/iyTo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录