Eclipse IDE

jfx

java模块

算法

定义了三个数据结构

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;
}

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

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

一个游戏逻辑类

	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++) {
var cube = boardData[row][col].visable;
b = b && cube;
}

if (b) {
removed++;
removeCount++;
for (int ui = row - 1; ui >= 0; ui--) {
for (int uj = 0; uj < 10; uj++) {
var pre = boardData[ui][uj];
boardData[ui + 1][uj].visable = pre.visable;
boardData[ui + 1][uj].color = pre.color;
}
}
}
}

var tempScore = 0;
if (removeCount == 1)
tempScore += 100;
if (removeCount == 2)
tempScore += 200;
if (removeCount == 3)
tempScore += 400;
if (removeCount == 4)
tempScore += 400;

score += tempScore;
levelScore += tempScore;

if (levelScore >= 2000) {
if (level < 12) {
level++;
ticks = 1065 - level * 65;
} else
level = 1;

levelScore = 0;
}
}

界面渲染

for (int i = 0; i < 20; i++) {
for (int j = 0; j < 10; j++) {
Ellipse ellipse = new Ellipse();
ellipse.setCenterX(j * 30.0f + 15);
ellipse.setCenterY((19 - i) * 30.0f + 15);
ellipse.setFill(Color.WHITE);
cubes[19 - i][j] = ellipse;
}
}

	/**
*
* 刷新界面
*/
private void render() {

var datas = data.getBoardData();

for (int row = 0; row < 20; row++) {
for (int col = 0; col < 10; col++) {
var cell = datas[row][col];
Ellipse cube = cubes[row][col];
if (!cell.visable) {
cube.setFill(Color.WHITE);
} else {
var c = cell.color;
Stop[] stops = { new Stop(0, Color.WHITE), new Stop(1, Color.valueOf(c)) };

stops);
}
}
}

if (data.getCurrent() == null)
return;
var arr = data.getCurrent().getData();

int sh = data.getCurrent().getHeight();
int sw = data.getCurrent().getWidth();

for (int row = 0; row < sh; row++) {
for (var col = 0; col < sw; col++) {
if (arr[row][col]) {
var cube = cubes[data.getCurrent().getTop() + row][data.getCurrent().getLeft() + col];
var c = data.getCurrent().getColor();

// cube.setFill(Color.valueOf(c));
Stop[] stops = { new Stop(0, Color.WHITE), new Stop(1, Color.valueOf(c)) };

stops);
}

}

for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
next[i][j].setFill(
data.getNext().getData()[i][j] ? Color.valueOf(data.getNext().getColor()) : Color.DARKGRAY);
}
}
}

Platform.runLater(new Runnable() {
@Override
public void run() {
score.setText(String.format("分数：%d", data.getScore()));
level.setText(String.format("级别：1", data.getLevel()));

if (data.isFull()) {
started = false;

ButtonType loginButtonType = new ButtonType("OK", ButtonData.OK_DONE);
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("Game Over!");
dialog.setContentText("Game Over!");

boolean disabled = false; // computed based on content of text fields, for example
dialog.showAndWait();
}
}
});

}

用到的工具技术

/*
*/
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
javafx {
version = "14"
modules = ['javafx.controls','javafx.fxml','javafx.base']
}
launcher {
name = 'tetris'
}
mergedModule {
additive = true  // redundant, because excludeXXX() methods are also present
//requires 'java.ws.rs'

}
}
java {
modularity.inferModulePath = true

}

mainClassName = "tetris.Main"

dependencies {

}

Eclipse IDE

IDE就不用讲了，强大的代码编辑功能，为写程序提供了很多方便。

jfx

jfx是个兼容并包的东西，如果你有微软WPF的经验，会发现jfx很容易上手，它们在设计上有着相似的理念。另外，jfx可以用html+css3来渲染，因而有web开发经验的同学上手jfx也不算难。如果有awt和swing的经验的同学，会发现jfx基本就是复制了一份类库出来，因而上手也比较容易。

多线程

	public synchronized void moveDown() {

if (!this.isBottom()) {
current.moveDown();
} else {

remove();

}
}

	private void timerTask() {
render();

@Override
public void run() {
data.moveDown();
if (!data.isFull())

}
}, data.getTicks());
}

声效

var audioSrc = getClass().getResource("1.mp3");
var m = new Media(audioSrc.toString());

audio1 = new MediaPlayer(m);
audioSrc = getClass().getResource("2.mp3");
m = new Media(audioSrc.toString());
audio2 = new MediaPlayer(m);

data.setOnRemove(() -> {
audio2.stop();
audio2.play();
});

06-09 6万+

11-19 219
05-24 1359
07-29
11-21 114
11-20 29