用Java写一个俄罗斯方块

时间: 2023-08-23 09:05:53 浏览: 41
好的,以下是Java实现俄罗斯方块的代码示例: ```java import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.ArrayList; import java.util.Random; import javax.swing.JPanel; public class Tetris extends JPanel implements Runnable { 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(2, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(2, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) } } }; 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<Integer>(); private long score; private Color[][] well; private boolean gameOver = false; private Thread gameThread; private final Object pauseLock = new Object(); private boolean isPaused = false; public Tetris() { setPreferredSize(new Dimension(200, 400)); setBackground(Color.BLACK); 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(); } public void newPiece() { pieceOrigin = new Point(5, 2); rotation = 0; if (nextPieces.isEmpty()) { Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6); Collections.shuffle(nextPieces); } 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.x + x][p.y + y] != Color.BLACK) { return true; } } return false; } public void rotate(int i) { int newRotation = (rotation + i) % 4; if (newRotation < 0) { newRotation = 3; } if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) { rotation = newRotation; } repaint(); } public void move(int i) { if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) { pieceOrigin.x += i; } repaint(); } public void dropDown() { if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) { pieceOrigin.y += 1; } else { fixToWell(); } repaint(); } public void fixToWell() { for (Point p : Tetraminos[currentPiece][rotation]) { well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece]; } clearRows(); newPiece(); if (collidesAt(pieceOrigin.x, pieceOrigin.y, rotation)) { gameOver = true; pause(); } } public void deleteRow(int row) { for (int j = row-1; j > 0; j--) { for (int i = 1; i < 11; i++) { well[i][j+1] = well[i][j]; } } } 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) { deleteRow(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; } } public void drawPiece(Graphics g) { g.setColor(tetraminoColors[currentPiece]); for (Point p : Tetraminos[currentPiece][rotation]) { g.fillRect((p.x + pieceOrigin.x) * 26, (p.y + pieceOrigin.y) * 26, 25, 25); } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); // Draw 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 drawPiece(g); } public void pause() { isPaused = true; } public void resume() { synchronized (pauseLock) { isPaused = false; pauseLock.notifyAll(); } } public void run() { while (!gameOver) { synchronized (pauseLock) { while (isPaused) { try { pauseLock.wait(); } catch (InterruptedException e) { } } } dropDown(); try { Thread.sleep(400); } catch ( InterruptedException e ) {} } } public static void main(String[] args) { Tetris game = new Tetris(); JFrame f = new JFrame("Tetris"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(12*26+10, 26*23+25); f.add(game); f.setVisible(true); game.start(); } public void start() { gameOver = false; score = 0; newPiece(); gameThread = new Thread(this); gameThread.start(); } } ``` 这个示例中,我们使用了Java的Swing来实现游戏界面,使用了多线程来控制游戏的运行。具体实现中,我们定义了方块的类型和颜色,以及游戏区域。游戏区域由一个二维数组表示,每一种方块都是由自己所在位置和相对位置组成的一个数组,通过不停地调整方块的位置和旋转来达到消除行的目的。

相关推荐

最新推荐

recommend-type

基于SpringBoot框架仿stackOverflow网站后台开发.zip

基于springboot的java毕业&课程设计
recommend-type

基于SpringBoot洗衣店管理系统.zip

基于springboot的java毕业&课程设计
recommend-type

【优化覆盖】算术算法求解传感器覆盖优化问题【含Matlab源码 2436期】.zip

【优化覆盖】算术算法求解传感器覆盖优化问题【含Matlab源码 2436期】.zip
recommend-type

【优化覆盖】蜣螂算法DBO求解无线传感器WSN覆盖优化问题【含Matlab源码 3567期】.zip

【优化覆盖】蜣螂算法DBO求解无线传感器WSN覆盖优化问题【含Matlab源码 3567期】.zip
recommend-type

FusionCompute修改VRM节点IP地址

FusionCompute修改VRM节点IP地址 该任务指导工程师对VRM节点的IP地址、主机的管理IP地址进行修改。 执行该任务时应注意: • 建议同时修改VRM和主机的管理IP。如果修改了VRM的IP,会导致本地PC与VRM的连接短暂中断。 • 修改前应已完成网络规划,并在FusionCompute中确认VRM节点运行正常,所有主机运行正常(无处于异常或维护状态的主机)。 • 如果跨网段修改IP地址时,则应注意在完成所有节点IP地址的修改后,在相应的汇聚交换机进行配置,保证修改后的主机IP地址、VRM节点及本地PC之间能进行正常通信。相关交换机配置命令,请参考交换机配置样例。 • 如果跨网段修改管理IP地址,同时涉及修改管理VLAN,请先修改管理平面VLAN,待修改完成,且各节点与VRM网络通信正常后,再进行修改VRM IP地址和主机IP地址的操作。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。