尝试用JavaFX写个俄罗斯方块

本文介绍了作者使用JavaFX开发俄罗斯方块游戏的过程,包括算法设计、数据结构定义、游戏逻辑实现、界面渲染以及所用到的工具技术,如Gradle、Eclipse IDE和JavaFX。文章详细阐述了游戏的自动移动、消行、得分计算等功能,并分享了项目的源代码链接。
摘要由CSDN通过智能技术生成

目录

先截个图

算法

定义了三个数据结构

一个游戏逻辑类

界面渲染

用到的工具技术

Gradle构建工具

Eclipse IDE

jfx

多线程

java模块

声效


概述

一时半会也不知道写点什么,就做个简易的俄罗斯方块吧。这个游戏的算法早就想出来了,但JFX是第一次接触,一边查资料,一边写,前前后后用了将近半个月时间,终于初见成效。目前完成了俄罗斯方块的基本操作,形状颜色区分、算分、升级加速、形状预览等功能。下图是游戏中的一个截图,玩法跟常规的俄罗斯方块无异:

俄罗斯方块

算法

这个俄罗斯方块的算法很简单,用20*10个球作为前端的呈现,后台放一个对应维数的数组来保存每个球的状态,每次移动完刷新界面,以呈现移动动画的效果。无键盘操作的时候,使用一个定时器,自动向下移动方块。移动到底部的时候,消去满行,将上部方块向移动,并计算分数。

定义了三个数据结构

一个是移动的形状,是一个4*4的矩阵,加上top、left、height、width、corlor五个属性。

public class ShapeStruct {
//省略代码...
	public int left;
	public int top;
	public boolean[][] data;
	public String color;
	private int width;
	private int height;
//省略代码...
}

表示格子的定义

public class BlockCell {
	public String color = "#FFFFFF";
	public boolean visable = false;
}

预定义形状的定义,比ShapeStruct 少了两个位置属性。

public class Block {
//省略代码...

	public boolean[][] data;
	public String color;
	private int width;
	private int height;
//省略代码...
}

一个游戏逻辑类

随机形状、移动、消行、算分等都在这个类里边实现。这个类里边不涉及UI元素,纯粹游戏逻辑。

