用java写一个俄罗斯方块游戏

时间: 2023-12-18 08:03:32 浏览: 29
好的,下面是一个基于Java Swing的俄罗斯方块游戏的代码示例: ``` import java.awt.Color; import java.awt.Dimension; 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.Random; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class Tetris extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; private static final int BLOCK_SIZE = 30; private static final int BOARD_WIDTH = 10; private static final int BOARD_HEIGHT = 20; private static final int GAME_SPEED = 500; private Timer timer; private Block currentBlock; private ArrayList<Block> blocks; private int[][] board; private Random random; public Tetris() { setPreferredSize(new Dimension(BLOCK_SIZE * BOARD_WIDTH, BLOCK_SIZE * BOARD_HEIGHT)); setBackground(Color.WHITE); timer = new Timer(GAME_SPEED, this); random = new Random(); board = new int[BOARD_HEIGHT][BOARD_WIDTH]; blocks = new ArrayList<Block>(); addNewBlock(); timer.start(); } private void addNewBlock() { int type = random.nextInt(7); int x = BOARD_WIDTH / 2 - 1; int y = 1; currentBlock = new Block(type, x, y); if (isColliding(currentBlock)) { timer.stop(); JFrame frame = (JFrame) getTopLevelAncestor(); frame.setTitle("Game Over!"); return; } blocks.add(currentBlock); } private boolean isColliding(Block block) { for (Point p : block.getPoints()) { int x = block.getX() + p.x; int y = block.getY() + p.y; if (x < 0 || x >= BOARD_WIDTH || y >= BOARD_HEIGHT || board[y][x] != 0) { return true; } } return false; } private void mergeBlocks() { for (Point p : currentBlock.getPoints()) { int x = currentBlock.getX() + p.x; int y = currentBlock.getY() + p.y; board[y][x] = currentBlock.getType() + 1; } blocks.remove(currentBlock); } private void clearLines() { int lines = 0; for (int i = BOARD_HEIGHT - 1; i >= 0; i--) { boolean isLineFull = true; for (int j = 0; j < BOARD_WIDTH; j++) { if (board[i][j] == 0) { isLineFull = false; break; } } if (isLineFull) { for (int j = i; j > 0; j--) { System.arraycopy(board[j - 1], 0, board[j], 0, BOARD_WIDTH); } lines++; i++; } } if (lines > 0) { JFrame frame = (JFrame) getTopLevelAncestor(); frame.setTitle(String.format("Tetris - Score: %d", lines)); } } @Override public void actionPerformed(ActionEvent e) { currentBlock.moveDown(); if (isColliding(currentBlock)) { currentBlock.moveUp(); mergeBlocks(); addNewBlock(); clearLines(); } repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < BOARD_HEIGHT; i++) { for (int j = 0; j < BOARD_WIDTH; j++) { int type = board[i][j]; if (type > 0) { g.setColor(getBlockColor(type)); g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); g.setColor(Color.BLACK); g.drawRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } for (Block block : blocks) { int type = block.getType() + 1; g.setColor(getBlockColor(type)); for (Point p : block.getPoints()) { int x = block.getX() + p.x; int y = block.getY() + p.y; g.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); g.setColor(Color.BLACK); g.drawRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } private Color getBlockColor(int type) { switch (type) { case 1: return Color.BLUE; case 2: return Color.RED; case 3: return Color.GREEN; case 4: return Color.YELLOW; case 5: return Color.ORANGE; case 6: return Color.PINK; case 7: return Color.CYAN; default: 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); } private static class Block { private int type; private int x; private int y; private Point[] points; public Block(int type, int x, int y) { this.type = type; this.x = x; this.y = y; this.points = getPointsForType(type); } public void moveLeft() { x--; } public void moveRight() { x++; } public void moveDown() { y++; } public void moveUp() { y--; } public int getType() { return type; } public int getX() { return x; } public int getY() { return y; } public Point[] getPoints() { return points; } private Point[] getPointsForType(int type) { switch (type) { case 0: return new Point[]{new Point(0, 0), new Point(1, 0), new Point(0, 1), new Point(1, 1)}; case 1: return new Point[]{new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(3, 0)}; case 2: return new Point[]{new Point(0, 0), new Point(1, 0), new Point(0, 1), new Point(0, 2)}; case 3: return new Point[]{new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1)}; case 4: return new Point[]{new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0)}; case 5: return new Point[]{new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2)}; case 6: return new Point[]{new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2)}; default: return new Point[0]; } } } } ``` 运行这段代码,你就可以在窗口中体验俄罗斯方块游戏了。

相关推荐

最新推荐

recommend-type

基于关键词搜索结果的微博爬虫(下载即用).zip

基于关键词搜索结果的微博爬虫(下载即用).zip本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,当然也适合小白学习进阶。如果基础还行,可以在此代码基础上进行修改,以实现其他功能。 基于关键词搜索结果的微博爬虫(下载即用).zip本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,当然也适合小白学习进阶。如果基础还行,可以在此代码基础上进行修改,以实现其他功能。 基于关键词搜索结果的微博爬虫(下载即用).zip本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,当然也适合小白学习进阶。如果基础还行,可以在此代码基础上进行修改,以实现其他功能。 基于关键词搜索结果的微博爬虫(下载即用).zip本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,当然也适合小白学习进阶。如果基础还行,可以在此代码基础上进行修改
recommend-type

node-v4.4.1-headers.tar.xz

Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
recommend-type

J波模拟matlab代码.zip

1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
recommend-type

Obsidian quickadd插件

Obsidian quickadd插件
recommend-type

Fraunhofer Versatile Video Encoder (VVenC) v0.1

这份文件是关于Fraunhofer Versatile Video Encoder (VVenC) v0.1版本的文档,由Fraunhofer Heinrich Hertz Institute (HHI)的视频编码与分析部门的Jens Brandenburg, Adam Wieckowski, Tobias Hinz, Benjamin Bross撰写
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

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

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