比如消行部分的逻辑实现:

	private void removeRows() {
		if (preview != null) {
			for (var row = 0; row < preview.getHeight(); row++) {
				for (var col = 0; col < preview.getWidth(); col++) {
					if (preview.getCell(row, col)) {
						var cell = boardData[preview.getTop() + row][preview.getLeft() + col];
						cell.visable = true;
						cell.color = preview.color;
					}
				}
			}
		}
		preview = null;

		int removeCount = 0;
		for (int row = 0; row < 20; row++) {
			boolean b = true;
			for (int col = 0; col < 10; col+&#
以下是Java编写俄罗斯方块的代码及其每行代码的作用: ```java import java.util.Random; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; ``` 这些是需要引入的包,在这里我们需要使用`Random`类和JavaFX相关的类。 ```java public class Tetromino { private Tetrominoes pieceShape; private int[][] coords; private int[][][] coordsTable; private Board board; private Color color; private Random rand; } ``` 这是`Tetromino`类的定义,表示俄罗斯方块的形状、坐标、坐标表、所在的游戏板、颜色以及随机数生成器。 ```java private static final int[][][] coordsTable = { {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, {{0, -1}, {0, 0}, {-1, 0}, {-1, 1}}, {{0, -1}, {0, 0}, {1, 0}, {1, 1}}, {{0, -1}, {0, 0}, {0, 1}, {0, 2}}, {{-1, 0}, {0, 0}, {1, 0}, {0, 1}}, {{0, 0}, {1, 0}, {0, 1}, {1, 1}}, {{-1, -1}, {0, -1}, {0, 0}, {0, 1}}, {{1, -1}, {0, -1}, {0, 0}, {0, 1}} }; ``` 这是一个坐标表,表示所有七种俄罗斯方块的所有可能的形状和方向。每个方块用一个`int[][]`数组表示,其中每个元素都是一个长度为2的`int`数组,表示其在游戏板上的(x,y)坐标。 ```java public Tetromino() { rand = new Random(); coords = new int[4][2]; coordsTable = new int[][][] { {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, {{0, -1}, {0, 0}, {-1, 0}, {-1, 1}}, {{0, -1}, {0, 0}, {1, 0}, {1, 1}}, {{0, -1}, {0, 0}, {0, 1}, {0, 2}}, {{-1, 0}, {0, 0}, {1, 0}, {0, 1}}, {{0, 0}, {1, 0}, {0, 1}, {1, 1}}, {{-1, -1}, {0, -1}, {0, 0}, {0, 1}}, {{1, -1}, {0, -1}, {0, 0}, {0, 1}} }; setShape(Tetrominoes.NoShape); } ``` 这是`Tetromino`类的构造函数,初始化随机数生成器、坐标数组和坐标表,并将形状设置为`NoShape`。 ```java public void setShape(Tetrominoes shape) { coordsTable = Tetromino.coordsTable; for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; ++j) { coords[i][j] = coordsTable[shape.ordinal()][i][j]; } } pieceShape = shape; setColor(Tetromino.colorTable[shape.ordinal()]); } ``` 这是设置方块形状的方法,根据给定的形状从坐标表中获取该形状对应的坐标数组,并将颜色设置为相应的颜色。 ```java private void setColor(Color color) { this.color = color; } ``` 这是设置方块颜色的方法。 ```java public int getX(int index) { return coords[index][0]; } public int getY(int index) { return coords[index][1]; } ``` 这是获取方块某个坐标的x和y值的方法。 ```java public Tetrominoes getShape() { return pieceShape; } public void setX(int index, int x) { coords[index][0] = x; } public void setY(int index, int y) { coords[index][1] = y; } public Color getColor() { return color; } ``` 这是获取和设置方块形状、坐标和颜色的方法。 ```java public int minX() { int m = coords[0][0]; for (int i = 0; i < 4; i++) { m = Math.min(m, coords[i][0]); } return m; } public int minY() { int m = coords[0][1]; for (int i = 0; i < 4; i++) { m = Math.min(m, coords[i][1]); } return m; } public int maxX() { int m = coords[0][0]; for (int i = 0; i < 4; i++) { m = Math.max(m, coords[i][0]); } return m; } public int maxY() { int m = coords[0][1]; for (int i = 0; i < 4; i++) { m = Math.max(m, coords[i][1]); } return m; } ``` 这是获取方块在x和y轴上的最小和最大坐标值的方法。 ```java public void rotateLeft() { if (pieceShape == Tetrominoes.SquareShape) { return; } for (int i = 0; i < 4; i++) { int x = coords[i][0]; int y = coords[i][1]; coords[i][0] = y; coords[i][1] = -x; } } public void rotateRight() { if (pieceShape == Tetrominoes.SquareShape) { return; } for (int i = 0; i < 4; i++) { int x = coords[i][0]; int y = coords[i][1]; coords[i][0] = -y; coords[i][1] = x; } } ``` 这是旋转方块的方法,对于正方形方块不做处理。左旋转将方块坐标(x,y)转换为(y,-x),右旋转将其转换为(-y,x)。 ```java public boolean tryMove(int deltaX, int deltaY) { for (int i = 0; i < 4; i++) { int x = coords[i][0] + deltaX; int y = coords[i][1] + deltaY; if (x < 0 || x >= Board.BoardWidth || y < 0 || y >= Board.BoardHeight) { return false; } if (board.getShape(x, y) != Tetrominoes.NoShape) { return false; } } for (int i = 0; i < 4; i++) { coords[i][0] += deltaX; coords[i][1] += deltaY; } board.update(); return true; } ``` 这是尝试将方块沿x和y轴移动的方法,先检查是否超出游戏板范围或与其他方块重叠,如果没有则更新方块坐标并更新游戏板。如果移动成功则返回true,否则返回false。 ```java private static final Color[] colorTable = { Color.rgb(0, 0, 0), Color.rgb(204, 102, 102), Color.rgb(102, 204, 102), Color.rgb(102, 102, 204), Color.rgb(204, 204, 102), Color.rgb(204, 102, 204), Color.rgb(102, 204, 204), Color.rgb(218, 170, 0) }; ``` 这是一个颜色表,表示每个形状对应的颜色。 ```java public enum Tetrominoes { NoShape, ZShape, SShape, LineShape, TShape, SquareShape, LShape, MirroredLShape } ``` 这是一个枚举类型,表示所有可能的方块形状。`NoShape`表示空方块,其他几个表示不同形状的方块。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值